How to upload large size image by Intervention Image in Laravel 5

ぃ、小莉子 提交于 2019-12-23 20:14:39

问题


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!


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/45736358/how-to-upload-large-size-image-by-intervention-image-in-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!