imagerotate() seems to produce a fatal error with an image that has dimensions of 2048x2048.

<br />

<b>Fatal error</b>:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 8192 bytes) in

 <b>/home/kintyrep/public_html/rotate.php</b> on line <b>32</b><br />

At what dimensions will this function fail and how do I detect failure without producing a fatal error?

    The problem is that your script requires too much memory to rotate an image that large. Exactly how much memory is "too much memory" will depend on the server configuration. You can set the memory limit in the PHP.INI file. 32MB is pretty much memory. I think the default is 8MB.

    I'm not certain, but I don't think you can predict just how much memory imagerotate() will require to rotate a given image. It's certainly not a simple function of image dimensions. You could try to ball park some rough upper limit to rotatable image dimensions based on the 'pixel area' of the image (i.e., the width_in_pixels * height_in_pixels of the image). You would try progressively smaller images until it succeeds and then use that as your upper bound.

    As for avoiding a fatal error, I'm not sure that will be possible. Running out of memory is a fairly nasty situation. If you're using php 5 or higher, try wrapping the imagerotate() call in a try/catch block.

    try {
      imagerotate($blah, $blah);
    } catch (Exception $e) {
      echo 'oops...it did not work ';
    }
    

      You can (normally) dynamically increase the memory limit for a PHP script using [man]ini_set/man.

      In fact, while sneakyimp is correct in saying that memory determination for an image is based on more than just its dimensions, you can actually calculate roughly how much memory an image will need; several user-contributed notes on the manual page for [man]imagecreatefromjpeg/man have suggested functions to do just that (search for 'setMemoryForImage' and you'll find one).

        I feel I should point out that the memory required to rotate an image is greater than the memory needed to create an image or crop it. I noticed this when I was working on a site that had an image rotator/resizer/cropper. I could not rotate certain images that were easily resized or cropped.

          I created a blank image 2048 x 2048 that was only about 45k. This still caused imagerotate to run out of memory.

          When uploaded, the largest images displayed are 200x200. So I am going to use copyresampled to make the image a smaller size at the time it is uploaded. This should eliminate any memory issues.

            asa_carter wrote:

            I created a blank image 2048 x 2048 that was only about 45k.

            No, it wasn't. You can work out for yourself that a 2048-pixel by 2048-pixel has over four million pixels in it. If it was a truecolour image there would be three bytes per pixel, so that's 12MB of memory required just to store this blank image (if it was a palette-based image it's still 4MB; for hints about getting a more accurate result, see bradgrafelman's advice). And since, when rotating, the transformed image is usually larger than the source (so that the corners don't get clipped), rotating a 2048x2048 image will need more than 24MB.

              That is jpg, which is a compressed image. When you do any kind of work on it with non-specialized software, it first needs to be decompressed in its 'raw' form, inflating it to it's full size.

                You could just try increasing the memory limit to an arbitrarily large value... say 40-50MB?

                  I am on a shared host so I'm not sure if I can / am allowed to do that. I also have limited disk space so reducing the size of an image is better anyway in this case.

                    Write a Reply...