title: Image magick/PHP is falling over with large images
body: <p>I have a PHP script which is used to resize images in a user's FTP folder for use on his website.  </p>
  
  <p>While slow to resize, the script has completed correctly with all images in the past. Recently however, the user uploaded an album of 21-Megapixel JPEG images and as I have found, the script is failing to convert the images but not giving out any PHP errors. When I consulted various logs, I've found multiple Apache processes being killed off with Out Of Memory errors.  </p>
  
  <p>The functional part of the PHP script is essentially a for loop that iterates through my images on the disk and calls a method that checks if a thumbnail exists and then performs the following:  </p>
  
  <pre><code>$image = new Imagick();
  $image-&gt;readImage($target);
  $image-&gt;thumbnailImage(1000, 0);
  $image-&gt;writeImage(realpath($basedir)."/".rescale."/".$filename);
  $image-&gt;clear();
  $image-&gt;destroy();  
  </code></pre>
  
  <p>The server has 512MB of RAM, with usually at least 360MB+ free.  </p>
  
  <p>PHP has it's memory limit set currently at 96MB, but I have set it higher before without any effect on the issue.<br>
  By my estimates, a 21-Megapixel image should occupy in the region of 80MB+ when uncompressed, and so I am puzzled as to why the RAM is disappearing so rapidly unless the Image Magick objects are not being removed from memory.</p>
  
  <p>Is there some way I can optimise my script to use less memory or garbage collect more efficiently?<br>
  Do I simply not have the RAM to cope with such large images?</p>
  
  <p>Cheers</p>