imagecreatefrompng使用PHP GD库处理24位pngs失败


imagecreatefrompng fails with 24bit pngs using PHP GD library

我正试图构建一个脚本,从<input type='file' />调整png的大小,并将其作为8bit png保存到服务器,但我遇到了一些问题。

$srcimage = imagecreatefrompng($orig_source);
$img = imagecreatetruecolor($resize_width, $resize_height);
$bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagecolortransparent($img, $bga);
imagefill($img, 0, 0, $bga);
imagecopy($img, $srcimage, 0, 0, 0, 0, $resize_width, $resize_height);
imagetruecolortopalette($img, false, 255);
imagesavealpha($img, true);
imagepng($img, $filepath);
imagedestroy($img);

如何使用PHP GD库将PNG转换为8位PNG

当我上传一个32位的png时,这很好,当我上传了一个已经8位的pnng时,它保持了透明度,并按预期调整了大小,但当我选择上传一个24位的pnd时,我会收到这个错误

Warning: imagecreatefrompng() [function.imagecreatefrompng]: '/share/MD0_DATA/Qweb/php2zRiNv' is not a valid PNG file
Warning: imagecopy() expects parameter 2 to be resource, boolean given

我知道第二个错误是因为imagecreatefrompng失败了,但我不明白为什么会失败,因为它是一个有效的png文件!

如有任何帮助,我们将不胜感激!

如果要从Photoshop等应用程序保存,请确保使用"保存为web"选项,因为某些应用程序嵌入了指南等非标准数据。