Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP实现PNG透明_Php_Png_Transparency - Fatal编程技术网

用PHP实现PNG透明

用PHP实现PNG透明,php,png,transparency,Php,Png,Transparency,嘿,当我从png创建缩略图时,在保持png的透明度方面遇到了一些麻烦,有没有人有过这样的经验?任何帮助都会很好,以下是我目前正在做的: $fileName= "../js/ajaxupload/tees/".$fileName; list($width, $height) = getimagesize($fileName); $newwidth = 257; $newheight = 197; $thumb = imagecreatetruecolor($newwidth, $newhei

嘿,当我从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()

输出图像质量要好得多。这些函数访问底层的gdlib库,这是一个很好的工具,但并不能获得很好的结果。如果您有此选项,请改用。缺点是目前没有好的php绑定,因此您需要通过shell访问它,这在共享主机上通常是不允许的。

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

真是太棒了!非常感谢!使用imagecopyresampled最终对我有效,因此它不仅提高了质量,而且imagecopyresized无论如何都不会尊重透明度。这一确切的代码对我不起作用,而是更改imagealphablending($thumb,false);要创建图像,请单击($thumb,true);在第二行修正了,太棒了!浪费了一个多小时来解决这个问题,而你的解决方案是唯一有效的!比以往任何时候都完美!。你是伟大的人@user629089。非常感谢。您能解释一下为什么使用
220、220、220
imagecopyresampled
是一个更好的解决方案,尤其是对于用户提交的图片,在上传之前无法手动调整大小;我觉得这个答案不是很有帮助。你不需要用透明度填充整个背景,但是GD不擅长处理不透明度,除非它是100%(完全不透明)或0%(完全透明)