以下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 );
Run Code Online (Sandbox Code Playgroud) 我想写一个例程,它将PNG图像路径作为参数,并将该图像转换为8位PNG图像.我需要使用PHP GD库.