【问题标题】:Laravel 5.4 Error: NotReadableException: Image source not readableLaravel 5.4 错误:NotReadableException:图像源不可读
【发布时间】:2018-05-05 02:03:39
【问题描述】:

我正在尝试在创建个人资料时创建多个不同大小的个人资料图片副本。但我不断收到此错误:

“NotReadableException:图像源不可读”

有人能指出我在下面的代码中缺少什么吗:

public function updateprofile(UserProfileRequest $request){
    $user_id = Auth::User()->id;
    $profile = UserProfile::where('user_id','=',$user_id)->first();
    $profile->fullname = $request->fullname;

    if ($request->hasFile('img')) {
        if($request->file('img')->isValid()) {
            $types = array('_original.', '_32.', '_64.', '_128.');
            $sizes = array( '32', '64', '128');
            $targetPath = 'public/uploads/'.$user_id;

            try {
                $file = $request->file('img');
                $ext = $file->getClientOriginalExtension();
                $fName = time();
                $original = $fName . array_shift($types) . $ext;
                Storage::putFileAs($targetPath, $file, $original);

                foreach ($types as $key => $type) {
                    $newName = $fName . $type . $ext;
                    Storage::copy($targetPath . $original, $targetPath . $newName);
                    $newImg = Image::make($targetPath . $newName);
                    $newImg->resize($sizes[$key], null, function($constraint){
                        $constraint->aspectRatio();
                    });
                    $newImg->save($targetPath . $newName);
                }

                $profile->img = 'public/uploads/'.$user_id;
            } catch (Illuminate\Filesystem\FileNotFoundException $e) {
            }
        }
    }

    $profile->save();}

【问题讨论】:

  • 您可以在您的视图中发布提交此请求的表单吗?
  • 这里是查看页面中的图片上传字段:
    {!! Form::file('img',null,array('id'=>'img')) !!} {!! $errors->first('img') !!}
    如果您还需要什么,请告诉我。
  • 你在表单中使用 'files' => 'true', 'enctype' => "multipart/form-data" 吗?
  • 是的,我用过它们:{!! Form::model($profile, ['route'=>'profile','method'=>'post', 'files'=>'true', 'enctype'=>'multipart/form-data']) !!} {!! csrf_field() !!}

标签: laravel image-resizing intervention


【解决方案1】:

如果我没记错的话,提供的路径应该是您服务器上的绝对文件路径。例如,而不是:

$targetPath = 'public/uploads/'.$user_id;

使用(你的实际路径会因你的配置而异)

$targetPath = '/var/www/sitename/public/uploads/'.$user_id;

Laravel 还包含一个名为 public_path() 的辅助函数,可用于获取“公共目录的完全限定路径”。这将允许您使用以下内容:

$targetPath = public_path('uploads/'. $user_id);

另外,在这一行,不要忘记在新文件名前放置一个斜杠:

$newImg = Image::make($targetPath . '/' . $newName);

我还要确认执行脚本的用户(如果 apache 或 nginx 通常为 www-data 除非更改)对您的 public/uploads/ 目录具有写入权限

【讨论】:

    【解决方案2】:

    最后,我得到了它的工作。我对我的代码进行了以下更改:

    1. 使用commanderZiltoid 建议的完整操作系统路径作为目标路径。
    2. 不要使用Storage::putFileAs 方法来保存文件。所以,删除这一行:Storage::putFileAs($targetPath, $file, $original);
    3. 不要使用Storage::copy() 复制文件,所以,删除这一行: Storage::copy($targetPath . $original, $targetPath . $newName);
    4. 对于第 2 点和第 3 点,使用Image::make($file->getRealPath()); 这将创建文件并记住创建文件的路径。 Image->resize 方法稍后将使用此路径。
    5. 最后,将相对路径保存在数据库中,如这里:$profile->img = 'storage/uploads/'.$user_id.'/img/profile/'.$fName。由于我们将使用{{ asset($profile->img) }},因此只需要保存相对路径而不是绝对操作系统路径。

      if($request->hasFile('img')) {
      
          if($request->file('img')->isValid()) {
      
              $types = array('_original.', '_32.', '_64.', '_128.'); 
      
              $sizes = array( array('32','32'), array('64','64'), array('128','128'));
              $targetPath = '/Users/apple/Documents/_chayyo/chayyo/storage/app/public/uploads/'.$user_id.'/img/profile/';
      
          try {
              $file = $request->file('img');
              $ext = $file->getClientOriginalExtension();
              $fName = time();
              $o_name = $fName . array_shift($types) . $ext;
              $original = Image::make($file->getRealPath());
              $original->save($targetPath . $o_name);
      
              foreach ($types as $key => $type) {
                  $newName = $fName . $type . $ext;
                  $newImg = Image::make($file->getRealPath());
                  $newImg->resize($sizes[$key][0], $sizes[$key][1]);
                  $newImg->save($targetPath . $newName);
              }
      
              $profile->img = 'storage/uploads/'.$user_id.'/img/profile/'.$fName;
      
          } 
           catch (Illuminate\Filesystem\FileNotFoundException $e) {
      
          }
        }
      }
      

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我运行了这个命令并且它工作了

      php artisan storage:link
      

      此命令在公用文件夹下创建一个存储目录。

      同样使用公共路径函数获取公共路径

      $targetPath = public_path('storage/uploads/'. $user_id);
      

      laravel public_path() 函数内部使用的 'storage' 用于获取 storage 主文件夹。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-10
        • 1970-01-01
        • 2018-07-06
        • 1970-01-01
        • 1970-01-01
        • 2016-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多