Convert PDF to Jpeg with PHP and Imagemagick

Convert PDF to JPEG with PHP and ImageMagick

It can be done using setResolution, but you need to do it before loading an image.
Try something like this:

// 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();

Php Imagick : Pdf are converted into JPG with Very bad quality

You need to put options for the image background color set white. For here I have added options of $im->->flattenImages();
Here you can find out the solutions https://www.binarytides.com/convert-pdf-image-imagemagick-php/

From

$pdfPath = $config['upload_path'] . '/' . $fileName;
$im = new imagick();
$im->setResolution(300, 300);
$im->readImage($pdfPath);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(100);
$im->writeImage($config['upload_path'] . '/' . str_replace('pdf', 'jpeg', $fileName));
$im->clear();
$im->destroy();

TO

$pdfPath = $config['upload_path'] . '/' . $fileName;
$im = new imagick();
$im->setResolution(300, 300);
$im->readImage($pdfPath);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(100);
// -flatten option, this is necessary for images with transparency, it will produce white background for transparent regions
$im = $im->flattenImages();
$im->writeImage($config['upload_path'] . '/' . str_replace('pdf', 'jpeg', $fileName));
$im->clear();
$im->destroy();

I am not sure it will be help for you or not.

Convert PDF to high quality JPG using PHP and ImageMagick

You need to set the resolution before reading the image in. Please see this comment on the manual - see if that will work.

ImageMagick with PHP text overflowing PDF to JPG conversion

Its very hard to say why you see the result you do, without seeing the original PDF file, rather than a picture of it.

The most likely explanation is that your original PDF file uses a font, but does not embed that font in the PDF. When Ghostscript comes to render it to an image it must then substitute 'something' in place of the missing font. If the metrics (eg spacing) of the substituted font do not match precisely the metrics of the missing font, then the rendered text will be misplaced/incorrectly sized. Of course since its not using the same font it also won't match the shapes of the characters either.

This can result in several different kinds of problems, but what you show is pretty typical of one such class of problem. Although you haven't mentioned it, I can also see several places in the document where text overwrites as well, which is another symptom of exactly the same problem.

If this is the case then the Ghostscript back channel transcript will have told you that it was unable to find a font and is substituting a named font for the missing one. I can't tell you if Imagemagick stores that anywhere, my guess would be it doesn't. However you can copy the command line from the ImagMagick profile.xml file and then use that to run Ghostscript yourself, and then you will be able to see if that's what is happening.

If this is what is happening then you must either;

  1. Create your PDF file with the fonts embedded (this is good practice anyway)
  2. Supply Ghostscript with a copy of the missing font as a substitute
  3. Live with the text as it is

PHP Imagick hangs the whole server when converting PDF to Image

php-vips is faster at converting pdfs to pngs, you could try that.

I made you a sample program:

#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';

use Jcupitt\Vips;

for ($i = 1; $i < count($argv); $i++) {
$image = Vips\Image::newFromFile($argv[$i]);
$n_pages = $image->get("n-pages");
echo($argv[$i] . " has " . $n_pages . " pages\n");

for ($n = 0; $n < $n_pages; $n++) {
echo(" rendering page " . $n . " ...\n");
$page = Vips\Image::newFromFile($argv[$i], [
"dpi" => 300,
"page" => $n,
# this enables image streaming
"access" => "sequential"
]);
$page->writeToFile($argv[$i] . "_page_" . $n . ".png");
}
}

I can run it like this:

$ /usr/bin/time -f %M:%e ../convert-vips.php nipguide.pdf 
nipguide.pdf has 58 pages
rendering page 0 ...
...
rendering page 57 ...
107808:31.72

So it made 58 pngs in 32 seconds and needed a max of 110mb of ram. It doesn't create any temporary files -- that 110mb covers everything.

png is a very slow file format. If you save as jpg instead, it takes about 6s for everything.

I tried a version of your imagick code:

#!/usr/bin/env php
<?php

for ($i = 1; $i < count($argv); $i++) {
$imagick = new Imagick();
$imagick->setResourceLimit(6, 1);
$imagick->setResolution(300, 300);
$imagick->readImage($argv[$i]);
$pages = $imagick->getNumberImages();
echo($argv[$i] . " has " . $pages . " pages\n");

for ($x = 0; $x < $pages; $x++) {
echo(" rendering page " . $x . " ...\n");
$imagick->readImage($argv[$i] . "[" . $x . "]");
$imagick->setImageFormat("png");
$imagick->writeImage($argv[$i] . "_page_" . $x . ".png");
}

$imagick->clear();
$imagick->destroy();
}

Running that I see:

$ /usr/bin/time -f %M:%e ../convert-imagick.php nipguide.pdf 
nipguide.pdf has 58 pages
rendering page 0 ...
...
rendering page 57 ...
255640:223.26

So 220s (almost 7x slower) and 260mb of memory. That memory use isn't the whole story -- at 300 DPI, imagick will make a 65mb file in /tmp for each page, so it needs about 5gb of storage in total.



Related Topics



Leave a reply



Submit