使用 PHP 实现 PNG 透明度

发布于 2024-07-09 11:33:28 字数 551 浏览 8 评论 0原文

嘿,当我从中创建缩略图时,试图保持 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);

Hey having some trouble trying to maintain transparency on a png when i create a thumbnail from it, anyone any experience with this? any help would be great, here's what i am currently doing:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随梦而飞# 2024-07-16 11:33:28

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

$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() 好得多

I have had success doing it like this in the past:

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

I found the output image quality much better using imagecopyresampled() than imagecopyresized()

寄居者 2024-07-16 11:33:28

忘记颜色透明度指数吧,它在所有渲染产品中都不起作用。 而是使用 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);

Forget the color transparency index, it never works in all rendering products. Instead use an alpha layer mask:

$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);
淡莣 2024-07-16 11:33:28

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

Those functions access the underlying gdlib library, which is a fine toy, but not something that makes for nice results. If you have the option, use imagemagick instead. The downside is that there are currently no good php-bindings, so you need to access it over the shell, which you're usually not allowed on shared hosts.

微凉 2024-07-16 11:33:28

请参阅 dycey 对“如何调整大小... ”。 本质上,在执行任何其他操作之前,您需要用透明度填充整个背景。

See dycey's answer to "How do I resize...". Essentially, you need to fill the entire background with transparency before you do any other operations.

我爱人 2024-07-16 11:33:28

imagecopyresized 不正确支持透明度。

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

解决方案? 您可能最终会手动调整该东西的大小。

imagecopyresized does not support transparency properly.

imagecopymerge does, but it doesn't resize.

The solution? You'd probably end up resizing the thing manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文