Laravel 9 Resize Image Before Upload

Websolutionstuff | Jun-15-2022 | Categories : Laravel PHP

In this article, we will see how to resize image before uploading in laravel 9. we will install the intervention/image composer package. intervention/image provides to resize image using resize() method also you can crop, rotate, compress, blur the image. In this example, you can learn how to resize the image in laravel 9.

So, let's see laravel 9 resize the image before upload, image resize in laravel 9, resize the image before upload in laravel 9, laravel intervention image resize, resize the image in laravel 9, laravel image resize, intervention image laravel 9, thumbnail image in laravel.

Step 1: Install Laravel 9

In this step, we will install the laravel 9 using the below command.

composer create-project laravel/laravel image-resize-example

 

 

Step 2: Install Intervention Image Package

Now, we will install the intervention/image package using the below command. intervention/image is very useful for resizing images and other actions performed on the image.

composer require intervention/image

 

Step 3: Create Routes

In this step, we will add routes in the web.php file.

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

In this step, we will create a new ImageController.

php artisan make:controller ImageController

 

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('image_upload');
    }
      
    /**
     * 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);
    }
}

Note: Make sure, you have created the "images" and "thumbnail" folder in the public folder.

 

 

Step 5: View File

Now, we will create the image_upload.blade.php file for uploading an image.

resources/views/image_upload.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Resize Image Before Upload - Websolutionstuff</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 9 Resize Image Before Upload - Websolutionstuff</h1>
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> something wants wrong.<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>

 


You might also like:

Recommended Post
Featured Post
How to Create Form Request Validation in Laravel 10
How to Create Form Request Val...

Hey there! Today, I want to talk to you about a super useful feature in Laravel 10 called form request validation. If yo...

Read More

Feb-23-2024

How To Get Client IP Address In Laravel 9
How To Get Client IP Address I...

In this article, we will see how to get a client's IP address in laravel 9. Many times you need a user IP addre...

Read More

Oct-26-2022

How to Create Trait in Laravel 10 Example
How to Create Trait in Laravel...

Hello developers! 👋 Today, let's dive into the wonderful world of Laravel 10 and explore one of its handy featu...

Read More

Feb-09-2024

How To Send Email With Attachment In Laravel 9
How To Send Email With Attachm...

In this article, we will see how to send email with attachments in laravel 9. As we all know mail functionalities are co...

Read More

Mar-16-2022