Let’s see, how to create a thumbnail in Laravel using the Intervention Image library. Intervention Image is free and one of the popular libraries for image manipulation. 

  1. Install the Intervention Image library in Laravel
composer require intervention/image

2. Open config/app.php file and add the following lines in it.

Intervention\Image\ImageServiceProvider::class

3.  To store the images we need to create a directory. This command creates a storage directory under the public folder.

php artisan storage:link

        if ($request->hasfile('teampic')) {
            $file = $request->file('teampic');
            $filenamewithextension =  $file->getClientOriginalName();
            $filename = time() . '-' . $filenamewithextension;
            $services->teampic =  $filename;

            $smallthumbnail = "team_" . $filename;
            $path = public_path('uploads/team/thumbnail');

            if (!File::isDirectory($path)) {
                File::makeDirectory($path, 0775, true, true);
            }
            //create small thumbnail
            $smallthumbnailpath = public_path('uploads/team/thumbnail/' . $smallthumbnail);

            if (!File::exists($smallthumbnailpath)) {
                $this->createThumbnail($request->file('teampic')->getRealPath(), $smallthumbnailpath, 400, 400);
            }
            $file->move(public_path() . '/uploads/team/', $filename);
        } else {
            $request->session()->flash('error', 'Upload the Team Image');
        }

        $services->update();
   public function createThumbnail($real_path, $path, $width, $height)
    {
        $img = Image::make($real_path)->resize($width, $height, function ($constraint) {
            $constraint->aspectRatio();
        });
        $img->save($path);
    }
use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;

Category: Laravel
Balbir KaurPHP and WordPress