php imagick 将 PNG 转换为 jpg
$image = "[...]"; //binary string containing PNG image
$file = fopen('image.tmp', 'wb');
fputs($file, $image);
fclose($file);
$image = new Imagick('PNG:image.tmp');
$image->thumbnailImage($width, $height);
$image->setImageFormat('jpg');
$image->setCompressionQuality(97);
$image->writeImage('image.jpg');
以上不起作用,并为我提供了 此图像。当这样做时,
[...]
$image->setImageFormat('png');
$image->setCompressionQuality(97);
$image->writeImage('image.png');
一切都会恢复正常。我认为它必须用透明背景做一些事情,而 JPG 格式不提供这种功能。任何人都可以帮助解决这个问题(imagick 没有很好的记录,所以我不知道如何帮助自己)。
$image = "[...]"; //binary string containing PNG image
$file = fopen('image.tmp', 'wb');
fputs($file, $image);
fclose($file);
$image = new Imagick('PNG:image.tmp');
$image->thumbnailImage($width, $height);
$image->setImageFormat('jpg');
$image->setCompressionQuality(97);
$image->writeImage('image.jpg');
The above doesn't work and gives me a black image for this image. When doing instead
[...]
$image->setImageFormat('png');
$image->setCompressionQuality(97);
$image->writeImage('image.png');
all is fine again. I think it has to do something with transparent background, which isn't available in JPG format. Can anyone help to solve this (imagick isn't documented very well, so I don't know how to help myself).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
找到了解决办法:
Found a solution:
另一种将透明png转换为jpg的方法,如Imagick::flattenImages:
随着时间的推移,
flattenImages()
已被弃用。使用以下代码代替上面的行:
Another way to convert transparent png to jpg, as mentioned in Imagick::flattenImages:
As time moves on,
flattenImages()
has been deprecated.Instead of the line above use:
您可以使用
setBackgroundColor
来将默认背景颜色设置为黑色以外的其他颜色。保存为 JPG 时,PNG 透明度将被背景颜色替换。编辑:像这样使用它:
You can use
setBackgroundColor
to set the default background color to something else than black. The PNG transparency will be replaced by the background color when saving to JPG.Edit: Use it like so:
尝试在 < 之后添加
$image->setBackgroundColor(0xFFFFFF);
代码>$image = new Imagick('PNG:image.tmp');Try adding
$image->setBackgroundColor(0xFFFFFF);
after$image = new Imagick('PNG:image.tmp');