Norbertho's avatar

Allowed memory size of 134217728 bytes exhausted

Hi, I have the following error when i register a user:

{message: "Allowed memory size of 134217728 bytes exhausted (tried to allocate 71073600 bytes)",…}
exception: "Symfony\Component\Debug\Exception\FatalErrorException"
file: "E:\Projects\test\listico\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php"
line: 30
message: "Allowed memory size of 134217728 bytes exhausted (tried to allocate 71073600 bytes)"
trace: []

I know that i have reached the memory limit and i have run out of memory i understand that i have to raise the memory limit in php.ini file, but i am worried how is that possible? I have tried to upload 2 images each image exactly 527kb so altogether around 1 mb and i have about 30 text type input field. So I just dont get it how is it possible to reach the 134 mb limit? Any thoughts? I send all the data to back end with axios. Is it normal? or i have done something terribly wrong with my code?

Thanks for any advice.

0 likes
8 replies
Tray2's avatar

Show us some code and we might be able to help you.

Cronix's avatar

E:\Projects\test\listico\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php

What do your validation rules for the images look like and are you doing any extra processing on them? Intervention can take a lot of resources depending on what it's doing.

Norbertho's avatar

Validation

        return Validator::make($request, [
           'username' => 'required|string|max:255',
           'tokena'  => 'required|string',
           'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/',
            'category' => 'required|string',
            'platform' => 'required|string',
            'type' => 'required|string',
            'website' =>'required|string',
            'kyc' => 'required|string',
            'aml' => 'required|string',
            'logoupload' => 'required|image',
            'imageupload' => 'required|image',
        ]);

The 2 images i have tried to upload was 527kb each.

And that is how i tried to save images:

  /*Logo Upload*/

            $logo = $data['logoupload'];
            $logoname = uniqid($user->id."_").".".$logo->getClientOriginalExtension();

            Storage::disk('s3')->put($logoname, File::get($logo), 'public');
            $logourl = Storage::disk('s3')->url($logoname);

          $thumb = Image::make($logo);
          $thumb->fit(200);
          $jpg = (string) $thumb->encode('jpg');
          $thumbName = pathinfo($logoname, PATHINFO_FILENAME).'-thumb.jpg';
          Storage::disk('s3')->put($thumbName, $jpg, 'public');

        /*ImageUpload*/
            $request = request();
            $image = $data['imageupload'];
            $imagename = uniqid($user->id."_").".".$image->getClientOriginalExtension();

            Storage::disk('s3')->put($imagename, File::get($image), 'public');
            $imageurl = Storage::disk('s3')->url($imagename);

            $thumb2 = Image::make($image);
            $thumb2->fit(350, 233);
            $jpg2 = (string) $thumb2->encode('jpg');
            $thumbName2 = pathinfo($imagename, PATHINFO_FILENAME).'-thumb.jpg';
            Storage::disk('s3')->put($thumbName2, $jpg2, 'public');

            $thumb2n = Image::make($image);
            $thumb2n->fit(900, 600);
            $jpg2n = (string) $thumb2n->encode('jpg');
            $thumbName2n = pathinfo($imagename, PATHINFO_FILENAME).'-thumbn.jpg';
            Storage::disk('s3')->put($thumbName2n, $jpg2n, 'public');

Snapey's avatar

do you have any with() statements in your models?

Cronix's avatar
Cronix
Best Answer
Level 67

Ah, you're using Intervention to do some stuff.

$thumb = Image::make($logo);
$thumb->fit(200);
$jpg = (string) $thumb->encode('jpg');
$thumbName = pathinfo($logoname, PATHINFO_FILENAME).'-thumb.jpg';
Storage::disk('s3')->put($thumbName, $jpg, 'public');

$thumb->destroy(); // free the memory that intervention was using
unset($jpg); // same here since it contains the entire jpg as a string

After you're done saving the image(s), destroy() them so it will free up the resources it was using. http://image.intervention.io/api/destroy

1 like
Snapey's avatar

it should be fairly easy to exclude bits of code to find what is causing the issue?

Cronix's avatar

If it fixes it, please mark the thread as solved. Remember to destroy/unset each place you're doing that.

Please or to participate in this conversation.