Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set density parameter for imagick with php

Tags:

php

pdf

png

imagick

I want to convert a pdf page to a png image with Imagick.

I tried with PHP, but the image quality was very low.
When I tried with command line, the result was perfect.

PHP code

$im = new imagick( __DIR__ . DIRECTORY_SEPARATOR.$PDFName.'['.$i.']' );  
$params = $im->identifyImage();
$width = $params['geometry']['width']*1;
$height = $params['geometry']['height']*1;
$im->setResolution(400,400);
$im->resizeImage($width ,$height, imagick::FILTER_SINC, 1, true);
$im->writeImage(__DIR__ . DIRECTORY_SEPARATOR.'pdf_pages\\'.$i.'.png'); 
$im->clear(); 
$im->destroy();

Command line code

convert -density 400 a.pdf -resize 25% -a.png

PHP code (2nd attempt)

$im = new imagick( __DIR__ . DIRECTORY_SEPARATOR.$PDFName.'['.$i.']' );  
$im->setOption('density','400x400');
$im->setOption('resize','25%');
$im->writeImage(__DIR__ . DIRECTORY_SEPARATOR.'pdf_pages\\'.$i.'.png'); 
$im->clear(); 
$im->destroy();

Still bad results.

What can I do? I want to use the density parameter in my PHP code, but how?

Output with PHP enter image description here

Output with command line enter image description here

like image 596
dechiffre Avatar asked Dec 25 '12 20:12

dechiffre


People also ask

How resize ImageMagick in PHP?

Imagick::resizeImage Before this version given dimensions 400x400 an image of dimensions 200x150 would be left untouched. In Imagick 3.0. 0 and later the image would be scaled up to size 400x300 as this is the "best fit" for the given dimensions. If bestfit parameter is used both width and height must be given.

Is imagick included in PHP?

Imagick is a PHP extension to create and modify images using the ImageMagick library. There is also a version of Imagick available for HHVM. Although the two extensions are mostly compatible in their API, and they both call the ImageMagick library, the two extensions are completely separate code-bases.

What is imagick PHP module?

Imagick: The PHP ImageMagick Extension The ImageMagick extension, called Imagick when referring to the PHP extension, is a native PHP extension to create and modify images using the ImageMagick API.

What is imagick library?

In simple terms, ImageMagick is a large collection of tools and libraries used to read, write, and manipulate digital images. These 200 formats include popular image formats such as TIFF, JPEG, PNG, PDF, PhotoCD, and GIF, etc.


2 Answers

You need to set Resolution before you read the file.

$im = new imagick();
$im->setResolution(200,200);
$im->readImage(__DIR__ . DIRECTORY_SEPARATOR.$PDFName.'['.$i.']');

Than, the result will be perfect.

Hope this help someone.

like image 177
Simon Arnold Avatar answered Oct 05 '22 01:10

Simon Arnold


From the manual http://php.net/manual/en/imagick.setresolution.php

Imagick::setResolution() must be called before loading or creating an image.

See this answer Pdf to image using php-imagick api

like image 23
dakdad Avatar answered Oct 05 '22 02:10

dakdad