下面的PHP代码片段使用GD调整浏览器的大小,将PNG上传到128x128.它工作得很好,除了原始图像中的透明区域被替换为纯色——在我的例子中是黑色.
即使设定了imagesavealpha
,还是有些地方不太对劲.
在重采样的图像中保持透明度的最佳方式是什么?
$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 );