Convert pdf to image php imagick

Hey, Today I would like to show you how we can convert PDF to JPEG using imagick extension. Imagick is a native php extension to create and modify images using the ImageMagick API, which is mostly built-in in PHP installation so no need to include any thing. ImageMagick software suite allow us to create, read, edit, and compose bitmap images easily.

PHP – Convert all PDF pages to JPEG

Using following simple example you can convert all pages of PDF to JPEG images.

readImage('myfile.pdf');
 // Writes an image or image sequence Example- converted-0.jpg, converted-1.jpg
 $imagick->writeImages('converted.jpg', false);
?> 

As you are seeing, you have to pass a PDF file and it will produce JPEG files for each page of your given PDF file as output. writeImages() function second parameter is false, so it will not join the images, means it will produce image sequence(create images for each page) Example – converted-0.jpg, converted-1.jpg.

Convert pdf to image php imagick

PHP – Convert specific PDF page to JPEG

If you want to convert specific page for example first page of your PDF file only then define PDF file name like this myfile.pdf[0] and run the script it will show convert only first page of your PDF file. Following is the example –

readImage('test.pdf[0]');
 // Writes an image
 $imagick->writeImages('converted_page_one.jpg');
?> 

PHP – Convert specific PDF page to JPEG with quality

If you need better quality, try adding $imagick->setResolution(150, 150); before reading the file. setResolution() must be called before loading or creating an image.

setResolution(150, 150);
 // Reads image from PDF
  $imagick->readImage('test.pdf[0]');
 // Writes an image
 $imagick->writeImages('converted_page_one.jpg');
?> 

If you experience transparency problems when converting PDF to JPEG (black background), try flattening your file:

setResolution(150, 150);
 // Reads image from PDF
  $imagick->readImage('myFile.pdf[0]');
  // Merges a sequence of images
  $imagick = $imagick->flattenImages(); 
 // Writes an image
 $imagick->writeImages('converted_page_one.jpg');
?> 

shared hosting – Convert a PDF to JPEG using PHP

Most of the shared hosting providers do not compile imagick extension with PHP, but imagick binaries will be available, so here is the code to convert PDF to JPEG with imagick binaries.

// source PDF file
$source = "myFile.pdf";
// output file
$target = "converted.jpg";
// create a command string 

exec('/usr/local/bin/convert "'.$source .'" -colorspace RGB -resize 800 "'.$target.'"', $output, $response);

echo $response ? "PDF converted to JPEG!!" : 'PDF to JPEG Conversion failed';
?>

You have to change binaries location (/usr/local/bin/convert) to your server location which you can get from your hosting admin.

Lots of things can be done with Imagick extension, explore more about it at – http://php.net/manual/en/book.imagick.php

Contents

Previously, I have talked about how to convert PDF to images using pdftoppm here. In this post, I want to share how to accomplish this task with Imagemagick.

Imagemagick provides the convert tool that can be used to do various complicated image processing tasks.

Convert All Pages of PDF File to Images

Use convert to convert PDF pages to images with the following command:

convert -density 150 presentation.pdf -quality 90 output-%3d.jpg

In the above command, we convert all the pages of the PDF files to images. -density is used to specify the DPI of the output images. For PDF files, it must be the first option since PDF files has no notion of DPI. This option must be used first so that convert know how to sample the PDF pages. -quality specify the quality for the generated images. %3d is used to specify the format for generated image names. The generated images will be named output-001.jpg, output-002.jpg ……

Convert Single Page of PDF File to Image

To convert a single page of PDF to image, use the following command:

convert -density 150 presentation.pdf[0] -quality 90 test.jpg

The number inside the bracket is used to select a page. Note that the page index starts at 0 instead of 1.

To resize the converted image, you can supply the -resize option:

convert -density 150 presentation.pdf[0] -quality 90 -resize 50% test.jpg

Convert A Range of PDF Pages to Images

You can also specify a range of pages with the following command:

# convert from page 0 to page 5
convert -density 150 presentation.pdf[0-5] -quality 90 test.jpg

References

  • https://aleksandarjakovljevic.com/convert-pdf-images-using-imagemagick/
  • https://askubuntu.com/questions/50170/how-to-convert-pdf-to-image
  • https://stackoverflow.com/questions/6605006/convert-pdf-to-image-with-high-resolution
  • Convert multi-page PDFs to multiple images.
  • Imagemagick: command line options.
  • Imagemagick: command line processing.

Author jdhao

LastMod 2020-02-27

License CC BY-NC-ND 4.0

Reward

Convert pdf to image php imagick
wechat
Convert pdf to image php imagick
alipay