php 图像源在 Laravel 5.2 中不可读 - 干预图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38923863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image source not readable in Laravel 5.2 - Intervention Image
提问by KaldoLeb
I have a small problem concerning the resizing process of a given image, I am trying to submit a form containing an input type -->file<-- I was able to upload a picture without resizing it, after that I decided to resize that image so I installed the Intervention Image Library using:
我有一个关于给定图像的大小调整过程的小问题,我正在尝试提交一个包含输入类型的表单 -->file<-- 我能够上传图片而不调整它的大小,之后我决定调整它的大小图像,所以我使用以下方法安装了干预图像库:
composer require intervention/image
then I integrated the library into my Laravel framework
然后我将库集成到我的 Laravel 框架中
Intervention\Image\ImageServiceProvider::class
'Image' => Intervention\Image\Facades\Image::class
and finally I configured it like following
最后我配置如下
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
my controller is like the following
我的控制器如下
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Image;
class ProjectController extends Controller{
public function project(Request $request){
$file = Input::file('file');
$fileName = time().'-'.$file->getClientOriginalName();
$file -> move('uploads', $fileName);
$img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());
}
}
but instead of resizing the pic the following exception is throwed
但不是调整图片大小,而是抛出以下异常
NotReadableException in AbstractDecoder.php line 302:
Image source not readable
回答by borfast
Shouldn't it be Image::make($file->getRealPath())
instead of Image::make('public/uploads/', $file->getRealPath())
?
不应该是Image::make($file->getRealPath())
代替Image::make('public/uploads/', $file->getRealPath())
吗?
Image::make()
doesn't seem to take two arguments, so that could be your problem.
Image::make()
似乎没有两个论点,所以这可能是你的问题。
Try this:
尝试这个:
$file = Input::file('file');
$fileName = time() . '-' . $file->getClientOriginalName();
$file->move('uploads', $fileName);
$img = Image::make($file->getRealPath())
->resize(320, 240)
->save('public/uploads/', $file->getClientOriginalName());
Or if you want to do it without moving the file first, try this:
或者,如果您想在不移动文件的情况下执行此操作,请尝试以下操作:
$file = Input::file('file');
$img = Image::make($file)
->resize(320, 240)
->save('public/uploads/', $file->getClientOriginalName());
回答by Tarunn
In L5.2
its not directly possible to get image from Input
facade. For that we need to first store the image on server and then give path into Image
facade to do operations on image.
因为L5.2
它不能直接从Input
门面获取图像。为此,我们需要首先将图像存储在服务器上,然后将路径提供给Image
Facade 以对图像进行操作。
Code goes likes this:
代码是这样的:
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;
}
I was looking for direct solution i.e. as it was possible previously to take image directly from Input
facade. If anyone of you have direct solution, show your code here and I'll reward you this bounty. Cheers.
我正在寻找直接的解决方案,即因为以前可以直接从Input
立面拍摄图像。如果你们中的任何人有直接的解决方案,请在此处展示您的代码,我将奖励您这个赏金。干杯。
回答by shock_gone_wild
Uploading a file and resizing it before saving is as easy as that: (Without validation or checks)
在保存之前上传文件并调整其大小就这么简单:(无需验证或检查)
You can directly pass an instance of UploadedFile to InterventionImage::make()
您可以直接将 UploadedFile 的实例传递给 InterventionImage::make()
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))
}
If you want to save original size and resized image:
如果要保存原始大小和调整大小的图像:
$img->save(public_path('uploads/'.$filename))
->resize(320, 240)
->save(public_path('uploads/thumb_'.$filename));
This was just tested on currently latest 5.2 version with is 5.2.45
这刚刚在当前最新的 5.2 版本上进行了测试,版本为 5.2.45
[EDIT:]
[编辑:]
If you call
如果你打电话
$file->move();
Don't use
不要使用
$file->getRealPath()
afterwards, because this will return false after calling move()
之后,因为这将在调用 move() 后返回 false
$filename = $file->getClientOriginalName();
$file->move('uploads', $filename);
dd($file->getRealPath());
回答by Mycodingproject
This problem can also occur when you don't want to name the uploaded file using
当您不想使用命名上传的文件时,也会出现此问题
$filename = $file->getClientOriginalName();
$filename = $file->getClientOriginalName();
method. If you want to create your own uploaded file names, let's say, using the method below
方法。如果你想创建自己上传的文件名,比如说,使用下面的方法
// $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;
If you use Laravel 5.8 and you get that error and you are trying to create your own file name, which is mostly necessary, you can fall into this trap. You should check the name field if it is coming as a form input. For example for the code above if you don't use the Str::slug function, which is a Laravel helper function, like the code with comments, you can run into problems because of the white space that the form field can have.
如果您使用 Laravel 5.8 并且遇到该错误,并且您正在尝试创建自己的文件名(这在大多数情况下是必需的),您可能会陷入这个陷阱。如果它作为表单输入出现,您应该检查名称字段。例如,对于上面的代码,如果您不使用 Str::slug 函数,它是 Laravel 辅助函数,就像带有注释的代码一样,由于表单字段可以有空格,您可能会遇到问题。
回答by Saeed
i just solved this problem.
我刚刚解决了这个问题。
change this line:
改变这一行:
$file -> move('uploads', $fileName);
to
到
$file = $file -> move('uploads', $fileName);
now $file->getRealPath()
has a valid value.
现在 $file->getRealPath()
有一个有效的值。
hope this works for you.
希望这对你有用。
回答by Abdalla Arbab
This problem occurred when you resize the image after moving it
移动后调整图像大小时出现此问题
$file->move('uploads', $fileName);
$file->move('uploads', $fileName);
$file->getRealPath()
will return false
after moving the image. You need to resize the image before the moving process. That's it ;)
$file->getRealPath()
将false
在移动图像后返回。您需要在移动过程之前调整图像大小。就是这样 ;)
$img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());
$file->move('uploads', $fileName);