ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
TomXampp
Posts: 26
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by TomXampp »

I've succeeded in installing ImageMagick on my local computers and Imagick in both XAMPP and WAMP, and I can perform resizing operations on JPGs of any size on my localhost sites when running them. My live host is DreamHost, and they have ImageMagick/Imagick configured as shown here:

Image

However, the very same code cannot handle JPGs above approximately 7mb on DreamHost, and I can't determine why.

Here is sample code for the resizing routine I'm using, which works perfectly on JPGs of any size on my localhost, but fails on DreamHost when the JPG is about 7mbs or larger:

Code: Select all

<?php

$source_image_file = "1.jpg";
$source_image_path = $_SERVER['DOCUMENT_ROOT'] . "/" . $source_image_file;
$destination_image_file = "1-resized.jpg";
$destination_image_path = $_SERVER['DOCUMENT_ROOT'] . "/" . $destination_image_file;

$im = new Imagick($source_image_path);
$im->resizeImage(528, null, Imagick::FILTER_LANCZOS2SHARP, 1);
$im->stripImage();
$im->writeImage($destination_image_path);
$im->clear();

?>
Is there (a) something missing in the DreamHost Imagick configuration, and/or (b) an operation I can perform on the Imagick object that will allow it to open and process files ~7mbs and larger? (e.g., creating an empty Imagick object first, setting some values on it, and then reading in the JPG? I tried doing that following the procedures of some other examples of how to open a file that I found online, but the result was the same...DreamHost's Imagick config [or something] prevents it from working on larger JPGs...and I'm testing a 10mb and 20mb file that will open and convert perfectly on my localhosts...a 6.6mb file can be opened and processed by Imagick on both of my localhosts and on DreamHost, which makes this all the more curious.)

Many thanks in advance!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by Bonzo »

I would first contact Dreamhost and see what they say; there might be a restriction on the amount of ram you can use.

You can use -define in the command line but I doubt Imagick supports that:
jpeg:size=geometry Set the size hint of a JPEG image, for example, -define jpeg:size=128x128. It is most useful for increasing performance and reducing the memory requirements when reducing the size of a large JPEG image.
From another thread fmw42 suggested:
I know you are not working on large files but there may be something useful there. But again it might not be possible to implement in Imagick.
TomXampp
Posts: 26
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by TomXampp »

I already have contacted DreamHost and they suggested turning off FastCGI, on the misunderstanding that it was a "timing-out" error. I did so, nonetheless, and to no effect.

Following your suggestion, I used the setOption operator to specify the file type and maximum output size, as here:

Code: Select all

$im = new Imagick();
$im->setOption('jpeg:size', '528x4000');
$im->readImage($source_image_path);
As expected, this works fine (as did the original version of the code) on my localhosts (XAMPP and WAMP), but not on DreamHost. And the DreamHost memory limitations seem to be extremely large and fine for these purposes. :-(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by fmw42 »

I presume your Host has put some RAM limitations in their PHP configuration or Imagemagick configuration (not Imagick). Point your ISP host to the links that bonzo has provided and see what they say.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by fmw42 »

Have them especially check the MAGICK_MEMORY_LIMIT in the IM policy.xml file, which may default to 8MB . See http://www.imagemagick.org/script/resou ... nvironment and viewtopic.php?f=4&t=26801
TomXampp
Posts: 26
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by TomXampp »

I've found a work-around. I first test if the image file is greater than 5MB. If so, then it loads it setting resource limits, as here:

Code: Select all

$im = new Imagick();
$im->setResourceLimit(Imagick::RESOURCETYPE_MEMORY,1);
$im->setResourceLimit(Imagick::RESOURCETYPE_MAP,1);
$im->readImage($source_image_file);
I'm only guessing at 5MB (6MB seems to work without resource limits, but I wanted to set the bar a bit lower just in case).

This points at a different problem: I wanted to make the conditional code execute when an exception error is thrown by Imagick, but try as I might, I couldn't succeed at it. It has no problem with just creating the object, like this:

Code: Select all

$im = new Imagick($filename);
Doing that isn't a problem. Doing anything with the Imagick object AFTER initializing it with an image file then causes the routine to crash (e.g. resize, get geometry, etc.) Putting that into a try/catch block did not work. Putting it into a try/catch block would be ideal, because then I'm not simply relying on an arbitrary file size (5MB in this case) as the litmus test for conditionally branching to setting resource limits that force the work to be done on disk and not in RAM.

Is there a magic-bullet coding solution for this? Following the prescriptions here:

https://stackoverflow.com/questions/158 ... -error-php

...did not work. Instead of proceeding to the "catch" block, the routine simply falls over. And I simply used:

Code: Select all

$im = new Imagick($filename);
$geo = $im->getImageGeometry();  
...in the "try" block; the routine simply stops when the $geo line is executed (i.e., it doesn't advance to the "catch" block.)

Any suggestions?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by fmw42 »

See my message above. For example on my home system:

Code: Select all

identify -list resource
Resource limits:
  Width: 214.7MP
  Height: 214.7MP
  Area: 4.295GP
  Memory: 2GiB
  Map: 4GiB
  Disk: unlimited
  File: 192
  Thread: 1
  Throttle: 0
  Time: unlimited
Have your ISP check this or do it in PHP exec()

See if any of them are too small, especially the Memory item.
TomXampp
Posts: 26
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by TomXampp »

Thanks for the suggestion. I created this PHP script:

Code: Select all

<pre>
<?php

$output = exec("identify -list resource");
print_r($output);

$source_image_file = "0.jpg";
$source_image_path = $_SERVER['DOCUMENT_ROOT'] . "/" . $source_image_file;
$im = new Imagick($source_image_path);

print_r("\n\nUndefined: ");
print_r($im->getResourceLimit(imagick::RESOURCETYPE_UNDEFINED));

print_r("\nArea: ");
print_r($im->getResourceLimit(imagick::RESOURCETYPE_AREA));

print_r("\nDisk: ");
print_r($im->getResourceLimit(imagick::RESOURCETYPE_DISK));

print_r("\nFile: ");
print_r($im->getResourceLimit(imagick::RESOURCETYPE_FILE));

print_r("\nMap: ");
print_r($im->getResourceLimit(imagick::RESOURCETYPE_MAP));

print_r("\nMemory: ");
print_r($im->getResourceLimit(imagick::RESOURCETYPE_MEMORY));

?>
</pre>
...which produced this output on screen:
768 8.4361GB 31.427GiB 62.854GiB unlimited 1 3600

Undefined: 0
Area: 8436092928
Disk: -1
File: 768
Map: 67488743424
Memory: 33744371712
Sadly, the exec on "identify -list resource", which does identify its values when I run it on my own CMD console in Windows, only provides the numeric output as shown when run on the DreamHost server. Thus I added the other print-outs, which seem to correlate with the values of the "identify" routine. Is the presence or absence of a value in the two results indicative of anything that hasn't already come to mind?
Does this hold a clue?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by fmw42 »

You Imagemagick version on the server at 6.6.9.7 is ancient. Thus, likely the limited output from identify -list resources. I am not an expert at interpretation or knowing what are good values, though the policy.xml file in the link I referenced gives some numbers that the IM developers suggest.

I do not think you can change resources on an image by image basis.
TomXampp
Posts: 26
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Re: ImageMagick cannot open/process files above 7mb on DreamHost...trying to find configuration that will work

Post by TomXampp »

I've apparently found the solution.

I hit on this solution when I discovered that at certain times of the day I could process fairly large files with Imagick on the live server, but then could not just a few minutes later. This suggested to me that memory was being consumed by others on the shared server at fully expected erratic and unpredictable intervals, and therefore if there were a way to allocate memory to the Imagick object *before* initializing a file in it, then it might work.

The trick is to explicitly allocate memory to the Imagick object after creating it using the setResourceLimit() function of the object, calling the 5 "RESOURCETYPE" values as shown below. What follows that is simply the resizing routine I perform on the image file, which was never the source of the problem.

Code: Select all

$im = new Imagick();

$im->setResourceLimit(imagick::RESOURCETYPE_MEMORY, 256);
$im->setResourceLimit(imagick::RESOURCETYPE_MAP, 256);
$im->setResourceLimit(imagick::RESOURCETYPE_AREA, 1512);
$im->setResourceLimit(imagick::RESOURCETYPE_FILE, 768);
$im->setResourceLimit(imagick::RESOURCETYPE_DISK, -1);

$im->readImage($source_image_path);
$im->resizeImage(528, null, Imagick::FILTER_LANCZOS2SHARP, 1);
$im->stripImage();
$im->writeImage($destination_image_path);
$im->clear();
I hope this is helpful to someone else, because I was banging my head against the wall trying to find a solution; Imagick/ImageMagic is indispensable to my site.

Also, if anyone knowledgeable about other RESOURCETYPE values I may want to apply here, please chime in. I happened upon the settings above while scouring the internet for info about allocating memory to an Imagick object for the same precise purposes I've been trying to achieve.
Post Reply