上传到 Laravel 5.2 后调整图像大小出现问题。

3

晚上好,我想在我的网站上传图片时添加图像调整大小的功能,我已经安装了必要的依赖项,并在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]);
        }
    }

感谢您的帮助,祝您有美好的一天


你是否正在使用Intervention Image包? - Kmg Kumar
文件上传对象中没有可用的调整大小功能。 - Kmg Kumar
是的,完全正确 @ kmg kumar。所以他需要使用图像处理技术。 - ManojKiran A
1
可能这个链接可以帮助您:https://dev59.com/DlkS5IYBdhLWcg3wHDLU - Uddyan Semwal
1个回答

7

Laravel没有默认的图片大小调整功能。但是大多数Laravel开发人员在处理图像时使用“Image intervention”(易于使用)。

要安装(Image intervention):

步骤1 运行

composer require intervention/image

步骤2 在您的config/app.php文件中:
在$providers数组中添加以下内容:
Intervention\Image\ImageServiceProvider::class

在 $aliases 数组中添加以下内容:
'Image' => Intervention\Image\Facades\Image::class

如果您遇到 GD 库缺失的问题,请安装它。
PHP5: sudo apt-get install php5-gd
PHP7: sudo apt-get install php7.0-gd

~~ 在您的控制器上使用 ~~

第三步,在您的控制器顶部

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));

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接