Laravel 10 Image Resize & Upload with Intervention Image

10-Apr-2023

.

Admin

Laravel 10 Image Resize & Upload with Intervention Image

Hi friends,

Today, I am explaining laravel 10 images resize & upload with intervention image. we will help you to give an example of laravel 10 resizing the image before uploading. I will explain step by step tutorial laravel 10 to resize images. we will learn how to use the image intervention package with laravel 10. we will generate a thumbnail image on upload in laravel 10.

we will use intervention/image package for resizing or resizing images in laravel. the intervention provides a resize function that will take three parameters. three parameters are width, height, and callback function. the callback function is optional.

So, let's start with the following example:

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

Step 2: Install Intervention Image Package

In the second step, we will install intervention/image for resizing the image. this package through which we can generate thumbnail images for our project. so first fire the bellow command in your cmd or terminal:

composer require intervention/image

Step 3: Add Routes

In this step, we will add routes and a controller file so first add below 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: Add Controller File

Now require to create new ImageController for image upload and resize it, so first run the bellow command :

php artisan make:controller ImageController

After this command, you can find ImageController.php file in your app/Http/Controllers directory. open the ImageController.php file and put the bellow code in that file.

Make sure, you have created the "images" and "thumbnail" folders in the 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 an imageUpload.blade.php file for the photo upload form and manage the error messages and also the success message. So first create an imageUpload.blade.php file and put bellow code:

resources/views/imageUpload.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 10 Image Resize & Upload with Intervention Image - Nicesnippets.com</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 border mt-5">

<h3 class="text-center mt-3">Laravel 10 Image Resize & Upload with Intervention Image - Nicesnippets.com</h3>

@if (count($errors) > 0)

<div class="alert alert-danger mt-5">

<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/>

<label class="mb-1"><strong>Select Image:</strong></label>

<input type="file" name="image" class="image form-control">

</div>

<div class="col-md-12 mb-3">

<br/>

<button type="submit" class="btn btn-success">Upload Image</button>

</div>

</div>

</form>

</div>

</body>

</html>

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open a web browser, type the given URL and view the app output:

http://localhost:8000/image-upload

I hope it can help you...

#Laravel 10