3

I'm using Image Intervention in my project.

My application working smoothly while uploading small size images. But when I try to upload large size image(>2mb), my application stops working!

Even It shows no proper errors. Sometimes It shows Token mismatch error & sometimes the url not redirects.

How to fix it? I've no idea.

Here is my code:

$post->new Post();

if($request->hasFile('image')){
    $image=$request->file('image');
    $filename=Auth::user()->id.'_'.time().'.'.$image->getClientOriginalExtension();
    $location=public_path('images/'.$filename);
    Image::make($image)->save($location);

    $post->image=$filename;
}

$post->save();

I'm using Image intervention for uploading images. But you can suggest alternative of it as well.

Thanks!

S M Iftakhairul
  • 1,120
  • 2
  • 19
  • 42

2 Answers2

3

Actually this is the issue from server side setting variable values into php.ini file. if you upload more then your server's post_max_size setting the input will be empty, you will get Token mismatch error.

change upload_max_filesize , post_max_size value as per you required and restart the server.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • 1
    I've changed `upload_max_filesize=100M` & `post_max_size=20M` and restarted the server. After that I've tried to upload a `jpeg` image(4.04M). Now It shows no errors but when i submit my form, a blank page comes & after all image not uploading. Can you explain please? – S M Iftakhairul Aug 17 '17 at 13:54
  • Hope you check with with debugging turned on. Also look at [ini.memory limit](http://ca3.php.net/manual/en/info.configuration.php#ini.max-execution-time) and [ini.upload-max-filesize](http://ca3.php.net/manual/en/ini.core.php#ini.upload-max-filesize). May this help you. – AddWeb Solution Pvt Ltd Aug 17 '17 at 14:11
  • Had this exact error (token mismatch on a large file image upload) and changed the two ini settings noted in the answer. This successfully fixed the issue. Thank you @AddWeb – Watercayman Aug 27 '17 at 21:55
0

It turns out this is a memory issue. If you check the error log you with see that the server ran out of memory. You will see something like

PHP Fatal error:  Allowed memory size of XXXXXXXX bytes exhausted (tried to allocate XXXXX bytes) in ...

Because Intervention Image reads the whole image pixel by pixel keeping the data in memory, seemingly small images like 2MB can end up requiring dozens of MB of memory to process.

You may need to set your memory limit to the highest available and check the file size before it is opened because a site that breaks without error messages is embarrassing. Use something like

if( $request->hasFile('image') && $request->file('image')->getClientSize() < 2097152 ){
    $image=$request->file('image');
    $filename=Auth::user()->id.'_'.time().'.'.$image->getClientOriginalExtension();
    $location=public_path('images/'.$filename);
    Image::make($image)->save($location);

    $post->image=$filename;
}
murume
  • 154
  • 1
  • 3
  • 13