PNG透明度与PHP

Bas*_*nce 43 php png transparency

当我从它创建缩略图时,我在尝试保持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);
Run Code Online (Sandbox Code Playgroud)

Tom*_*igh 85

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

$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);
Run Code Online (Sandbox Code Playgroud)

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


小智 14

忘记颜色透明度索引,它永远不会适用于所有渲染产品.而是使用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);
Run Code Online (Sandbox Code Playgroud)

  • 你能解释为什么用'220,220,220`? (3认同)