Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PNG Transparency with PHP

Hey having some trouble trying to maintain transparency on a png when i create a thumbnail from it, anyone any experience with this? any help would be great, here's what i am currently doing:

$fileName= "../js/ajaxupload/tees/".$fileName;  list($width, $height) = getimagesize($fileName);  $newwidth = 257; $newheight = 197;  $thumb = imagecreatetruecolor($newwidth, $newheight); imagealphablending($thumb, true); $source = imagecreatefrompng($fileName); imagealphablending($source, true);  imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);  imagesavealpha($thumb, true); imagepng($thumb,$newFilename); 
like image 577
BastardPrince Avatar asked Nov 23 '08 22:11

BastardPrince


2 Answers

I have had success doing it like this in the past:

$thumb = imagecreatetruecolor($newwidth, $newheight); imagealphablending($thumb, false); imagesavealpha($thumb, true);    $source = imagecreatefrompng($fileName); imagealphablending($source, true);  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);  imagepng($thumb,$newFilename); 

I found the output image quality much better using imagecopyresampled() than imagecopyresized()

like image 72
Tom Haigh Avatar answered Sep 19 '22 18:09

Tom Haigh


Forget the color transparency index, it never works in all rendering products. Instead use an alpha layer mask:

$image = imagecreatetruecolor($size, $size);  imagealphablending($image, false); imagesavealpha($image, true);  $trans_layer_overlay = imagecolorallocatealpha($image, 220, 220, 220, 127); imagefill($image, 0, 0, $trans_layer_overlay); 
like image 43
user629089 Avatar answered Sep 21 '22 18:09

user629089