ImageMagick Converts PDF to Low Resolution JPG File

I am trying to convert PDF to JPG using ImageMagick on CodeIgniter, but the generated image is of poor quality and always has a black background for some reason (while PDF is not).

The code I use

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( ); } } 

Does anyone have an idea of โ€‹โ€‹what seems wrong here?

+3
source share
3 answers

You need two things that CodeIgniter probably does not support, so you need to use ImageMagick directly.

First, you must set the PDF resolution to get a quality result. On the ImageMagick command line, this can be done using the -density option. Using PHP imagick use setResolution .

To get rid of the black background, you first need to collapse the PDF on a white background. At the command prompt, use the options -background white -flatten . With php, imagick, setImageBackgroundColor and flattenImages should work.

+2
source

You can set the quality and transparency of the output image in the preferences of the "image_lib" library. Please read http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html and find the quality and wm_x_transp options.

+2
source

I had a similar problem that I solved for myself by calling GhostScript to create a png file (the jpg created was not high enough):

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

Then convert jpgs to png (using ImageMagick):

 mogrify -format jpg *.png 
0
source

Source: https://habr.com/ru/post/1240980/


All Articles