具有重命名和调整大小的laravel 5.6图像干预上传 [英] laravel 5.6 image intervention upload with rename and resize

查看:55
本文介绍了具有重命名和调整大小的laravel 5.6图像干预上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第1步安装:(撰写者需要干预/提供图片)

Step 1-Installation: (composer require intervention/image)

第2步配置:安装Intervention Image之后,打开您的Laravel配置文件config/app.php并添加以下几行.

Step 2-Configuration: After you have installed Intervention Image, open your Laravel config file config/app.php and add the following lines.

在$ providers数组中添加此程序包的服务提供者.Intervention \ Image \ ImageServiceProvider :: class

In the $providers array add the service providers for this package. Intervention\Image\ImageServiceProvider::class

将此程序包的外观添加到$ aliases数组中.

Add the facade of this package to the $aliases array.

'图像'=>干预\图像\外观\图像::类

'Image' => Intervention\Image\Facades\Image::class

第3步使用:使用图片

 public function avatar(Request $request){
    $user = new User();
    if($request->hasFile('image')) {
        if ($user->image){
            unlink(public_path('/image/user/').$user->image);
        }
        $image = $request->file('image');
        $imageName = $image->getClientOriginalName();
        $fileName = $userName . "_profile_". $userId . "_" . $imageName;

        $directory = public_path('/image/user/');
        $imageUrl = $directory.$fileName;
        Image::make($image)->resize(200, 200)->save($imageUrl);
        $user->image = $fileName;
    }

    if ($user->save())
        return redirect()->back()->with('success','Update successfully');

    return redirect()->back()->with('error', 'There is an error message');
}

推荐答案

如果您正在使用laravel 5.6,则不需要步骤2. composer需要干预/图像将安装在您的供应商文件夹和laravel软件包中发现将为您完成剩下的工作

step 2 is not needed if you are working on laravel 5.6 composer require intervention/image will install in your vendor folder and laravel package discovery will do the rest for you

这篇关于具有重命名和调整大小的laravel 5.6图像干预上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆