-1.6 C
New York
Wednesday, February 1, 2023

Laravel – Grayscale Image Generate Example from scratch

Today, i am going to share with you how to make grayscale image from original in laravel 6, laravel 7 and laravel 8 application.

Sometimes, we may require to make grayscale image when user upload image. grayscale image looks better if you have an good layout website. in article we will learn how to create grayscale image using intervention package in laravel. intervention package provide a lots of things to do with image like resize, Grayscale, write on image etc. intervention package is also perfect to generate grayscale image.

So, let’s follow few step to generate grayscale image and upload it to our server. You can make grayscale image as like bellow preview, i added one originally image and another grayscale image looks.

Original Image:

Grayscale Image:

Step 1: Install Laravel 5 Application

In this step, if you haven’t laravel 5 application setup then we have to get fresh laravel 5 application. So run bellow command and get clean fresh laravel 5 application.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install Intervention Package

In this step we will install intervention/image for generate grayscale image. this package through we can generate grayscale image for our project. so first fire bellow command in your cmd or terminal:

composer require intervention/image

Now we need to add provider path and alias path in config/app.php file so open that file and add bellow code.


return [


$provides => [





$aliases => [



'Image' => 'Intervention\Image\Facades\Image'



Step 3: Add Route

In second step we will add routes and controller file so first add bellow route in your routes.php file.


Route::get('grayscaleImage', 'ImageController@grayscaleImage');


Step 4: Add Controller

Now require to create new ImageController for image uploading and make grayscale image so first run bellow command :

php artisan make:controller ImageController

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



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Image;

class ImageController extends Controller



* Show the form for creating a new resource.


* @return \Illuminate\Http\Response


public function grayscaleImage()


return view('grayscaleImage');



* Show the form for creating a new resource.


* @return \Illuminate\Http\Response


public function grayscaleImagePost(Request $request)


$this->validate($request, [

'title' => 'required',

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',


$image = $request->file('image');

$input['imagename'] = time().'.'.$image->getClientOriginalExtension();

$destinationPath = public_path('/images');

$img = Image::make($image->getRealPath());



Write Your Insert data code


return back()

->with('success','Image Upload successful')




Step 5: Blade and Create Upload directory

Ok, in this last step we will create grayscaleImage.blade.php file for photo upload form and manage error message and also success message. So first create grayscaleImage.blade.php file and put bellow code:




<div class="container">

<h1>Grayscale Image Uploading Demo</h1>

@if (count($errors) > 0)

<div class="alert alert-danger">

<strong>Whoops!</strong> There were some problems with your input.<br><br>


@foreach ($errors->all() as $error)

<li>{{ $error }}</li>





@if ($message = Session::get('success'))

<div class="alert alert-success alert-block">

<button type="button" class="close" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>


<div class="row">

<div class="col-md-4">

<strong>Grayscale Image:</strong>


<img src="/images/{{ Session::get('imageName') }}" width="500px" />




{!! Form::open(array('route' => 'grayscaleImagePost','enctype' => 'multipart/form-data')) !!}

<div class="row">

<div class="col-md-4">

<strong>Add Title:</strong>

{!! Form::text('title', null,array('class' => 'form-control','placeholder'=>'Add Title')) !!}


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

<strong>Add Image:</strong>

{!! Form::file('image', array('class' => 'image')) !!}


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


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



{!! Form::close() !!}



Ok, at last create directory in your public folder images and please give permission to that folder and check….

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:


I hope it can help you…

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles