首页 文章

使用Laravel Intervention上传的大图片

提问于
浏览
1

在我的laravel设置中,我目前正在使用Intervention进行图像上传 .

但是,目前还没有上传大小超过3MB的图像 .

ini_get('upload_max_filesize')ini_get('post_max_size') 分别给我5MB和8MB .

我的图像保存控制器如下:

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

   }
}

上传3MB或更高的文件时,我只返回一个白页 . 此外,我的 laravel.log 文件没有显示任何错误 .

通过在每一行之后使用 dd('test'); ,我能够发现崩溃发生在这一行:

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

救命?

EDIT:

apache错误日志中有错误:

[Mon Oct 31 04:17:31.109685 2016] [:error] [pid 13024:tid 1596] [client :: 1:55986] PHP致命错误:允许的内存大小为134217728字节耗尽(试图分配20480字节)第119行的C:\ xampp \ htdocs \ series \ commend-me \ CommendMe \ vendor \ intervention \ image \ src \ Intervention \ Image \ Gd \ Decoder.php,referer:http:// localhost / submit

尝试上传3MB以上的图片时会出现此错误 .

我在php.ini文件中提高了内存限制,但是我仍觉得这很奇怪 . 为什么这会占用如此多的内存?这是正常的吗?

1 回答

  • 3

    图像操作往往会耗费大量内存,因为图像处理库通常会将所有像素“解包”到内存中 . 因此,一个3MB的JPEG文件可以很容易地在内存中增长到60MB,而且当你可能达到为PHP分配的内存限制时 .

    据我所知,XAMP仅为PHP分配128 MB RAM .

    检查你的php.ini并增加内存限制,例如:

    memory_limit = 512MB
    

相关问题