首页 > 解决方案 > “如何修复”png图像透明度

问题描述

我创建了一个白色背景并添加了一个 PNG 图像。但是在进行跟踪和错误时,png 图像仍然显示浅背景颜色,它应该是 100% 透明且没有背景颜色。

这是我在进行跟踪和错误后的代码,但它仍然在 png 图像中显示浅色背景色。它应该是 100% 透明的。我尝试了很多人,但到目前为止还没有任何解决方案。

$img_h = imagesy($image);
$img_w = imagesx($image);

// create new image (canvas) of proper aspect ratio
$img = imagecreatetruecolor($canvas_w, $canvas_h);

$background = imagecolorallocate($img, 255, 255, 255);

imagefill($img, 0, 0, $background);

$xoffset = ($canvas_w - $img_w) / 2;
$yoffset = ($canvas_h - $img_h) / 2;

imagealphablending($img, true);
imagesavealpha($img, true);
imagecopyresampled($img, $image, $xoffset, $yoffset, 0, 0, $img_w, $img_h, $img_w, $img_h); 

我预计 png 图像将在白色背景上显示为透明。但是代码在 png 图像上添加了浅色背景色。图像预览 - 输出结果图像

标签: phpgdphp-gd

解决方案


推荐阅读