Php Laravel 5.2-干预图像中的图像源不可读
关于给定图像的大小调整过程,我有一个小问题,我正在尝试提交一个包含输入类型-->文件的表单是否应该是Php Laravel 5.2-干预图像中的图像源不可读,php,laravel-5.2,intervention,Php,Laravel 5.2,Intervention,关于给定图像的大小调整过程,我有一个小问题,我正在尝试提交一个包含输入类型-->文件的表单是否应该是image::make($file->getRealPath())而不是image::make('public/uploads/,$file->getRealPath()) 似乎不需要两个论点,所以这可能是你的问题 试试这个: 或者,如果不想先移动文件,请尝试以下操作: 在L5.2中,无法直接从输入获取图像。为此,我们需要首先将映像存储在服务器上,然后给出进入imagefacade的路径,以便
image::make($file->getRealPath())
而不是image::make('public/uploads/,$file->getRealPath())
似乎不需要两个论点,所以这可能是你的问题
试试这个:
或者,如果不想先移动文件,请尝试以下操作:
在
L5.2
中,无法直接从输入获取图像。为此,我们需要首先将映像存储在服务器上,然后给出进入image
facade的路径,以便对映像执行操作
代码如下:
if ($request->hasFile('picture') ) {
$destinationPath = public_path('uploads/user');
$photoname = date("YmdHis");
$file_extention = '.'.$request->file('picture')->getClientOriginalExtension();
$photo = $photoname.$file_extention;
$file_check = $request->file('picture')->move($destinationPath, $photo);
$thumb_path = $destinationPath.'/thumbnail/'.$photo;
$new_filePath = $destinationPath.'/'.$photo;
$assets_path = url('uploads/user/');
$img = Image::make($assets_path.'/'.$photo)->fit(100)->save($thumb_path,40);
$data['picture'] = $photo;
}
我正在寻找直接的解决方案,也就是说,以前可以直接从Input
facade拍摄图像。如果你们中有人有直接的解决方案,在这里显示你们的代码,我将奖励你们这笔赏金。干杯。上传文件并在保存前调整其大小非常简单:
(未经验证或检查)
您可以直接将UploadedFile的实例传递给InterventionImage::make()
如果要保存原始大小和已调整大小的图像:
$img->save(public_path('uploads/'.$filename))
->resize(320, 240)
->save(public_path('uploads/thumb_'.$filename));
这是在目前最新的5.2版本is 5.2.45上测试的
[编辑:]
如果你打电话
$file->move();
不要使用
$file->getRealPath()
之后,因为调用move()后将返回false
移动图像后调整图像大小时出现此问题
$file->move('uploads',$fileName)代码>
移动图像后,$file->getRealPath()
将返回false
。在移动过程之前,需要调整图像的大小。就这样;)
当您不想使用名称命名上载的文件时,也会出现此问题
$filename=$file->getClientOriginalName()代码>
方法。如果你想创建你自己上传的文件名,比如说,使用下面的方法
// $filename_without_ext = $request->input('name');
$filename_without_ext = Str::slug($request->input('name'));
$file_extension = pathinfo($logo->getClientOriginalName(), PATHINFO_EXTENSION);
$filename = time() . '-' . $filename_without_ext . '.' . $file_extension;
如果您使用的是Laravel5.8,并且出现了这个错误,并且您正试图创建自己的文件名(这是非常必要的),那么您可能会落入这个陷阱。您应该检查名称字段是否作为表单输入。例如,对于上面的代码,如果您不使用Str::slug函数,这是一个Laravel辅助函数,就像带有注释的代码一样,您可能会遇到问题,因为表单字段可能有空白。我刚刚解决了这个问题
更改此行:
$file -> move('uploads', $fileName);
到
现在$file->getRealPath()
有一个有效值
希望这对你有用。不,我已经试过了<代码>图像::make($file->getRealPath())
或图像::make($file)
给出错误,因为图像源不可读
。它在Laravel5中工作,但在L5.2中不工作。您是否通过手动查看目录来检查文件是否确实存在?另外,试着使用调试器跟踪代码执行,看看到底发生了什么。@Tarunn我在这个答案中对此有解释,这似乎是原始海报已经在做的事情,显然它不起作用。你能检查一下吗?这段代码中的问题是,$file->getRealPath()如果之前有对$file->move()的调用,则始终返回false可能是因为您没有权限(chmod 600)?或者php.ini->php\u value post\u max\u size(可能图像太大)?
$file = Input::file('file');
$img = Image::make($file)
->resize(320, 240)
->save('public/uploads/', $file->getClientOriginalName());
if ($request->hasFile('picture') ) {
$destinationPath = public_path('uploads/user');
$photoname = date("YmdHis");
$file_extention = '.'.$request->file('picture')->getClientOriginalExtension();
$photo = $photoname.$file_extention;
$file_check = $request->file('picture')->move($destinationPath, $photo);
$thumb_path = $destinationPath.'/thumbnail/'.$photo;
$new_filePath = $destinationPath.'/'.$photo;
$assets_path = url('uploads/user/');
$img = Image::make($assets_path.'/'.$photo)->fit(100)->save($thumb_path,40);
$data['picture'] = $photo;
}
public function upload(Request $request)
{
$file = $request->file('file');
$filename = $file->getClientOriginalName();
$img = \Image::make($file);
$img->resize(320, 240)->save(public_path('uploads/'.$filename))
}
$img->save(public_path('uploads/'.$filename))
->resize(320, 240)
->save(public_path('uploads/thumb_'.$filename));
$file->move();
$file->getRealPath()
$filename = $file->getClientOriginalName();
$file->move('uploads', $filename);
dd($file->getRealPath());
$img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());
$file->move('uploads', $fileName);
// $filename_without_ext = $request->input('name');
$filename_without_ext = Str::slug($request->input('name'));
$file_extension = pathinfo($logo->getClientOriginalName(), PATHINFO_EXTENSION);
$filename = time() . '-' . $filename_without_ext . '.' . $file_extension;
$file -> move('uploads', $fileName);
$file = $file -> move('uploads', $fileName);