How to Convert PDF to JPEG Image in PHP

yotube
0

Hi! Today let's see how to convert pdf to jpeg in php using imagick. PHP offers some good native extensions for image processing. Imagick is one of those extensions with which we can easily create JPEG images from pdf without the need for third-party tools.

The library does not need installation since it comes built-in with PHP. You just have to instantiate the class and use it. Plus it provides several customization options to create images. I suggest you to refer the official documentation for the complete list of functions.

php convert pdf to jpeg

PHP - Convert PDF File to JPEG:

Imagick is the native php extension to create and process images using the ImageMagick API. To covert the pdf into jpeg with imagick, you must first read the image from the pdf file and then write it into an image. The following example converts all the pages in a pdf file into jpeg images.


<?php
$imagick = new Imagick();
$imagick->readImage('mypdf.pdf');
$imagick->writeImages('myimage.jpg', false);
?>

In the above snippet, we used two functions,

  • Function readImage(), to read the image from the given file.
  • Function writeImages(), to write an image or image sequence to the file. We have set the second parameter as 'false', which makes the pages to be split into a separate image file. For example, if there are 3 pages in the pdf, they will be saved as myimage-0.jpg, myimage-1.jpg and myimage-2.jpg.

For Better Quality:

To obtain better image quality, use the setResolution() method before reading the image.


$imagick->setResolution(150, 150);

Converting Specific Page to JPEG:

In case you want to convert only a particular page in the pdf file, use the page number after the file name like this, mypdf.pdf[0]. Please remember, the pages start from zero. It will just convert the first page of the pdf into image. Here's the example.


<?php
$imagick = new Imagick();
$imagick->readImage('mypdf.pdf[0]');
$imagick->writeImages('page_one.jpg');
?>
Read Also:

It is really easy to convert pdf to jpeg using the imagick extension. It offers a wide range of functions to create, edit and process images and support formats such as jpg, png, etc. I hope this tutorial is useful to you. Please share it on social media if you like it.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top