0

Getting this error when trying to load image into memory...

Allowed memory size of 10485760 bytes exhausted (tried to allocate 18048 bytes)

//attempt to open image
ini_set('memory_limit', '10M');
if($im = imagecreatefromjpeg($_GET['imgname'])) {
    //See if there is a width or height constraint
    if (isset($_GET['restraint'])) {
        $constr = $_GET['restraint'];
        if ($constr == "width") {
            if (imagesx($im) > imagesy($im)) {
                $im = imagerotate($im, 90, 0);
            }
        } elseif ($constr == "height") {
            if (imagesy($im) > imagesx($im)) {
                $im = imagerotate($im, 90, 0);
            }
        }
    }
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);
}

Any ideas?

Edit:

memory_get_usage = 125768 memory_get_peak_usage = 127016

Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • how large is the image ? –  Feb 27 '14 at 19:00
  • possible duplicate of [imagejpeg memory exhaustion](http://stackoverflow.com/questions/2668533/imagejpeg-memory-exhaustion) – Pitchinnate Feb 27 '14 at 19:01
  • How much memory is actually available on the server? Just because you tell PHP it can use that much doesn't mean that much is available. – Pitchinnate Feb 27 '14 at 19:06
  • How do I find that out @Pitchinnate ? The laptop itself has 4GB – Martyn Ball Feb 27 '14 at 19:08
  • jpeg is a compressed type of image, maybe when the image is loaded gets uncompressed, I had a similar situation but not in php, for example a 200kb image can grow up until 9MB in memory – Allende Feb 27 '14 at 19:09
  • I just used memory_get_usage and it returned: 125768 – Martyn Ball Feb 27 '14 at 19:11
  • Have you tried with a smaller image ? around 100kb ? If it works then your 1MB image it's the problem(based on resolution and bpp maybe it's too big) – Allende Feb 27 '14 at 19:16

1 Answers1

1

JPEG is a compressed type of image, maybe when the image is loaded gets uncompressed, I had a similar situation but not in php (.net compact framework):

Even though the compressed image size is 200kb, when you load it as a bitmap it will be decompressed and stored in memory in native Bitmap format. Given a height and width of 1500px each, and assuming a bitmap format of 32bpp (the default when not specified), you're looking at 9MB of allocated memory

1500 * 1500 * 4 = 9MB.

Take a look to this:

http://www.dotsamazing.com/en/labs/phpmemorylimit

Also you can try to set your memory limit directly on in php.ini and restart apache.

http://www.php.net/manual/en/ini.core.php#ini.memory-limit

Try with a big one (512M)

Here's the question I had but again, it's not php related, but image-size/memory related OutOfMemoryException loading big image to Bitmap object with the Compact Framework

Community
  • 1
  • 1
Allende
  • 1,480
  • 2
  • 22
  • 39