问题:
The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128x128. 以下PHP代码段使用GD将浏览器上传的PNG调整为128x128。 It works great, except that the transparent areas in the original image are being replaced with a solid color- black in my case. 效果很好,只是在我的情况下,原始图像中的透明区域已被替换为纯黑色。
Even though imagesavealpha
is set, something isn't quite right. 即使设置了imagesavealpha
,也不是很正确。
What's the best way to preserve the transparency in the resampled image? 在重新采样的图像中保留透明度的最佳方法是什么?
$uploadTempFile = $myField[ 'tmp_name' ]
list( $uploadWidth, $uploadHeight, $uploadType )
= getimagesize( $uploadTempFile );
$srcImage = imagecreatefrompng( $uploadTempFile );
imagesavealpha( $targetImage, true );
$targetImage = imagecreatetruecolor( 128, 128 );
imagecopyresampled( $targetImage, $srcImage,
0, 0,
0, 0,
128, 128,
$uploadWidth, $uploadHeight );
imagepng( $targetImage, 'out.png', 9 );