7

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!

Brian Mayer
  • 989
  • 3
  • 13
  • 23

2 Answers2

12

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

dakdad
  • 2,947
  • 2
  • 22
  • 21
  • If I put setImageResolution first, the class hasn't been declared yet. If I do $im = new imagick(), then $im->setImageResolution, then $im->readImage($srcimg), I get this error: Fatal error: Uncaught exception 'ImagickException' with message 'Can not process empty Imagick object' in ... – Brian Mayer Mar 06 '13 at 03:07
  • 1
    try using `$im->setResolution()` on the empty object (see nots for `Imagick::setResolution` on the manual) – dakdad Mar 06 '13 at 03:28
  • Yes--just did this and I got it to work, although only 300,300 was needed. The trick was switching the order and using setResolution instead of setImageResolution. Thanks! – Brian Mayer Mar 06 '13 at 03:29
  • 1
    This is so right, thank you. Everyone repeat: Set the resolution before reading the image in. Also, use methods like setResolution and not setImageResolution, otherwise it errors out. That tripped me up. – coupdecoop Mar 31 '15 at 23:42
10

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();
user2957058
  • 270
  • 5
  • 16