php Imagemagick jpg 黑色背景

发布于 2024-11-08 20:23:45 字数 444 浏览 0 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(8

总以为 2024-11-15 20:23:45

之前发布的答案都不适合我,但是下面的答案对我有用:

$image = new Imagick;

$image->setResolution(300, 300);

$image->readImage("{$originalPath}[0]");

$image->setImageFormat('jpg');

$image->scaleImage(500, 500, true);

$image->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);

当我使用 Laravel 框架时,我会获取转换后的图像并使用 Laravel 文件系统存储它。

Storage::put($storePath, $image->getImageBlob());

更新:所以我最近更改了操作系统,虽然这之前在我的 Mac 上的 Ubuntu 机器上工作,但某些图像仍然显示为黑色。

我必须将脚本更改为以下内容:

$image = new Imagick;

$image->setResolution(300, 300);

$image->setBackgroundColor('white');

$image->readImage("{$originalPath}[0]");

$image->setImageFormat('jpg');

$image->scaleImage(500, 500, true);

$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);

在读取图像之前设置背景颜色似乎很重要。我还展平任何可能的图层并删除 Alpha 通道。我觉得我在我的 Ubuntu 机器上尝试了 ALPHACHANNEL_REMOVE 但它不起作用,所以希望读者可以在这些答案之间找到适合他们的东西。

None of the previously posted answers worked for me however the below did:

$image = new Imagick;

$image->setResolution(300, 300);

$image->readImage("{$originalPath}[0]");

$image->setImageFormat('jpg');

$image->scaleImage(500, 500, true);

$image->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);

As I'm using the Laravel framework I then take the converted image and store it using Laravels filesystem.

Storage::put($storePath, $image->getImageBlob());

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:

$image = new Imagick;

$image->setResolution(300, 300);

$image->setBackgroundColor('white');

$image->readImage("{$originalPath}[0]");

$image->setImageFormat('jpg');

$image->scaleImage(500, 500, true);

$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);

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.

哥,最终变帅啦 2024-11-15 20:23:45

如果您的 Imagick 版本不是最新的,setImageBackgroundColor 可能是错误的。

将以下行交换

$im->setImageBackgroundColor("red");

为此(Imagick 版本 >= 2.1.0)

$im->setBackgroundColor(new ImagickPixel("red"));

或(Imagick 版本 <2.1.0)

$im->setBackgroundColor("red");

If your version of Imagick is not up to date, the setImageBackgroundColor may be wrong.

Swap the following line

$im->setImageBackgroundColor("red");

to this (Imagick version >= 2.1.0)

$im->setBackgroundColor(new ImagickPixel("red"));

or (Imagick version < 2.1.0)

$im->setBackgroundColor("red");
风流物 2024-11-15 20:23:45

我解决了它;

$im = new imagick(realpath($file).'[0]');
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->writeImage("imagename.jpg");

I solved it by;

$im = new imagick(realpath($file).'[0]');
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->writeImage("imagename.jpg");
拧巴小姐 2024-11-15 20:23:45

只需添加此选项即可防止创建带有黑色背景的 JPG

-alpha off

Simply adding this prevents the JPG to be created with a black background

-alpha off
要走干脆点 2024-11-15 20:23:45

将此代码 $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.

北陌 2024-11-15 20:23:45

只需在创建新的 imagick() 后立即使用 flattenImages() 即可:

$im = new Imagick('file.pdf[0]');
$im = $im->flattenImages();

编辑: flattenImages 方法已被弃用 &已删除。使用

$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );

Just use flattenImages() right after creating a new imagick():

$im = new Imagick('file.pdf[0]');
$im = $im->flattenImages();

Edit: The flattenImages method has been deprecated & removed. Use

$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );
南城旧梦 2024-11-15 20:23:45

经过无数次尝试在 pdf 文件中附加 jpeg 图像而不出现黑色区域​​,我找到了解决方案:

按此顺序使用的函数 transformImageColorspace 效果很好:

$im = new Imagick();
$im->readImage("file.pdf");

$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);

$im->setImageFormat('jpeg');
$im->writeImage('image.jpg');

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:

$im = new Imagick();
$im->readImage("file.pdf");

$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);

$im->setImageFormat('jpeg');
$im->writeImage('image.jpg');
度的依靠╰つ 2024-11-15 20:23:45

我想补充一下,如果您的 PDF 有多个页面,则需要稍微不同的方法,以补充出色且有用的答案。

我惊讶地发现,Imagick 类实现了 Iterable,这意味着您可以通过 foreach 循环运行它并在每个页面上进行操作反过来(事实证明这是必要的,因为图层、颜色和 Alpha 通道的更改似乎只在最后一页上生效),它将作为单独的 Imagick 对象呈现给您:

$im = new Imagick('path/to/file.pdf');
foreach ($im as $c => $page)
{
    // do what you need to do with each page and then...
    $im->writeImage('path/to/image-'.$c.'.jpg');
}

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 implements Iterable, which means you can run it through a foreach 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 separate Imagick object:

$im = new Imagick('path/to/file.pdf');
foreach ($im as $c => $page)
{
    // do what you need to do with each page and then...
    $im->writeImage('path/to/image-'.$c.'.jpg');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文