1

I am using Imagick extension in my one of project. This is new for me.

The following is my code.

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

This gives me very poor quality in image. All text are converted into black background. Images are also not showing proper. See below image, which is converted from PDF. enter image description here Please help me.

fmw42
  • 46,825
  • 10
  • 62
  • 80
Dhara
  • 1,431
  • 4
  • 29
  • 47
  • The more you compress an image, the more information you lose with JPEG as it is a lossy compression algorithm. if you try lower compression levels, do you get the same result? – Dragonthoughts May 07 '18 at 06:42
  • https://stackoverflow.com/questions/15237994/convert-pdf-to-high-quality-jpg-using-php-and-imagemagick – Joseph_J May 07 '18 at 07:07
  • I have tried to less compress but not working. @Joseph_J i get code from your link but not working. Is there any pdf resolution issue? – Dhara May 07 '18 at 07:13

2 Answers2

4

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.

digitalextremist
  • 5,952
  • 3
  • 43
  • 62
Kamlesh Solanki
  • 616
  • 5
  • 10
  • yes. it solved my problem. ` $im->flattenImages();` This solved my problem. But this function is deprecated. – Dhara May 08 '18 at 06:06
  • When pdf has multiple pages then multiple images are not generated. Is there any option to set? – Dhara May 08 '18 at 10:07
  • replace `$im = $im->flattenImages();` with the following `$im->setImageBackgroundColor('white'); $im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);` – YesItsMe Mar 01 '21 at 02:42
2

I think the problem lies in the fact that we are inputing a resolution of dots per inch from the pdf and outputting it as pixels per inch as we make the jpeg.

This codes works as far as it appears to keep the correct aspect ration and I can make out the pdf that I used to test. But it's not very clear at all, I cant read any text. Here is what I used.

    $pdfPath = $config['upload_path'] . '/' . $fileName;
    $img = new imagick();
    $img->setResolution(300, 300);
    $img->readImage($pdfPath);  //Open after yuo set resolution.
    $img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH); //Declare the units for resolution.
    $img->setImageFormat('jpeg');
    $img->setImageCompression(imagick::COMPRESSION_JPEG);
    $img->setImageCompressionQuality(100);
    $img->writeImage($config['upload_path'] . '/' . str_replace('pdf', 'jpeg', $fileName));
    $img->clear();
    $img->destroy();

From what I have read you may need to install Ghostscript on your machine and executing the script from your command line seems to be preferred and offer better results and performance.

I found this article and it looks like it's loaded with a bunch of info for image formats and how imagemagik handles them. It has a bit in there on PDFs.

http://www.imagemagick.org/Usage/formats/#tiff

I might look into a PHP PDF library that has all the stuff already built into it. No sense in reinventing the wheel. There might be a function already build in to do exactly what your trying to do.

Joseph_J
  • 3,654
  • 2
  • 13
  • 22