首先,我想说我在PHP GD中阅读了很多关于透明的主题。
php GD 创建一个透明的 png 图像
使用 PHP 的 GDlib 图像复制重采样时,是否可以保留 PNG 图像透明度?
甚至在手册中评论。为了证明我不只是阅读,而是尝试理解,下面我将链接粘贴到我发现的完全错误的评论中。他写的东西与手册中关于 imagesavealpha 的内容完全相反(你必须取消设置 alpha blending(imagealphablending($i'm, false)),才能使用它。
http://pl.php.net/manual/en/function.imagepng.php#85748
http://php.net/manual/en/function.imagesavealpha.php
我想在保存图像之前保持透明。查看代码(它只是一个例如):
$base = imagecreatetruecolor(500,500);
imagealphablending($base, false);
imagesavealpha($base, true);
$image = base64_decode($image);
$image = imageCreateFromString($image);
imagecopyresampled($base,$image,0,0,0,0,500,500,500,500);
//to this step I have transparent
//now if I save image everything is okay
$avatar = imagescale($avatar,101,101);//lost transpratent
imagepng($base,$savePath,0);
我知道我可以在 imagecopyresampled() 操作中缩放图像,并且不需要 2 个操作操作。但我在上面写过,这只是模式。
有人可以写出如何在每次操作后保持透明度。
这是我不久
前编写的用于调整图像大小并保持其透明度的函数:
function resize($image, $src_width, $src_height, $width, $height)
{
if(!$image) return FALSE;
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $src_width, $src_height);
return $new_image;
}
我也在透明度方面遇到过挣扎,但最后我想出了编写上面的代码。我认为这对你有用。