dqzd92796 2014-11-07 02:29
浏览 619

使用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.

  • 写回答

1条回答

  • duanping1632 2014-11-07 15:17
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多