laravel 5.6 图像干预上传重命名和调整大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49810373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:37:54  来源:igfitidea点击:

laravel 5.6 image intervention upload with rename and resize

laravel

提问by Rashiqul Rony

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

步骤 1-安装:(作曲家需要干预/图像)

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

第 2 步-配置:安装干预映像后,打开 Laravel 配置文件 config/app.php 并添加以下行。

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

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

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

将此包的外观添加到 $aliases 数组中。

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

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

Step 3-Uses:use Image

第 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');
}

回答by Adedayo Sule-odu

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

如果您正在使用 laravel 5.6composer require intervention/image将安装在您的供应商文件夹中,则不需要第 2 步,而 laravel 包发现将为您完成剩下的工作