Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 500
Khaled Avatar asked Mar 20 '23 12:03

Khaled


1 Answers

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.

like image 59
nwellnhof Avatar answered Apr 05 '23 23:04

nwellnhof