Convert PDF to high quality JPG using PHP and ImageMagick

有些话、适合烂在心里 提交于 2019-12-07 02:13:28

问题


I'm tearing my hair out.

I have a 300 DPI PDF that I want to turn into a 300 DPI JPG that's 2550x3300. I am told ImageMagick can do this, so I get ImageMagick to work, but it only returns a JPG that is sized about 1/5 the original PDF size.

It's not the source image--I've done it with several high quality PDFs and they all have the same problem.

After scouring StackOverflow for ideas, this is what I came up with to use:

$im = new imagick($srcimg);
$im->setImageResolution(2550,3300);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG); 
$im->setImageCompressionQuality(100);
$im->writeImage($targetimg);
$im->clear();
$im->destroy();

But it still doesn't work.

I also have tried using $img->resizeImage() to resize the JPG, but then it comes out at really bad quality, if the right size.

Totally stumped. Appreciate your help!


回答1:


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




回答2:


This would be the correct way, the quality will improve.

$im = new imagick();
$im->setResolution(300, 300);
$im->readImage($srcimg);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG); 
$im->setImageCompressionQuality(100);
$im->writeImage($targetimg);
$im->clear();
$im->destroy();


来源:https://stackoverflow.com/questions/15237994/convert-pdf-to-high-quality-jpg-using-php-and-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!