Resize laravel 5 image file

I installed the "intervention / image", "must-master" fix to reduce the image to 300 by 300. I did some forms and always seemed the same error to me.

Calling the member size resizing function () in a string

who got the error?

controller

public function updateProfile() {

    $file = Input::file('imagem');
    $profileData = Input::except('_token');
    $validation = Validator::make($profileData, User::$profileData);
    if ($validation->passes()) {
        if ($file == null) {
            User::where('id', Input::get('id'))->update($profileData);
            Session::flash('message', 'Perfil editado com sucesso');
            return view('backend/perfil.index'); 
        }
        $file = array_get($profileData,'imagem');
        $destinationPath = 'imagens/perfil';
        $extension = $file->getClientOriginalExtension();
        $filename = rand(11111, 99999) . '.' . $extension;
        $reduzir = $filename -> resize (300,300); 
        $profileData['imagem'] = $filename;
        $upload_success = $file->move($destinationPath, $filename);


        User::where('id', Input::get('id'))->update($profileData);
        Session::flash('message', 'Perfil editado com sucesso');
        return Redirect::to('backend/perfil');
    } else {
        return Redirect::to('backend/perfil')->withInput()->withErrors($validation);
    }
}   
+4
source share
1 answer

The problem may be due to these reasons.

Have you added these aliases to your app.php

'aliases' => [
         //add these three at the bottom
        'Form'      => Illuminate\Html\FormFacade::class, 
        'HTML'      => Illuminate\Html\HtmlFacade::class,
        'Image'     => Intervention\Image\Facades\Image::class
],

I believe that you already have a form and an html helper.

And use this function in the controller

ie, just pass the image value and size as a parameter to this function

In the controller, you simply call the function below, for example

 $resizedImage  =   $this->resize($image, $request->get('image_size'));

resize()

private function resize($image, $size)
    {
        try 
        {
            $extension      =   $image->getClientOriginalExtension();
            $imageRealPath  =   $image->getRealPath();
            $thumbName      =   'thumb_'. $image->getClientOriginalName();

            //$imageManager = new ImageManager(); // use this if you don't want facade style code
            //$img = $imageManager->make($imageRealPath);

            $img = Image::make($imageRealPath); // use this if you want facade style code
            $img->resize(intval($size), null, function($constraint) {
                 $constraint->aspectRatio();
            });
            return $img->save(public_path('images'). '/'. $thumbName);
        }
        catch(Exception $e)
        {
            return false;
        }
+1

Source: https://habr.com/ru/post/1615926/


All Articles