Laravel Intervention Image

  • 14-04-2022
  • 1514
  • Laravel 6
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Hi, Artisan

Hello Friends,In this post you are learn how to create Intervention Image in laravel as well as How to fit image and resizing image in laravel with custom size with using Intervention Image Library.

It provides an easier and expressive way to create, edit, and compose images and supports currently the two most common image processing libraries and It will help to create a custom image with custom height and width or resizing do many more.

Before you Start Installation below Configuration must Required

Following Intervention Image requires components to work correctly.

  • PHP >= 5.4
  • Fileinfo Extension

And one of the following image libraries.

  • GD Library >=2.0
  • or
  • Imagick PHP extension >=6.5.7

Getting start Installation

  • Install your Laravel Project
  • Set MySQL database
  • Install Intervention Image Package
  • Set Intervention Image Configuration
  • Make Controller

Step 1 : Install your Laravel Project

Open your command prompt and fire this below command.

composer create-project --prefer-dist laravel/laravel Intervention Image

After, clone laravel project directory we move on 2nd step.

Step 2 : Set MySQL database

Here we must need to configure .env file so let's do that.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE='interventionimage'
DB_USERNAME=root
DB_PASSWORD='Set your password'

Now we follow below command for migrate database.

php artisan migrate

Step 3 : Install Intervention Image Package

Open Your Command Prompt and go on Your Exist Project and Put the following command it will take 5-10 minutes of installation.

composer require intervention/image

Using above command we install Intervention Image library in your project.

How to use Date Format Validation in Laravel?

Step 4 : Set Intervention Image Configuration

After you have installed Intervention Image, open your Laravel config file config/app.php and add the following lines.

config/app.php

Here, we need to set Intervention Image library in our laravel project directory config file into provides alias.

'providers' => [
  Intervention\Image\ImageServiceProvider::class
];

'aliases' => [
  'Image' => Intervention\Image\Facades\Image::class
];

After put above Providers laravel will auto-loaded Image Class

Put above line into right place.

We have installed package successfully.

Now we are ready to make image.

Note : Before Use you have to put Define usage like Use Image Above Of the Controller without defined it will not work.

Step 5 : Create Controller

We have need to make Controller in your Project by following command.

php artisan make:controller ImageController.

Step 6 : Create blade file

resource/view/image.blade.php

<form action="{{ route('image') }}" method="post" enctype="multipart/form-data">
  @csrf
  <input type="file" name="image">
  <button type="submit">Create Image</button>
</form>

How to Add Bootstrap in Angular 8 | Install Bootstrap 4 in Angular 8

Step 7 : Create Route

Route::get('/image/form','ImageController@form')->name('image.form');
Route::post('/image','ImageController@imageCreate')->name('image');

Step : 8 Go To controller

app/Http/Controllers/ImageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Storage;
use Image;
class ImageController extends Controller
{
  public function form()
  {
    return view('image');
  }

  public function imageCreate(Request $request)
  {
    $this->validate($request,[
      'image' => 'required|mimes:jpeg,png,jpg,gif|max:2024',
    ]);

    if($request->has('image')){

      $image =  $request->image;
      $path = Storage::disk('public');
      $width = 800;
      $height = 600;
      $name = time().'.'.$image->getClientOriginalExtension();
     \Image::make($image->getRealPath())->fit($width,$height)->save($path.'/'.$name);
    }    

    return back()->with('success', 'Your images has been successfully Upload');
  }
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :