设置ImageMagick PNG图像背景颜色
我有一个 PHP 脚本来创建 PDF 文件的 PNG 缩略图,如下所示:
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";
?>
它返回缩略图,但背景是透明的。我想设置白色背景颜色(将 alpha 图层更改为白色)。我该怎么做?
I have a PHP script to create a PNG thumbnail of a PDF file as follows:
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";
?>
Which returns a thumbnail, but the background is transparent. I want to set white background color (change the alpha layer to white). How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案不在于背景颜色,而在于 Alpha 通道!
尝试使用以下代码:
The solution is not in the background colour, but in the alpha channel!
Try with this code:
我正在做类似的事情,尽管我实际上是将图像写入磁盘 -
当我使用你的直接输出时,它起作用了,我从 PDF 中得到了实际的颜色。
经过一番调试,我发现问题实际上与
功能有关。
无论出于何种原因,当您设置所有颜色、压缩等时,resizeImage 都会添加黑色背景。
我的解决方案是使用 GD 来调整大小,这样我就可以进行完整的动态调整大小 - 因为你对这样的事情不感兴趣,所以我会简单地使用图像采样功能。你的代码应该是这样的:
I'm doing something similar, although I'm actually writing the image to the disk -
When I used your direct output, it worked and I got the actual color from the PDF.
Through a bit of debugging, I figured that the issue was actually related to the
function.
For whatever reason, when you set all of your colors, compression, etc. the resizeImage adds the black background.
My solution is to use GD for the resizing, so that I can have a full dynamic resize - Since you're not interested in such thing, I would simply use the image sampling functionality. Your code should be like this: