php Imagemagick jpg 黑色背景
我有一个 php 脚本来创建 pdf 的 jpg 缩略图,如下所示;
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("jpg");
$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)."' />";
?>
但生成的 jpg 有黑色背景而不是白色。我该如何解决这个问题?
I have a php script to create jpg thumbnail of pdf as follows;
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("jpg");
$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)."' />";
?>
But the resulting jpg have black background instead of white.. How can I fix this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
之前发布的答案都不适合我,但是下面的答案对我有用:
当我使用 Laravel 框架时,我会获取转换后的图像并使用 Laravel 文件系统存储它。
更新:所以我最近更改了操作系统,虽然这之前在我的 Mac 上的 Ubuntu 机器上工作,但某些图像仍然显示为黑色。
我必须将脚本更改为以下内容:
在读取图像之前设置背景颜色似乎很重要。我还展平任何可能的图层并删除 Alpha 通道。我觉得我在我的 Ubuntu 机器上尝试了
ALPHACHANNEL_REMOVE
但它不起作用,所以希望读者可以在这些答案之间找到适合他们的东西。None of the previously posted answers worked for me however the below did:
As I'm using the Laravel framework I then take the converted image and store it using Laravels filesystem.
Update: So I recently changed OS and whereas this previously worked on my Ubuntu machine on my Mac certain images were still coming out black.
I had to change the script to the below:
Seems important to set the background colour before reading in the image. I also flatten any possible layers and remove the alpha channel. I feel like I tried
ALPHACHANNEL_REMOVE
on my Ubuntu machine and it didn't work so hopefully between these answers readers can find something that works for them.如果您的 Imagick 版本不是最新的,setImageBackgroundColor 可能是错误的。
将以下行交换
为此(Imagick 版本 >= 2.1.0)
或(Imagick 版本 <2.1.0)
If your version of Imagick is not up to date, the setImageBackgroundColor may be wrong.
Swap the following line
to this (Imagick version >= 2.1.0)
or (Imagick version < 2.1.0)
我解决了它;
I solved it by;
只需添加此选项即可防止创建带有黑色背景的 JPG
Simply adding this prevents the JPG to be created with a black background
将此代码
$im->setimageformat("jpg");
更改为此代码$im->setimageformat("png");
如果您遇到背景颜色问题。change this code
$im->setimageformat("jpg");
to this code$im->setimageformat("png");
if you face a background colour issue.只需在创建新的 imagick() 后立即使用 flattenImages() 即可:
编辑: flattenImages 方法已被弃用 &已删除。使用
Just use flattenImages() right after creating a new imagick():
Edit: The flattenImages method has been deprecated & removed. Use
经过无数次尝试在 pdf 文件中附加 jpeg 图像而不出现黑色区域,我找到了解决方案:
按此顺序使用的函数 transformImageColorspace 效果很好:
After endless attempts to append a pdf file with a jpeg image without getting black areas, I found the solution: the function transformImageColorspace
Used in this order works perfectly:
我想补充一下,如果您的 PDF 有多个页面,则需要稍微不同的方法,以补充出色且有用的答案。
我惊讶地发现,
Imagick
类实现了Iterable
,这意味着您可以通过foreach
循环运行它并在每个页面上进行操作反过来(事实证明这是必要的,因为图层、颜色和 Alpha 通道的更改似乎只在最后一页上生效),它将作为单独的Imagick
对象呈现给您:I would like to add to the excellent and helpful answers by saying that a slightly different approach is required if your PDF has multiple pages.
Something I was surprised to discover is that the
Imagick
class implementsIterable
, which means you can run it through aforeach
loop and operate on each page in turn (which it turns out is necessary because the layer, colour, and alpha channel changes appear to only take effect on the last page) which will be presented to you as a separateImagick
object: