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
Release: 2024-10-29 02:52:02
Original
695 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template