4

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?

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Khaled
  • 8,255
  • 11
  • 35
  • 56
  • Have you looked at "-transparent-color" to specify white as transparent color for example? The default transparent color for ImageMagick is black according to the documentation. – David van Driessche Feb 24 '14 at 11:08
  • And have you looked at this for text quality issues? http://stackoverflow.com/questions/15769623/imagemagick-convert-pdf-to-jpeg-has-poor-text-quality-after-upgrading-imagemagic – David van Driessche Feb 24 '14 at 11:14

3 Answers3

4

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.

Community
  • 1
  • 1
nwellnhof
  • 32,319
  • 7
  • 89
  • 113
  • Also note that the `-density` option has to come before the filenames. So `convert -density 200 input.pdf output.jpg` – Supernormal Jan 15 '23 at 20:21
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.

Robert Trzebiński
  • 1,327
  • 8
  • 16
  • As in the code, im using quality of 90% which is nearly perfect, images on the pdf looks totally good, but text looks totally unreadable. – Khaled Feb 24 '14 at 04:18
  • 1
    Actually this is incorrect, `wm_x_transp` is for *watermark*, not for the image – Jakub Feb 24 '14 at 05:31
1

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
hemisphire
  • 1,205
  • 9
  • 19