Home >Backend Development >PHP Tutorial >How to Convert PDF to JPEG with High Quality and Original Size Using PHP and ImageMagick?

How to Convert PDF to JPEG with High Quality and Original Size Using PHP and ImageMagick?

Barbara Streisand
Barbara StreisandOriginal
2024-10-29 02:52:02690browse

How to Convert PDF to JPEG with High Quality and Original Size Using PHP and ImageMagick?

Using PHP and ImageMagick to Convert PDF to JPEG with High Quality

If you're attempting to convert a PDF file to JPEG using PHP and ImageMagick, but encountering poor quality, this article will guide you through resolving this issue. Additionally, we'll address how to maintain the original size of the PDF during conversion.

Resolving Poor Image Quality

To improve the quality of the converted JPEG, you need to adjust the compression settings of the ImageMagick object. The setCompressionQuality() method accepts a value between 0 and 100, where 100 represents the highest quality. In your script, increase the value of this parameter to a higher number, such as:

<code class="php">$im->setCompressionQuality(100);</code>

Maintaining Original PDF Size

To prevent the cropping of the JPEG image, you should set the resolution before loading the PDF into the ImageMagick object. Modify your script as follows:

<code class="php">// Instantiate Imagick
$im = new Imagick();

$im->setResolution(300, 300);
$im->readimage('document.pdf[0]');
$im->setImageFormat('jpeg');
$im->writeImage('thumb.jpg');
$im->clear();
$im->destroy();</code>

By making these adjustments, you can now convert PDF files to JPEG with both high quality and original size preservation.

The above is the detailed content of How to Convert PDF to JPEG with High Quality and Original Size Using PHP and ImageMagick?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn