Laravel 9 Resize Image Before Upload

    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
Read Also: How To Use Google Recaptcha V2 In Laravel 9
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');
});
Read Also: Angular 13 Crop Image Before Upload With Preview
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.

Read Also: Laravel 9 Image Upload In Summernote Editor
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:

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan