Laravel 7:图像干预无法保存到存储中

问题描述

图片上传到存储设备时出现问题。我以前用过这个:

        
        if ($files) {
          foreach ($files as $file) {
            $imageName = 'gambar_laporan_'.$i.'.'.$file->getClientOriginalExtension();
            $upload = Storage::put($filePath.$imageName,file_get_contents($file));
            
          }
        }

在我尝试使用图像干预IO调整图像大小之前,它工作正常。所以我将代码更改为此:

$files = $request->file('gambar_laporan');
         
         
         if ($files) {
           foreach ($files as $file) {
             $imageSize = getimagesize($file);

             $newWidth = $imageSize[0] - ($imageSize[0] * 0.3);
             $newHeight = $imageSize[1] - ($imageSize[1] * 0.3);
             $image_resize = Image::make($file->getRealPath())->resize($newWidth,$newHeight);


             $imageName = 'gambar_laporan_'.$i.'.'.$file->getClientOriginalExtension();
             $upload = Storage::put($filePath.$imageName,file_get_contents($image_resize));
            
             
           }
         }

然后我有这个问题

file_get_contents(): Filename cannot be empty

我该怎么办?我找不到解决错误的任何问题。

解决方法

您可以使用以下代码进行检查:

        $file = $request->file('gambar_laporan');
        $image = Image::make($file);
        $extension = $filename->extension();
        $file = Str::random(40) . '.' . $extension;
        Storage::put('images' . $file,$image->orientate()->encode($extension),'public');
        $image->resize(350,350,function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        })->save();

        return $file;
        // It will return the name of image.