Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 8192 bytes) in /home/example/public_html/vendor/intervention/image/src/Intervention/Image/Gd/Decoder.php on line 154

I have deployed my project on a live server. am using image intervention to crop some images am uploading to the database. Am using the dropzone form to upload 5 images. Everything is working well but am getting this error PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 8192 bytes) in /home/example/public_html/vendor/intervention/image/src/Intervention/Image/Gd/Decoder.php on line 154 in the logs folder. I have researched the error and tried to solve it by adding ini_set('memory_limit','8192M'); but still, the bug doesn't disappear. How can I fix this? this is the function that uploads the images in the controller

                                           $rental_imagesname=$rentaldata->rental_name.'-'.rand(111,9999).'.'.$extension;

$alternatelarge_image_path='imagesforthewebsite/rentalhouses/alternateimages/large/'.$rental_imagesname; $alternatemedium_image_path='imagesforthewebsite/rentalhouses/alternateimages/medium/'.$rental_imagesname; $alternatesmall_image_path='imagesforthewebsite/rentalhouses/alternateimages/small/'.$rental_imagesname; ini_set('memory_limit','8192M'); Image::make($rental_imagestmp)->resize(1040,1200)->save($alternatelarge_image_path); Image::make($rental_imagestmp)->resize(520,600)->save($alternatemedium_image_path); Image::make($rental_imagestmp)->resize(260,300)->save($alternatesmall_image_path); $rental_images=new Alternaterental_image(); $rental_images->image=$rental_imagesname; $rental_images->house_id=$id; $rental_images->save();

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message indicates that the PHP script has exceeded the allowed memory limit. One solution is to increase the memory limit in the PHP configuration file (php.ini).

To do this, locate the php.ini file on the server and open it in a text editor. Look for the line that sets the memory_limit and increase the value to a higher number, such as 128M or 256M. Save the file and restart the web server.

If you don't have access to the php.ini file, you can try increasing the memory limit in the PHP script itself using the ini_set() function. However, this may not work if the server has a hard limit on memory usage.

Another solution is to optimize the image processing code to use less memory. One way to do this is to process the images in smaller batches instead of all at once. For example, you could process one image at a time and free up memory before processing the next image.

Here's an example of how to process images in batches:

$images = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg'];

foreach ($images as $image) {
    $rental_imagesname = $rentaldata->rental_name . '-' . rand(111, 9999) . '.' . $extension;
    $alternatelarge_image_path = 'imagesforthewebsite/rentalhouses/alternateimages/large/' . $rental_imagesname;
    $alternatemedium_image_path = 'imagesforthewebsite/rentalhouses/alternateimages/medium/' . $rental_imagesname;
    $alternatesmall_image_path = 'imagesforthewebsite/rentalhouses/alternateimages/small/' . $rental_imagesname;
    
    ini_set('memory_limit', '128M');
    
    $rental_imagestmp = Image::make($image);
    
    $rental_imagestmp->resize(1040, 1200)->save($alternatelarge_image_path);
    $rental_imagestmp->resize(520, 600)->save($alternatemedium_image_path);
    $rental_imagestmp->resize(260, 300)->save($alternatesmall_image_path);
    
    unset($rental_imagestmp);
    
    $rental_images = new Alternaterental_image();
    $rental_images->image = $rental_imagesname;
    $rental_images->house_id = $id;
    $rental_images->save();
}
1 like
Snapey's avatar

You exhausted 32MB memory limit by 8Kb but you tried setting the memory limit to 8GB (per php thread) !

How big is your server?

1 like

Please or to participate in this conversation.