【发布时间】: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