Can PNG Image Transparency Be Preserved When Using PHP's GDlib Imagecopyresampled?
Answer :
imagealphablending( $targetImage, false ); imagesavealpha( $targetImage, true );   did it for me. Thanks ceejayoz.
note, the target image needs the alpha settings, not the source image.
Edit: full replacement code. See also answers below and their comments. This is not guaranteed to be be perfect in any way, but did achieve my needs at the time.
$uploadTempFile = $myField[ 'tmp_name' ] list( $uploadWidth, $uploadHeight, $uploadType )    = getimagesize( $uploadTempFile );  $srcImage = imagecreatefrompng( $uploadTempFile );   $targetImage = imagecreatetruecolor( 128, 128 );    imagealphablending( $targetImage, false ); imagesavealpha( $targetImage, true );  imagecopyresampled( $targetImage, $srcImage,                      0, 0,                      0, 0,                      128, 128,                      $uploadWidth, $uploadHeight );  imagepng(  $targetImage, 'out.png', 9 );  Why do you make things so complicated? the following is what I use and so far it has done the job for me.
$im = ImageCreateFromPNG($source); $new_im = imagecreatetruecolor($new_size[0],$new_size[1]); imagecolortransparent($new_im, imagecolorallocate($new_im, 0, 0, 0)); imagecopyresampled($new_im,$im,0,0,0,0,$new_size[0],$new_size[1],$size[0],$size[1]);  I believe this should do the trick:
$srcImage = imagecreatefrompng($uploadTempFile); imagealphablending($srcImage, false); imagesavealpha($srcImage, true);   edit: Someone in the PHP docs claims imagealphablending should be true, not false. YMMV.
Comments
Post a Comment