Laravel 8 Create And Upload Grayscale Image With Example
In this tutorial, we will explain to you how to create and upload Grayscale Image with example in laravel 8. 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 8 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 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel8_image |
Step 2: Install intervention/image Package
Now, We will install intervention/image package using below command.
1 | composer require intervention/image |
Step 3: Add providers and aliases
We will add below providers and aliases in the “config/app.php” file.
1 2 3 4 5 6 7 8 | 'providers' => [ .... 'Intervention\Image\ImageServiceProvider' ], 'aliases' => [ .... 'Image' => 'Intervention\Image\Facades\Image' ] |
Step 4: Create Route
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php use App\Http\Controllers\ImageController; /* |-------------------------------------------------------------------------- | 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::class, 'index']); Route::post('gray-scale-image', [ImageController::class, 'grayScaleSaveImage'])->name('gray.scale.image'); ?> |
Step 5: Create a Controller
Here below command help to create the controller and model.
1 | php artisan make:controller ImageController |
ImageController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use Image; class ImageController extends Controller { public function index() { return view('viewGrayscaleImage'); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ 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 a viewGrayscaleImage.blade.php file in the “resources/views/” folder directory and paste the below code.
viewGrayscaleImage.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <html lang="en"> <head> <title>Laravel 8 create and upload Grayscale Image with example - XpertPhp </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 class="container" style="margin: 50px;"> <h1>Laravel5 create and upload Grayscale Image </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="{{ asset('uploads/'.Session::get('image_name')) }}" width="500px" /> </div> </div> @endif {{ Form::open(array('route' => 'gray.scale.image','enctype' => 'multipart/form-data')) }} <div class="row"> <div class="col-md-4"> <strong>Give a Title:</strong> {{ Form::text('title', null,array('class' => 'form-control','placeholder'=>'Give a title')) }} </div> <div class="col-md-12"> <strong>Upload Image:</strong> {{ Form::file('imagefile', array('class' => 'image')) }} </div> <div class="col-md-12"> <br/> <button type="submit" class="btn btn-success">Upload</button> </div> </div> {{ Form::close() }} </div> </body> </html> |
Step 7: Run Our Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/gray-scale-image |
Read Also
Laravel 8 Dropzone Image Upload Example Tutorial
Laravel 8 CKeditor Image Upload With Example
Laravel 8 Image Upload Tutorial Example