Laravel Grayscale Image Generate Example

By Hardik Savani April 16, 2024 Category : Laravel

Today, i am going to share with you how to make grayscale image from original in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 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.

config/app.php

return [

......

$provides => [

......

......,

'Intervention\Image\ImageServiceProvider'

],

$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.

routes/web.php

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

Route::post('grayscaleImagePost',['as'=>'grayscaleImagePost','uses'=>'ImageController@grayscaleImagePost']);

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.

app/Http/Controllers/ImageController.php

<?php


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());

$img->greyscale()->save($destinationPath.'/'.$input['imagename']);


/*

Write Your Insert data code

*/


return back()

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

->with('imageName',$input['imagename']);

}


}

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:

resources/views/grayscaleImage.blade.php

@extends('layouts.app')


@section('content')


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

<ul>

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

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

@endforeach

</ul>

</div>

@endif


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


<div class="row">

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

<strong>Grayscale Image:</strong>

<br/>

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

</div>

</div>

@endif


{!! 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>


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

<strong>Add Image:</strong>

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

</div>


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

<br/>

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

</div>


</div>

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


</div>


@endsection

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:

http://localhost:8000/grayscaleImage

I hope it can help you...

Shares