resize image and upload example laravel intervention

  • 29-12-2022
  • 522
  • Laravel 9
  • Haresh Chauhan

Image resize using intervention image and upload as a thumbnail image in laravel application with examples in this post I will show you. using image intervention we will resize or fit the image and save it as thubnail image from the original image in the storage folder in the laravel application.

We will generate a thumbnail image form the original image using image intervention in these examples. also, you can just fix the height and width of the image only using resize() image intervention method.

make the image resize and generate a thumbnail image form the original image and we will upload it to the storage of the application. I have described all details post in below from scratch. starting form the installation and configuration and the resized image generate.

resize the current image based on the given height and width, resize() stretches the image to the provided desired aspect ratio

Step 1. Install Composer

Install composer using the below-provided command, this command will import the core GD library for processing all the images.

composer require intervention/image

Step 2. Configuration

Register the Image server provider in the config/app.ph file under the provider attributes just like below given example. Also, register the global class name in the aliases attribute in the same file.

'providers' => [
    Intervention\Image\ImageServiceProvider::class
]
'aliases' => [
    'Image' => Intervention\Image\Facades\Image::class
]

By default, the intervention image uses the GD library to process all image extensions. if you wish to switch to Imagick, you can publish the configuration file into your application using the below-provided command in the command prompt.

This configuration file will help you to change by default configuration change in image intervention in the application.

php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"

Step 3. Image Resize

Now import the image class at the top of the class in which you are using the image intervention class, like the controller. here I am making just an example of how to do image resize using a route file.

Example 1

Route::get('/', function() {
    $img = Image::make('foo.jpg')->resize(300, 200);
    return $img->response('jpg');
});

Example 2

In this example first original image will be uploaded to the application storage. After the original image successfully uploads Image intervention will create/generate a thumbnail image from that original image upload, and then it will upload in the same folder path with the thumb prefix image name from the original image name.

function request imageUpload(Request $request)
{
    $storepath = \Storage::disk('public')->path('product');
        
    $imgname = time().'-'.\Str::random(5).'.'.$image->getClientOriginalExtension();
    
    $request->image->move($storepath,$imgname);
    
    Image::make($storepath.'/'.$imgname)->fit(100,100)->save($storepath.'/'.'thumb-'.$imgname);
    
    return $path.'/'.$imgname;
}

Example 3

If you wish to resize an image with the given fixed size use the below example.

// create instance
$img = Image::make('public/foo.jpg');

// resize image to fixed size
$img->resize(300, 200);

Example 4

If you wish to adjust only the height or width of the image, just give only the required image size parameter. the rest param will be null instead of giving any value in the param.

// create instance
$img = Image::make('public/foo.jpg');

// resize only the width of the image
$img->resize(300, null);

// resize only the height of the image
$img->resize(null, 200);

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :