Home Laravel Laravel 9 Image Intervention Tutorial

Laravel 9 Image Intervention Tutorial

by Shahriar Sagor

Hello Dev , Today now in this article i will show you laravel 9 image intervention example. Here i will use laravel 9 intervention image upload example. So if you have any question about how to use the image intervention in laravel 9 then here I will give a simple example with solution. 

Step 1: Install Laravel

At first of all we have to get the fresh Laravel version application by using the bellow command. So open your terminal OR command prompt and then run the bellow command:

composer create-project laravel/laravel example-app
Step 2: Install Intervention Image Package

Now in second step i will install intervention/image for the resize image. So using this package i can generate the thumbnail image for our project. So at first fire the bellow command in my cmd or terminal:

composer require intervention/image

If you are using the Laravel 6 or letter version then you have to add the following lines 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

Now in this step i will add routes and also controller file. So at first add the 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 have to create a new ImageController for image upload and resize it, So first run the bellow command :

php artisan make:controller ImageController

After this command we can find the ImageController.php file in our app/Http/Controllers directory. Now open ImageController.php file and then put the bellow code in that file.

Make sure, we have to created two folders one is “images” in public folder and then another “thumbnail” inside 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

Ok, now in this last step i will create the imageUpload.blade.php file for the photo upload form and then manage error message and then also success message. So at first create the imageUpload.blade.php file and put the bellow code:

resources/views/imageUpload.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Image Intervention Example Tutorial - CodingsPoint.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">
    <h1>Laravel Image Intervention Example Tutorial - CodingsPoint.com</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>

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like