我有一个关于给定图像的大小调整过程的小问题,我正在尝试提交一个包含输入类型的表单 - >文件< - 我能够在不调整大小的情况下上传图片,之后我决定调整大小图像所以我使用以下方法安装了干预图像库:
composer require intervention/image
然后我将库集成到我的Laravel框架中
Intervention\Image\ImageServiceProvider::class
'Image' => Intervention\Image\Facades\Image::class
最后我配置如下
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
我的控制器如下
<?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());
}
}
但不是调整pic的大小,而是抛出以下异常
NotReadableException in AbstractDecoder.php line 302:
Image source not readable
不应该 Image::make($file->getRealPath())
代替 Image::make('public/uploads/', $file->getRealPath())
?
Image::make()
似乎没有两个参数,所以这可能是你的问题。
尝试这个:
$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());
或者,如果您想在不先移动文件的情况下执行此操作,请尝试以下操作:
$file = Input::file('file');
$img = Image::make($file)
->resize(320, 240)
->save('public/uploads/', $file->getClientOriginalName());
在 L5.2
它不能直接从中获取图像 Input
正面。为此,我们需要首先将图像存储在服务器上然后提供路径 Image
门面对图像进行操作。
代码就像这样:
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
正面。如果你们中的任何人有直接解决方案,请在此处显示您的代码,我将奖励您这笔赏金。干杯。
在保存之前上传文件并调整其大小就像这样简单:
(没有验证或检查)
您可以直接将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))
}
如果要保存原始大小和调整大小的图像:
$img->save(public_path('uploads/'.$filename))
->resize(320, 240)
->save(public_path('uploads/thumb_'.$filename));
这只是在目前最新的5.2版本上进行了测试,版本为5.2.45
[编辑:]
如果你打电话
$file->move();
不要用
$file->getRealPath()
之后,因为这会在调用move()后返回false
$filename = $file->getClientOriginalName();
$file->move('uploads', $filename);
dd($file->getRealPath());
移动图像后调整图像大小时会出现此问题
$file->move('uploads', $fileName);
$file->getRealPath()
将返回 false
移动图像后。您需要在移动过程之前调整图像大小。而已 ;)
$img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());
$file->move('uploads', $fileName);