Laravel 7 create and upload Grayscale Image with example – onlinecode
In this post we will give you information about Laravel 7 create and upload Grayscale Image with example – onlinecode. Hear we will give you detail about Laravel 7 create and upload Grayscale Image with example – onlinecodeAnd how to use it also give you demo for it if it is necessary.
In this tutorial, we will show to you how to create and upload Grayscale Image with example in laravel 7. If you want to create a greyscale image from an original image in your laravel application then you can create using “intervention/image” package.
We will also explain in laravel 7 create and upload grayscale image with example. so you can also learn and use of laravel intervention image package.
So let’s start the creation of a greyscale image with the following steps.
Overview
Step 1: Install Laravel
Step 2: Install intervention/image Package
Step 3: Add providers and aliases
Step 4: Create Route
Step 5: Create a Controller
Step 6: Create Blade Files
Step 7: Run Our Laravel Application
Step 1: Install Laravel
We are going to install laravel 7, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
composer create-project --prefer-dist laravel/laravel laravel7_dompdf
Step 2: Install intervention/image Package
Now, We will install intervention/image package using below command.
composer require intervention/image
Step 3: Add providers and aliases
We will add below providers and aliases in the “config/app.php” file.
'providers' => [ .... 'InterventionImageImageServiceProvider' ], 'aliases' => [ .... 'Image' => 'InterventionImageFacadesImage' ]
Step 4: Create Route
Add the following route code in the “routes/web.php” file.
<?php /* |-------------------------------------------------------------------------- | 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::get('/', function () { // return view('welcome'); }); Route::get('gray-scale-image', 'ImageController@index'); Route::post('gray-scale-image',['as'=>'gray.scale.image','uses'=>'ImageController@grayScaleSaveImage']); ?>
Step 5: Create a Controller
Here below command help to create the controller and model.
php artisan make:controller ImageController
ImageController.php
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppHttpRequests; use Image; class ImageController extends Controller { public function index() { return view('viewGrayscaleImage'); } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ public function grayScaleSaveImage(Request $request) { $this->validate($request, [ 'title' => 'required', 'imagefile' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); $image = $request->file('imagefile'); $input['image_name'] = time().'.'.$image->getClientOriginalExtension(); $destinationPath = public_path('/uploads'); $img = Image::make($image->getRealPath()); $img->greyscale()->save($destinationPath.'/'.$input['image_name']); return back() ->with('success','Image Uploaded successfully.') ->with('image_name',$input['image_name']); } }
Step 6: Create Blade Files
Finally, We will create viewGrayscaleImage.blade.php file in the “resources/views/” folder directory and paste below code.
viewGrayscaleImage.blade.php
<html lang="en"> <head> <title>Laravel 7 create and upload Grayscale Image with example - onlinecode </title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <meta name="csrf-token" content="{{ csrf_token() }}"> </head> <body> <div style="margin: 50px;"> <h1>Laravel5 create and upload Grayscale Image </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="{{ asset('uploads/'.Session::get('image_name')) }}" width="500px" /> </div> </div> @endif {{ Form::open(array('route' => 'gray.scale.image','enctype' => 'multipart/form-data')) }} <div > <div > <strong>Give a Title:</strong> {{ Form::text('title', null,array('class' => 'form-control','placeholder'=>'Give a title')) }} </div> <div > <strong>Upload Image:</strong> {{ Form::file('imagefile', array('class' => 'image')) }} </div> <div > <br/> <button type="submit" >Upload</button> </div> </div> {{ Form::close() }} </div> </body> </html>
Step 7: Run Our Laravel ApplicationWe can start the server and run this example using the below command.
php artisan serve
Now we will run our example using the below Url in the browser.
Hope this code and post will helped you for implement Laravel 7 create and upload Grayscale Image with example – onlinecode. 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