Laravel Image Upload And Resize Made Easy With Intervention Image

Laravel is a popular PHP framework that is widely used for developing web applications. One common task when working with images in a Laravel application is uploading and resizing images. In this blog post, we'll show you how to do this using the Intervention Image package.


The Intervention Image package is a PHP image handling and manipulation library that provides an easy-to-use interface for interacting with images. It offers a variety of features such as image resizing, cropping, and rotating, as well as the ability to apply filters and effects.


To get started with the Intervention Image package, you'll need to install it using Composer. Open up your terminal and navigate to your Laravel project directory, then run the following command:


composer require intervention/image


Next, you'll need to add the service provider and alias to your config/app.php file. Add the following lines to the providers array:


Intervention\Image\ImageServiceProvider::class


And add the following line to the aliases array:


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


Now you're ready to use the Intervention Image package in your Laravel application. To upload and resize an image, you'll need to use the Image facade to open the image file and then call the resize method to specify the new width and height. For example:


$image = Image::make($request->file('image'))->resize(300, 200);


You can then save the image to a specified location on your server using the save method. For example:


$image->save(public_path('images/' . $filename));


That's it! With just a few lines of code, you can easily upload and resize images in your Laravel application using the Intervention Image package.


To see this example in action, let's create a form that allows users to select and upload an image file. In your view file, add the following HTML code:


<form method="POST" action="/upload" enctype="multipart/form-data">
    @csrf
    <input type="file" name="image">
    <button type="submit">Upload Image</button>
</form>


Next, create a route and controller method to handle the image upload. In your routes/web.php file, add the following route:


Route::post('/upload',[ImageController::class, 'upload']);


Then, create an ImageController controller with the upload method like this:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

class ImageController extends Controller
{
    public function upload(Request $request)
    {
        // Validate the request and retrieve the uploaded image file
        $request->validate([
            'image' => 'required|image|max:2048'
        ]);
        $image = $request->file('image');

        // Generate a unique filename
        $filename = uniqid() . '.' . $image->getClientOriginalExtension();

        // Open the image file and resize it to 300x200 pixels
        $image = Image::make($image)->resize(300, 200);

        // Save the resized image to the public/images directory
        $image->save(public_path('images/' . $filename));

        // Redirect the user back to the form with a success message
        return redirect()->back()->with('success', 'Image uploaded and resized successfully!');
    }
}


This controller includes an upload method that handles the image upload and resizing process. It first validates the request to ensure that only valid image files are uploaded and that they are not too large. It then generates a unique filename for the uploaded image to prevent conflicts with existing files. Finally, it uses the Image facade to open the image file, resize it, and save it to the public/images directory.


I hope this helps you understand how to use the Intervention Image package to handle image uploads and resizing in a Laravel application.