ImageMagick Convert PDF to low resolution JPG file

你。 提交于 2019-12-22 08:19:48

问题


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).

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?


回答1:


You need two things that CodeIgniter probably doesn't support, so you have to use ImageMagick directly.

First, you have to set the resolution of the PDF for a high-quality result. On the ImageMagick command line, this can be done with the -density option. With PHP imagick, use setResolution.

To get rid of the black background, you have to flatten the PDF on a white background first. On the command line, use the options -background white -flatten. With PHP imagick, setImageBackgroundColor and flattenImages should work.




回答2:


You can set quality and transparency of output picture in prefrences of 'image_lib' library. Please read http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html and look for 'quality', and 'wm_x_transp' options.




回答3:


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):

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

Then converting the jpgs to pngs (using ImageMagick):

mogrify -format jpg *.png


来源:https://stackoverflow.com/questions/21935436/imagemagick-convert-pdf-to-low-resolution-jpg-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!