晚上好,我想在我的网站上上传图像时添加图像的调整大小功能,我已经安装了必要的依赖项,并且在config / app文件中包括了提供程序和别名。但是我发现这个错误:
production.ERROR: Method Illuminate\Http\UploadedFile::resize does not exist.
我将部分代码放在下面:
public function imageProfile(Request $request)
{
$user = Auth::user();
$rules = array(
'profile-image' => 'required|image|mimes:jpeg,png,jpg,gif|max:8192|dimensions:min_width=160,min_height=160',
);
$customMessages = [
'profile-image.required' => 'E\' richiesta una immagine per cambiare immagine di profilo.',
'profile-image.image' => 'Devi inserire un immagine valida.',
'profile-image.mimes' => 'L\'immagine inserita non ha un formato adatto.',
'profile-image.dimensions' => 'L\'immagine deve essere minimo 160x160.',
];
$validator = Validator::make(Input::all(), $rules, $customMessages);
if ($validator->fails()) {
return response()->json(['success' => false, 'error' => $this->validationErrorsToString($validator->errors())]);
}
if ($request->hasFile('profile-image')) {
$number = mt_rand(1,1000000);
$image = $request->file('profile-image');
$name = $user->username.'-'.Carbon::now()->toDateString().'-'.$number.'.'.$image->getClientOriginalExtension();
$destinationPath = 'uploads/profile';
$imagePath = $destinationPath. "/". $name;
$image->move($destinationPath, $name);
$image->resize(200,200);
$user->image_profile = $imagePath;
$user->save();
$html = $imagePath;
return response()->json(['success' => true, 'html' => $html, 'image' => $imagePath]);
}
}
感谢您的帮助,祝您生活愉快
答案 0 :(得分:7)
Laravel没有图像的默认调整大小。但是大多数laravel开发人员在处理图像时都使用“图像干预”。 (易于使用)
要安装(图像干预):
STEP 1运行
composer require intervention/image
第2步在您的config / app.php中:
在$ providers数组中,添加以下内容:
Intervention\Image\ImageServiceProvider::class
在$ aliases数组中,添加以下内容:
'Image' => Intervention\Image\Facades\Image::class
如果您遇到问题,则缺少GD librabry
PHP5: sudo apt-get install php5-gd
PHP7: sudo apt-get install php7.0-gd
~~要在您的控制器上使用~~
STEP 3在控制器顶部
use Intervention\Image\ImageManagerStatic as Image;
步骤4关于您的方法(有几种方法,但这会给您一个想法)
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save(public_path('images/ServiceImages/' .$filename));
}