Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 9 Create And Upload Grayscale Image With Example

Laravel 9 Create And Upload Grayscale Image With Example

Posted on February 27, 2022November 20, 2022 By XpertPhp

In this tutorial, we will explain to you how to create and upload Grayscale Image with example in laravel 9(Laravel 9 Create And Upload Grayscale Image With Example). 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 9 create and upload grayscale image with example. so you can also learn and use of laravel intervention image package.

Read Also: Laravel 9 CRUD (Create Read Update Delete) Tutorial For Beginners

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 9, 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.

PHP
1
composer create-project --prefer-dist laravel/laravel laravel9_image

Step 2: Install intervention/image Package

Now, We will install intervention/image package using below command.

PHP
1
composer require intervention/image

Step 3: Add providers and aliases

We will add below providers and aliases in the “config/app.php” file.

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

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

PHP
1
php artisan make:controller ImageController

ImageController.php

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']);
    }
}
See also  Laravel 8 google recaptcha form validation example tutorial

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

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

Laravel Tags:laravel 9 example, laravel 9 tutorial

Post navigation

Previous Post: Laravel 9 Integrate Ckeditor With Example
Next Post: Laravel 9 Create Zip File And Download

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com