我的网站中有一个 png 图像上传脚本,我正在将大图像缩小为小尺寸,并且我正在使用此代码,但它丢失了透明背景
$image = $tmp; // Uploaded Image
$maxImgWidth = 224;
$src = imagecreatefrompng($image);
list($width, $height) = getimagesize($image);
$newwidth = $maxImgWidth;
$newheight = ($height / $width) * $newwidth;
$newImage = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagesavealpha($image, true);
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($newImage, "../thumb/$name_after-$code.$ext", 1);
imagedestroy($src);
imagedestroy($newImage);
此代码使图像不透明:
我想要像这样的透明背景:
最佳答案
抱歉,我没有 PHP 经验,但在我阅读 this page 后,我认为您可能需要在设置保存阿尔法为真
后添加透明颜色imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $color);
如果仍然无法尝试在imagecolorallocatealpha()
之后添加此内容,
imagecolortransparent($image, $color);
关于PHP 调整图像大小并保存透明 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22316475/