使用Imagick php扩展将多页PDF转换为JPG时的黑色背景

What is the best way to correct black background when converting multi page PDF to JPG with Imagick php extension?

Following is the code used on my application:

    $imagick = new Imagick($file);
    $imagick->setResolution(150,150);
    $imagick->setImageFormat("jpg");
    $imagick->setImageCompression(imagick::COMPRESSION_JPEG);
    $imagick->setImageCompressionQuality(70);
    foreach ($imagick as $c => $_page) {
        $_page->setImageBackgroundColor('white');
        $_page->adaptiveResizeImage($maxsize,$maxsize,true);
        $_page->writeImage("$file-$c.jpg");
    }

I'am aware that the flattenImage method can be used to remove black background, such as in:

    $imagick = $imagick->flattenImages();

But when the file has more the one pages, the flattenImages method puts all the pages on the same image, and therefore the result is a copy of the last page in all the JPGs generated.

I appreciate if anybody can help me.

doubao12345
doubao12345 呃,没有压力......但如果它解决了你的问题,那么点击那个接受按钮怎么样?;-)
接近 6 年之前 回复
donglu2008
donglu2008 当然,这是一个例子:faceo.com.br/temp/Manual%20Split%20hiwall%20YORK-1.pdf,以及生成的jpgs之一:faceo.com.br/temp/Manual%20Split%20hiwall%20YORK-1.PDF-10.JPG
接近 6 年之前 回复
dplm47571
dplm47571 您是否可以发布示例PDF?
接近 6 年之前 回复

1个回答

Working code first - explanation to follow:

This code works, but is incredibly slow:

$file = "./YORK.pdf";

$maxsize = 500;

$imagick = new Imagick($file);
$imagick->setResolution(150,150);
$imagick->setImageFormat("jpg");
$imagick->setImageCompression(imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality(70);

foreach ($imagick as $c => $_page) {
    $_page->setImageBackgroundColor('white');
    $_page->adaptiveResizeImage($maxsize,$maxsize,true);
    $_page->setImageCompose(\Imagick::COMPOSITE_ATOP);
    $_page->flattenImages();
    $_page->writeImage("$file-$c-compose.jpg");
}

This code works and is fast:

foreach ($imagick as $c => $_page) {
    $_page->setImageBackgroundColor('white');
    $_page->adaptiveResizeImage($maxsize,$maxsize,true);
    $blankPage = new \Imagick();
    $blankPage->newPseudoImage($_page->getImageWidth(), $_page->getImageHeight(), "canvas:white");
    $blankPage->compositeImage($_page, \Imagick::COMPOSITE_ATOP, 0, 0);
    $blankPage->writeImage("$file-$c.jpg");
}

What I think is happening is that when it comes to write the image ImageMagick is doing:

  • Convert the individual layers to JPG
  • Merge them on top of each other.

For each of the layers that has transparency because JPG doesn't support transparency it is rendering the transparency as black and then merging it. The code above makes the compositing be done in the correct order.

An alternative way to fix the problem is to put the output as PNG. As it supports transparency, the individual layers with transparency are merged correctly, and then you could convert the final image to JPG if you really wanted to.

Using PNG as the intermediate format may also produce a slightly higher quality output, as it may skip a 'save to JPG and decode' step. I do recommend using PNG in your workflow wherever possible, and then converting to JPG only when you serve a file to an end-user if you really need that extra bit of compression.

dongpangfu6322
dongpangfu6322 我仍然遇到问题,页面上出现黑色背景(对我而言,它是带有文字+图像的最后一页)。 用Imagick :: COMPOSITE_OVER替换Imagick :: COMPOSITE_ATOP似乎正在修复它
5 年多之前 回复
donglang8008
donglang8008 Tks man,很好的解决方案,将图像放在新的白色画布上!
接近 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐