note 78379 added to function.imagecolortransparent
| From: | Rob at osu1 dot php dot net | Date: | Tue, 09 Oct 2007 14:55:22 +0000 |
| Subject: | note 78379 added to function.imagecolortransparent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131248@lists.php.net to get a copy of this message | ||
This block of code should preserve transparency when resizing an image using the GD library.
<?php
/**
* Try to preserve transparency info
* $orig_type - Original Image Type
* $orig_img - GD Image Resource for original image
* $new_img - GD Image Resource for resized image
*/
// Transparency only available for GIFs & PNGs
if ( ($orig_type == IMAGETYPE_GIF) || ($orig_type == IMAGETYPE_PNG) ) {
$trnprt_indx = imagecolortransparent($orig_img);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($orig_img, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'],
$trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($new_img, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($new_img, $trnprt_indx);
// Always make a transparent background color for PNGs that don't have one allocated already
} elseif ($orig_type == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($new_img, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($this->new_img, 0, 0, $color);
// Restore transparency blending
imagesavealpha($this->new_img, true);
}
}
?>
----
Server IP: 216.92.131.65
Probable Submitter: 74.92.61.189
----
Manual Page -- http://www.php.net/manual/en/function.imagecolortransparent.php
Edit -- https://master.php.net/note/edit/78379
Del: integrated -- https://master.php.net/note/delete/78379/integrated
Del: useless -- https://master.php.net/note/delete/78379/useless
Del: bad code -- https://master.php.net/note/delete/78379/bad+code
Del: spam -- https://master.php.net/note/delete/78379/spam
Del: non-english -- https://master.php.net/note/delete/78379/non-english
Del: in docs -- https://master.php.net/note/delete/78379/in+docs
Del: other reasons-- https://master.php.net/note/delete/78379
Reject -- https://master.php.net/note/reject/78379
Search -- https://master.php.net/manage/user-notes.php