PNG 透明度与 PHP

2022-08-30 11:38:35

嘿,当我从中创建缩略图时,尝试在png上保持透明度时遇到了一些麻烦,有人对此有任何经验吗?任何帮助都会很棒,这是我目前正在做的事情:

$fileName= "../js/ajaxupload/tees/".$fileName;

list($width, $height) = getimagesize($fileName);

$newwidth = 257;
$newheight = 197;

$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagesavealpha($thumb, true);
imagepng($thumb,$newFilename);

答案 1

我过去曾成功地做到这样:

$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);  

$source = imagecreatefrompng($fileName);
imagealphablending($source, true);

imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagepng($thumb,$newFilename);

我发现输出图像质量比使用好得多imagecopyresampled()imagecopyresized()


答案 2

忘记颜色透明度指数,它永远不会在所有渲染产品中工作。请改用 Alpha 图层蒙版:

$image = imagecreatetruecolor($size, $size);

imagealphablending($image, false);
imagesavealpha($image, true);

$trans_layer_overlay = imagecolorallocatealpha($image, 220, 220, 220, 127);
imagefill($image, 0, 0, $trans_layer_overlay);

推荐