Convert pdf to image with imagemagick in php

Imagick

In a previous article we saw how to use imagemagick to convert pdfs to image to create a snapshot or thumbnail of the pdf. Now we are going to do the same in php using the Imagick class which provides the bindings for imagemagick library inside php. The library is available in the form of a class Imagick that does all the job of imagemagick utility.

Install

On ubuntu install the packages from the commandline as follows

$ sudo apt-get install imagemagick php5-imagick

Php Code


$pdf_file   = 'demo.pdf';
$save_to    = 'demo.jpg';

$img = new imagick($pdf_file);

//set new format
$img->setImageFormat('jpg');

//save image file
$img->writeImage($save_to);

It will create the jpg image file from the pdf file. The image would contain the last page of the pdf file.

To convert a specific page from the pdf use the following syntax

$img = new imagick($pdf_file.'[0]');

The number in the bracket indicates the page number starting with 0.

Scale down the image

The image can be scaled down using the scaleImage method.

$pdf_file   = 'demo.pdf';
$save_to    = 'demo.jpg';

$img = new imagick($pdf_file.'[1]');

//reduce the dimensions - scaling will lead to black color in transparent regions
$img->scaleImage(800,0);

//set new format
$img->setImageFormat('jpg');

//save image file
$img->writeImages($save_to, false);

The first parameter of the scaleImage function specifies the height in pixels. The second parameter is left 0 so that imagemagick calculates the correct value for it to preserve the aspect ratio.

There is however an issue when scaling images down. If the pdf has transparency then the transparent regions would turn black. This is fixed by flattening the image as follows


$pdf_file   = 'demo.pdf';
$save_to    = 'demo.jpg';

$img = new imagick($pdf_file.'[1]');

//reduce the dimensions - scaling will lead to black color in transparent regions
$img->scaleImage(800,0);

//set new format
$img->setImageFormat('jpg');

// -flatten option, this is necessary for images with transparency, it will produce white background for transparent regions
$img = $img->flattenImages();

//save image file
$img->writeImages($save_to, false);

Better resolution

The images created in the above examples would have text that is not very sharp or clear. The clarity is improved by setting a higher resolution. The resolution has to be set before opening the image.








$pdf_file   = 'demo.pdf';
$save_to    = 'demo.jpg';

$img = new imagick();

//this must be called before reading the image, otherwise has no effect - "-density {$x_resolution}x{$y_resolution}"
//this is important to give good quality output, otherwise text might be unclear
$img->setResolution(200,200);

//read the pdf
$img->readImage("{$pdf_file}[0]");

//reduce the dimensions - scaling will lead to black color in transparent regions
$img->scaleImage(800,0);

//set new format
$img->setImageFormat('jpg');

// -flatten option, this is necessary for images with transparency, it will produce white background for transparent regions
$img = $img->flattenImages();

//save image file
$img->writeImages($save_to, false);

Output to browser directly

To output the image to the browser directly replace the writeImages line with the following


//echo the jpg image
header("Content-type: image/".$img->getImageFormat()); 
echo $img;	//same as echo $img->getImageBlob();

The imagick object can be directly echoed which would echo the image content.

Using convert utility

The convert utility can also be used directly instead of the Imagick class. Then it would need to execute shell function through either of the functions shell_exec, system, exec or passthru.

Conclusion

Lots of things can be done with pdf to image feature. For example make an online pdf reader, that convert each page to image for reading. Or create thumbnails, previews of pdfs

Last Updated On : 15th November 2012

Subscribe to get updates delivered to your inbox

5 Comments + Add Comment

  • Very useful post.
    Thanks

  • I have following error

    Fatal error: Uncaught exception ‘ImagickException’ with message ‘Postscript delegate failed `D:/Xmapp/xampp/htdocs/test1/pdf.pdf’: No such file or directory @ error/pdf.c/ReadPDFImage/645′ in D:Xmappxampphtdocstest1index.php:16 Stack trace: #0 D:Xmappxampphtdocstest1index.php(16): Imagick->__construct(‘D:/Xmapp/xampp/…’) #1 {main} thrown in D:Xmappxampphtdocstest1index.php on line 16

    Please help.

  • Use the following code to save the pdf page by page:

    $pdf_file = $_SERVER[‘DOCUMENT_ROOT’].’/201307301103041375192984.pdf’;

    $img = new imagick($_SERVER[‘DOCUMENT_ROOT’].’/201307301103041375192984.pdf’);

    //this must be called before reading the image, otherwise has no effect – “-density {$x_resolution}x{$y_resolution}”

    //this is important to give good quality output, otherwise text might be unclear

    $img->setResolution(200,200);

    $number = $img->getnumberimages();

    for($i=0;$ireadImage(“{$pdf_file}[“.$i.”]”);

    $img->scaleImage(800,0);

    //set new format

    $img->setImageFormat(‘jpg’);

    // -flatten option, this is necessary for images with transparency, it will produce white background for transparent regions

    $img = $img->flattenImages();

    //save image file

    $img->writeImages($_SERVER[‘DOCUMENT_ROOT’].’/demo(‘.$i.’).jpg’, false);

    }

  • Great script helps a lot…..

  • The above code is worked fine, but only I can view a single page.
    I want to view multiple pages.
    Please give me a solution for this.

    Thanks in Advance.

Leave a comment