当我从png创建缩略图时,试图保持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);
有帮助吗?

解决方案

过去我成功地做到了这一点:

$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()

更好地找到输出图像质量

其他提示

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

imagecopyresized 不能正确支持透明度。

imagecopymerge 可以,但不会调整大小。

解决方案?你可能最终会手动调整大小。

这些函数访问底层的gdlib库,这是一个很好的玩具,但不是一个可以产生好结果的东西。如果您有此选项,请改用 imagemagick 。缺点是目前没有好的php绑定,所以你需要通过shell访问它,通常不允许在共享主机上使用它。

请参阅 dycey对<!>的回答;如何调整大小...... <!>“; 。基本上,在进行任何其他操作之前,您需要使用透明度填充整个背景。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top