Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intervention image Allowed memory size of 20971520 bytes exhausted (tried to allocate 10240 bytes)

I am implementing some image manipulation using great intervention library on Laravel 5. It works fine if image is small, like under 700KB with size lower than 800px wide.

But its not able to handle large size images, I want to upload images as big as 8 MB with 2000px wide.

I have tried classic way of bumping the memory_limit but its not working

ini_set("memory_limit","20M");

Is there any solution which can work without killing the server.

Here is the code of my Upload service. It takes image from Request::file('file') and Resize it to 3 sizes using intervention.

  • Big size which is max width of 2000 px, then a
  • Medium 800px wide
  • Thumb is 200px wide

    public function photo($file)
    {
        $me = Auth::user();
        //user folder for upload
        $userFolder = $me->media_folder;
    
        //Check the folder exist, if not create one
        if($this->setupUsrFolder($userFolder)) {
            //Unique filename for image
            $filename = $this->getUniqueFilename($userFolder);
    
            //Bump the memory to perform the image manipulation
            ini_set("memory_limit","20M");
    
            //Generate thumb, medium, and max_width image
            $img = Image::make($file);
            //big_img
            $big = $img->resize(config('go.img.max_width'), null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
            //Big Image
            $big->save($this->getPhotoPath($filename, $userFolder, 'b_'));
    
            //Medium image
            $med = $big->resize(config('go.img.med_width'), null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
            $med->save($this->getPhotoPath($filename, $userFolder, 'm_'));
    
            //Thumbnail
            $thumb = $med->resize(config('go.img.thumb_width'), null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
            $thumb->save($this->getPhotoPath($filename, $userFolder, 't_'));
    
            return $filename;
        }
    
        return null;
    }
    

Please help me how I can make it more efficient on and fast

like image 469
Saqueib Avatar asked Jun 30 '15 03:06

Saqueib


2 Answers

FWIW, the commented solutions didn't work for me. That is, I couldn't get the following to work for me:

Image::make($file)->resize(2000, null)->save('big.jpg')->destroy();
Image::make($file)->resize(800, null)->save('med.jpg')->destroy();
Image::make($file)->resize(200, null)->save('thumb.jpg')->destroy();

It was still throwing the following error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes) in  
C:\Users\XXX\app\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php on line 136

This solution worked for me was to change the memory limit as follows:

public function resize() {
    ini_set('memory_limit', '256M');
    // Do your Intervention/image operations...
}

// or...

ini_set('memory_limit', '256M');
Image::make($file)->resize(2000, null)->save('big.jpg');
Image::make($file)->resize(800, null)->save('med.jpg');
Image::make($file)->resize(200, null)->save('thumb.jpg');

This successfully solved the problem.
The reason being:

Resizing images is a very memory consuming task. You can't assume that a 1MB image takes 1MB of memory. Resizing a 3000 x 2000 pixel image to 300 x 200 may take up to 32MB memory for example. Even if the image is under 1MB filesize.
@olivervogel. 2018. Allowed memory size of 134217728 bytes exhausted. [ONLINE] Available at: https://github.com/Intervention/image/issues/567#issuecomment-224230343. [Accessed 14 June 2018].

like image 194
JustCarty Avatar answered Oct 19 '22 23:10

JustCarty


Just update php.ini file memory_limit to 256M.

memory_limit=256M

I have tried with other memory_limit with 128 and 20. That doesn't work for me. But when I update my memory limit to 256M then the issue solved for me.

In the cpanel, you will find PHP options. From that you need to update the memory limit to:

memory_limit=256M
or
memory_limit=192M
or
memory_limit=368M
or
memory_limit=512M
like image 32
MD. IBRAHIM KHALIL TANIM Avatar answered Oct 19 '22 23:10

MD. IBRAHIM KHALIL TANIM