This tutorial shows you laravel 9 resize image before upload example. This post will give you simple example of laravel 9 generate thumbnail image. you will learn laravel 9 resize image before upload. In this article, we will implement a laravel 9 image intervention example. Let's get started with resize and upload image in laravel 9.
In this example, we will install the intervention/image composer package. intervention/image provides to resize image using resize() method. we will create a simple form with the input field. you can choose an image and then you will see a preview of the original and thumbnail images.
So, let's follow the below steps to generate a thumbnail image in the laravel 9 application.
Step 1: Install Laravel 9
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
In second step we will install intervention/image for resize image. this package through we can generate thumbnail image for our project. so first fire bellow command in your cmd or terminal:
In this step we will add routes and controller file so first add bellow 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 require to create new ImageController for image upload and resize it, so first run bellow 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 bellow code in that file.
Make sure, you have created "images" and "thumbnail" folder in public 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', ]); $image = $request->file('image'); $imageName = time().'.'.$image->extension(); $destinationPathThumbnail = public_path('/thumbnail'); $img = Image::make($image->path()); $img->resize(100, 100, function ($constraint) { $constraint->aspectRatio(); })->save($destinationPathThumbnail.'/'.$imageName); $destinationPath = public_path('/images'); $image->move($destinationPath, $imageName); return back() ->with('success','Image Upload successful') ->with('imageName',$imageName); }}
Step 5: View File and Create Upload directory
Ok, in this last step we will create imageUpload.blade.php file for photo upload form and manage error message and also success message. So first create imageUpload.blade.php file and put bellow code: