Felix Maxime Felix Maxime - 1 month ago 16
PHP Question

Large images not being uploaded with Intervention in Laravel

In my laravel setup, I'm currently using Intervention for image uploads.

However, at the moment, images above the size of 3MB are not being uploaded.

ini_get('upload_max_filesize')
and
ini_get('post_max_size')
give me back 5MB and 8MB respectively.

My image save controller is as follows:

public function saveImage(Request $request) {
if (Auth::check()) {
$this->validate($request, [
'title' => 'required|max:50|min:2|alpha_num',
'mature' => 'required',
'categorie' => 'required',
'description' => 'string|max:2000',
'fileUpload' => 'required',
]);

$galleryConfirmation = Gallery::where('id', $request->get('gallerySelect'))->value('created_by');

if (Auth::user()->id == $galleryConfirmation || $galleryConfirmation == 0 || $galleryConfirmation == null ) {

$file = $request->file('fileUpload');

if ($file->getClientSize() > 3999999) {
dd("nooooo");
}

$filename = time() . '.' . $file->getClientOriginalExtension() ;

Image::make($file)->save( public_path('/uploads/artistUploads/' . $filename ) );

$thumb = Image::make($file);
$thumb->resize(null, 200, function ($constraint) {
$constraint->aspectRatio();
});
$thumb->save( public_path('/uploads/artistUploads/thumbs/' . $filename , 60) );

$images = new Images;

$images->title = $request->input('title');
$images->file_name = $filename;
$images->file_size = $file->getClientSize();
$images->file_mime = $file->getClientMimeType();
$images->file_path = 'uploads/artistUploads/' . $filename;
$images->description = $request->input('description');
$images->mature = $request->input('mature');
$images->categorie = $request->get('categorie');
if (Auth::user()->id == $galleryConfirmation) {
$images->gallery_id = $request->get('gallerySelect');
}
$images->created_by = Auth::user()->id;

$images->save();

return redirect()->back()->with('info', 'Image successfully uploaded.');

}

if (!Auth::user()->id == $galleryConfirmation) {
return redirect()->back()->with('info', 'Something went wrong');
}

}
}


When uploading a file of 3MB or higher, I get back only a white page. Furthermore, my
laravel.log
file is not showing any errors.

By using
dd('test');
after each line, I was able to find out that the crash happens at this line:


Image::make($file)->save( public_path('/uploads/artistUploads/' .
$filename ) );


Help?

EDIT:

There was an error in the apache error log:


[Mon Oct 31 04:17:31.109685 2016] [:error] [pid 13024:tid 1596] [client ::1:55986] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in C:\xampp\htdocs\series\commend-me\CommendMe\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php on line 119, referer: http://localhost/submit


This error shows up when trying to upload a picture above 3MB.

I raised the memory limit in my php.ini file, however I still find this rather strange. Why is this taking up so much memory? Is this normal?

Answer

Image operations tend to be quite memory exhausting because image handling libraries usually 'unpack' all the pixels to memory. A JPEG file of 3MB can thus easily grow to 60MB in memory and that's when you've probably hit the memory limit allocated for PHP.

As far I remember, XAMP only allocates 128 MB RAM for PHP.

Check your php.ini and increase the memory limit, e.g.:

memory_limit = 512MB
Comments