简体   繁体   English

ImageMagick将PDF转换为低分辨率JPG文件

[英]ImageMagick Convert PDF to low resolution JPG file

I have been trying to convert PDF to JPG images using ImageMagick on CodeIgniter, but the produced image is in low quality and always having black background for some reason (while PDF isn't). 我一直在尝试使用CodeIgniter上的ImageMagick将PDF转换为JPG图像,但生成的图像质量低,并且由于某种原因总是具有黑色背景(而PDF不是)。

The code I'm using 我正在使用的代码

public function converter($pdf){
    $this->load->library('image_lib');

    $config = array(
        'image_library' => 'imagemagick',
        'library_path' => '/usr/bin/convert',
        'source_image' => "./pdf/".$pdf,
        'new_image' => "./images/a.jpg",
        'maintain_ratio' => true,
        'width' => 980,
        'quality' => '90%',
       );

       $this->image_lib->initialize( $config );

       if ( $this->image_lib->resize( ) ) {
        $this->image_lib->clear( );
       }
}

Anybody have any idea about what does seem to be wrong here? 有人知道这里看起来有什么不对吗?

You need two things that CodeIgniter probably doesn't support, so you have to use ImageMagick directly. 你需要CodeIgniter可能不支持的两件事,所以你必须直接使用ImageMagick。

First, you have to set the resolution of the PDF for a high-quality result. 首先,您必须设置PDF的分辨率以获得高质量的结果。 On the ImageMagick command line, this can be done with the -density option. 在ImageMagick命令行上,可以使用-density选项完成此操作。 With PHP imagick, use setResolution . 使用PHP imagick,使用setResolution

To get rid of the black background, you have to flatten the PDF on a white background first. 要摆脱黑色背景,首先必须在白色背景上展平PDF。 On the command line, use the options -background white -flatten . 在命令行上,使用选项-background white -flatten With PHP imagick, setImageBackgroundColor and flattenImages should work. 使用PHP imagick, setImageBackgroundColorflattenImages应该可以工作。

You can set quality and transparency of output picture in prefrences of 'image_lib' library. 您可以在'image_lib'库的优先级中设置输出图片的质量和透明度。 Please read http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html and look for 'quality', and 'wm_x_transp' options. 请阅读http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html并查找“quality”和“wm_x_transp”选项。

I've run into a similar problem, which I solved for myself by calling GhostScript to create a png file (the jpg created wasn't high enough quality): 我遇到了类似的问题,我通过调用GhostScript来创建一个png文件(创建的jpg质量不够高)为自己解决了这个问题:

"gswin64c -r150 -dNOPAUSE -dBATCH -sDEVICE#pngalpha -sOutputFile=" + strTitle + "-%%02d.png " + strTitle + ".pdf"

Then converting the jpgs to pngs (using ImageMagick): 然后将jpgs转换为pngs(使用ImageMagick):

mogrify -format jpg *.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM