Search code examples
phpimage-processingimagemagickdpi

ImageMagick crashing for JPEG with incorrect density value


I am trying to convert an image with ImageMagick. I am cropping and resampling to 112 DPI:

convert myimg.jpg -crop 1024x683+0+0 -resize 100% -resample 112 downsample.jpg

A script runs through many images of varying DPI and downsamples them.

However, a particular file has gummed up the script. Somehow, the file has a DPI of 1 (?!?) So it ends up trying to sample it up to 112 from 1, and the attempt eats up all my RAM and hangs the script.

identify -verbose myimage.jpg 
Image: myimage.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  Geometry: 1024x683+0+0
  Resolution: 1x1 
  Print size: 1024x683

Is there an argument/flag I can use with ImageMagick to prevent this from happening, maybe something that tells it to only downsample?


Solution

  • Could your error be related to...

    For formats which do not support an image resolution, the original resolution of the image must be specified via -density on the command line prior to specifying the resample resolution.

    JPEG I know supports resolution but maybe its wrong in the EXIF header or somehow ambiguous / confused with embedded thumbnail (again, maybe the unit is assumed). I'd look more closely at the [-density] argument (http://www.imagemagick.org/script/command-line-options.php#density) and how it relates to resolution and density on certain formats.

    As for getting your script working. Seems you might want to break the operations out into more individual actions? If you can't satisfy the convert with using -density I would look at testing for the case you know fails and just skipping. Could parse the results of identify or just the failure of the convert. You are executing these in shell calls from PHP yes?