note 78379 deleted from function.imagecolortransparent by cmb
| From: | cmb@php.net | Date: | Sun, 01 Dec 2019 08:40:49 +0000 |
| Subject: | note 78379 deleted from function.imagecolortransparent by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213313@lists.php.net to get a copy of this message | ||
Note Submitter: Rob
----
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);
}
}
?>