3

Good evening, I would like to add the resize function of the images when an image is uploaded on my site, I have installed the necessary dependencies and I have included in the config/app files the provider and the aliases. But I find this error: production.ERROR: Method Illuminate\Http\UploadedFile::resize does not exist. I put the part of the code below:

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

Thanks for help me, and have a good day

4
  • are you using intervention image package ? Commented Jun 20, 2019 at 12:55
  • there is no resize function available in the file upload object. Commented Jun 20, 2019 at 12:57
  • yes exactly @ kmg kumar. So he need to use image intervention Commented Jun 20, 2019 at 13:02
  • 1
    stackoverflow.com/questions/40358510/… may this link help you Commented Jun 20, 2019 at 13:13

1 Answer 1

7

Laravel does not have a default resize of image. But most laravel developers use 'Image intervention' in handling the image. (Easy to use)

To install (Image intervention):

STEP 1 Run

composer require intervention/image

STEP 2 On your config/app.php:

In the $providers array, add the following:

Intervention\Image\ImageServiceProvider::class

In the $aliases array,add the following:

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

If you have problems your GD librabry is missing, intall it

PHP5: sudo apt-get install php5-gd
PHP7: sudo apt-get install php7.0-gd

~~ To use on your controller ~~

STEP 3 On top of your controller

use Intervention\Image\ImageManagerStatic as Image;

STEP 4 On your method (there are several ways but this will give you an idea)

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.