Laravel – Grayscale Image Generate Example from scratch
In this post we will give you information about Laravel – Grayscale Image Generate Example from scratch. Hear we will give you detail about Laravel – Grayscale Image Generate Example from scratchAnd how to use it also give you demo for it if it is necessary.
Today, i am going to share with you how to make grayscale image from original in laravel 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 => [
......
......,
'InterventionImageImageServiceProvider'
],
$aliases => [
.....
.....,
'Image' => 'InterventionImageFacadesImage'
]
]
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 AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use Image;
class ImageController extends Controller
{
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function grayscaleImage()
{
return view('grayscaleImage');
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
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 >
<h1>Grayscale Image Uploading Demo</h1>
@if (count($errors) > 0)
<div >
<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 >
<button type="button" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
<div >
<div >
<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 >
<div >
<strong>Add Title:</strong>
{!! Form::text('title', null,array('class' => 'form-control','placeholder'=>'Add Title')) !!}
</div>
<div >
<strong>Add Image:</strong>
{!! Form::file('image', array('class' => 'image')) !!}
</div>
<div >
<br/>
<button type="submit" >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…
Hope this code and post will helped you for implement Laravel – Grayscale Image Generate Example from scratch. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs