Press ESC to close

Manoj Bist

How To Resize Images in Laravel Before Uploading

Resize Images in Laravel

,

Resizing images in laravel requires you to install external libraries. There are many packages for image resize but we will be using the Intervention/image PHP library.

Here, I will be sharing, how to create resize an image in Laravel. For this article I am assuming, you already have installed Laravel and configured it to run on your local machine. Because I just want to make it short without taking your valuable time to resize an image without losing quality in Laravel.

So let’s just go to the point. and

Laravel provides an excellent file handling library, we can use that to handle our files like uploading, moving files to the desired location on your project. But what for resizing, let’s find out.

To Resize images in Laravel we use a package intervention/image. Let’s try that.

Let’s just download it

composer require intervention/image

After installing the library, we have to tell Laravel to load Interventions services, to do that we need to specify Intervention Service Provider in config/app.php


    	$provides => [
        	......,

            InterventionImageImageServiceProvider::class

        ],

        $aliases => [
            .....,

            'Image' => InterventionImageFacadesImage::class

        ]
    

I’m assuming you already have created Controller but no worries if not. You can do that by following the artisan command.

php artisan make:controller PostController --resource

Again, you might have added the route but don’t worry this is for those who have not yet.


    	use AppHttpControllersPostController;
        
    	Route::resource('posts', PostController::class)
    

Let’s create a view for uploading files. Save this file as views/upload.blade.php


    	<form action="{{route('posts.store')}}" method="post">>
        	@csrf
        	<input type="file" name="image">
            <input type="submit" name="save">
        </form>
    

Okay, let’s start the real game now, lets update our controller

I am just showing the create and store method of the controller


    	public function create()
        {
        	return view('upload');
        }
        
    	public function store(Request $request)
        {
            $this->validate($request, [
                
                'image' => 'required|image|mimes:jpg,jpeg,png,svg,gif|max:2048',
            ]);
			
            //get file from form
            
            $image = $request->file('image');
            
            //get image ext and create a custom file name
            
            $input['image'] = time().'.'.$image->extension();
			
            //prepare file path to store resized image
            
            $file_path = public_path('/thumbs');
			
            //make instance of intervention
            
            $img = Image::make($image->path());
            
            //resize image to 150x150 with aspect ratio and save
            
            $img->resize(150, 150, function ($instance) {
                $instance->aspectRatio();
            })->save($file_path.'/'.$input['image']);
			
            //upload the original file to uploads folder
            $file_path = public_path('/uploads');
            
            $image->move($file_path, $input['image']);

            return back()
                ->with('success','Upload success')
                ->with('file_name',$input['image']);
        }
    

Don’t forget to use the intervention package in your controller

use Image; //add this line to bellow namespace of your controller

Using the same intervention/image package, you can also perform bulk resize images. Just upload multiple files and use a loop when resizing and uploading in the above controller method.

That’s all. Happy Coding!

Manoj Bist

A Passionate Web Developer/Designer With over 6 years of experience in the industry, Manoj Bist is a seasoned professional who combines technical expertise with a down-to-earth demeanor. As a lover of both technology and design, he thrives on creating seamless digital experiences that captivate and inspire.

Leave a Reply

Your email address will not be published. Required fields are marked *