Index: branches/5.0.x/core/units/helpers/image_helper.php =================================================================== diff -u -r12322 -r12324 --- branches/5.0.x/core/units/helpers/image_helper.php (.../image_helper.php) (revision 12322) +++ branches/5.0.x/core/units/helpers/image_helper.php (.../image_helper.php) (revision 12324) @@ -1,6 +1,6 @@ _preserveTransparency($src_image_rs, $dst_image_rs, $image_info[2]); } // 1. resize @@ -202,6 +201,53 @@ } /** + * Preserve transparency for GIF and PNG images + * + * @param resource $src_image_rs + * @param resource $dst_image_rs + * @param int $image_type + * @return resource + */ + function _preserveTransparency($src_image_rs, $dst_image_rs, $image_type) + { + $transparent_index = imagecolortransparent($src_image_rs); + + // if we have a specific transparent color + if ($transparent_index >= 0) { + // get the original image's transparent color's RGB values + $transparent_color = imagecolorsforindex($src_image_rs, $transparent_index); + + // allocate the same color in the new image resource + $transparent_index = imagecolorallocate($dst_image_rs, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); + + // completely fill the background of the new image with allocated color + imagefill($dst_image_rs, 0, 0, $transparent_index); + + // set the background color for new image to transparent + imagecolortransparent($dst_image_rs, $transparent_index); + + return $dst_image_rs; + } + + // always make a transparent background color for PNGs that don't have one allocated already + if ($image_type == IMAGETYPE_PNG) { + // turn off transparency blending (temporarily) + imagealphablending($dst_image_rs, false); + + // create a new transparent color for image + $transparent_color = imagecolorallocatealpha($dst_image_rs, 0, 0, 0, 127); + + // completely fill the background of the new image with allocated color + imagefill($dst_image_rs, 0, 0, $transparent_color); + + // restore transparency blending + imagesavealpha($dst_image_rs, true); + } + + return $dst_image_rs; + } + + /** * Fills margins (if any) of resized are with given color * * @param resource $src_image_rs resized image resource