使用 PHP 实现 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);
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我过去曾成功地这样做过:
我发现使用
imagecopyresampled()
的输出图像质量比imagecopyresized()
好得多I have had success doing it like this in the past:
I found the output image quality much better using
imagecopyresampled()
thanimagecopyresized()
忘记颜色透明度指数吧,它在所有渲染产品中都不起作用。 而是使用 alpha 图层蒙版:
Forget the color transparency index, it never works in all rendering products. Instead use an alpha layer mask:
这些函数访问底层的 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.
请参阅 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.
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.