This article illustrates Laravel 9 image intervention example. We will be using Laravel 9 intervention image upload example. Intervention Image is an open-source PHP image manipulation library. It provides an easier way to manipulate images. you can use the intervention/image library for PHP Project as well. You can do the following things with image using the Intervention Image composer package, here is a list of mostly used functions here:
- crop()
- blur()
- canvas()
- filter()
- fill()
- height()
- insert()
- width()
- make()
- reset()
- resize()
- save()
- text()
- rotate()
You should have two following extensions enable in your server.
Step 1: Install Laravel
If you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Install Intervention Image Package
Install the intervention/image to resize the image. Through this package, we can generate thumbnail images for our project. Use the below command in your cmd or terminal:
composer require intervention/image
If you are using Laravel 6 or the latter version, then add the following lines the in config/app.php File:
config/app.php
<?php return [ ... ... 'providers' => [ ... Intervention\Image\ImageServiceProvider::class ], 'aliases' => Facade::defaultAliases()->merge([ ... 'Image' => Intervention\Image\Facades\Image::class ]] ]
Step 3: Create Routes
Add routes and controller files using the route in your routes.php file.
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ImageController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::controller(ImageController::class)->group(function(){ Route::get('image-upload', 'index'); Route::post('image-upload', 'store')->name('image.store');
Step 4: Create Controller File
Now , create new ImageController for image upload and resize it, run the below command :
php artisan make:controller ImageController
After this command, you can find ImageController.php file in your app/Http/Controllers directory. Open ImageController.php file and put the below code in that file. Make sure, you have created two folders one is “images” in the public folder and another “thumbnail” inside the images folder.
app/Http/Controllers/ImageController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Image; class ImageController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('imageUpload'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); if($request->hasFile('image')) { $image = Image::make($request->file('image')); /** * Main Image Upload on Folder Code */ $imageName = time().'-'.$request->file('image')->getClientOriginalName(); $destinationPath = public_path('images/'); $image->save($destinationPath.$imageName); /** * Generate Thumbnail Image Upload on Folder Code */ $destinationPathThumbnail = public_path('images/thumbnail/'); $image->resize(100,100); $image->save($destinationPathThumbnail.$imageName); /** * Write Code for Image Upload Here, * * $upload = new Images(); * $upload->file = $imageName; * $upload->save(); */ return back() ->with('success','Image Upload successful') ->with('imageName',$imageName); } return back(); } }
Step 5: View File and Create Upload directory
Create imageUpload.blade.php file for photo upload form and manage error message and success message. So, firstly create imageUpload.blade.php file and use the below code.
resources/views/imageUpload.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel Image Intervention Example Tutorial </title> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <h1>Laravel Image Intervention Example Tutorial</h1> @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif @if ($message = Session::get('success')) <div class="alert alert-success alert-dismissible fade show" role="alert"> <strong>{{ $message }}</strong> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> <div class="row"> <div class="col-md-4"> <strong>Original Image:</strong> <br/> <img src="/images/{{ Session::get('imageName') }}" width="300px" /> </div> <div class="col-md-4"> <strong>Thumbnail Image:</strong> <br/> <img src="/thumbnail/{{ Session::get('imageName') }}" /> </div> </div> @endif <form action="{{ route('image.store') }}" method="post" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-md-12"> <br/> <input type="file" name="image" class="image"> </div> <div class="col-md-12"> <br/> <button type="submit" class="btn btn-success">Upload Image</button> </div> </div> </form> </div> </body> </html>
Run Laravel App
Type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, move to your web browser, type the given URL and view the app output:
Output