使用PHP设置Imagick的密度参数

8

我想使用Imagick将PDF页面转换为PNG图像。

我尝试过PHP,但图像质量非常低。
当我尝试使用命令行时,结果是完美的。

PHP代码

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

命令行代码

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

PHP 代码(第二次尝试)

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

结果仍然不理想。

我该怎么办?我想在我的PHP代码中使用密度参数,但是怎么用呢?

PHP输出 enter image description here

命令行输出 enter image description here


你应该提供PDF文件。 - hakre
1
这是PDF文件 => http://www.cevhersys.net/5663.pdf - dechiffre
此外,如果 PHP API 无法满足您的需求,您始终可以回到使用 execshell_exec 来执行命令并生成所需的文件。 - dakdad
2个回答

30

在读取文件之前,您需要设置分辨率

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

那么,结果将会是完美的。

希望这能帮助某人。


5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接