How to Perfect Your Images in Laravel 7 Before Uploading

How to manipulate image before upload in Laravel 7 - Blur | Crop | Greyscale

Laravel 7 Image Manipulation Before Upload

In this article we will learn how can you apply blur effect , greyscale effect and crop an image before uploading it Laravel 7 with the help of Intervention Package with example . Similarly there are lots of image manipulation functions available which you can confidently use after reading this article .

To work with image manipulation we need to install intervention package . If you don't know how to install and setup intervention package then you can read my article how to install and setup intervention package  . Let's dive in and see how to manipulate an image before uploading it on the database .


Step 1 - Install Intervention Package :

As i have already told you , to work with images we need to install Intervention package . If you don't know how to install intervention package then read my article how to install and setup intervention package and the go to the step - 2 .

Step 2 - Setup Routes :


Route::get('/upload','imageController@index');

Route::post('/image','imageController@blur');

Step 3 - Setup your Controller :

The next step is to write our image manipulation code inside your controller function . Make sure to sure use the following class on the top of your controller .


use Intervention\Image\ImageManagerStatic as Image;

Blur an Image :

$img = Image::make('public/demo.jpg'); // Image path as parameter

$image->blur(15); // parameter should between 1 to 100

Greyscale to an Image :

$img = Image::make('public/demo.jpg'); // Image path as parameter
$image->greyscale();

Crop an Image :


$img = Image::make('public/demo.jpg'); // Image path as parameter
$image->crop(1200, 500,400,50);

//Takes 4 parameter width,height,X,Y
//where X and Y are Optional

ImageController.php :

Let's see how can you use multiple image manipulation functions for one image .


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;

class imageController extends Controller
{
    public function index()
    {
     return view('image');
    }
    public function blur(Request $request)
    {
      if($request->hasfile('image'))
        {
            $file=$request->file('image');
            $extension=$file->getClientOriginalExtension();
            $filename=time().'.'.$extension;
            $image = Image::make($file->getRealPath());              
            $image->greyscale();
            $image->crop(1200, 500,400,50);
         $image->blur(15);
            $image->save('blured.png',5);

            dd('Image Manipulated');
        }
        else
        {
            dd('Image not Manipulated');
        }
    }
}

After the image is manipulated it will be stored in the " public " folder but you can also change it . I have only use three effects Blur, Greyscale and Crop but you can also use other method from it's official site .

Step 4 - Output :

Original Image :

Laravel 7 Image Manipulation Before Upload

Manipulated Image :

Laravel 7 Image Manipulation Before Upload



Thank you for reading this article 😊

For any query do not hesitate to comment 💬



Previous Post Next Post

Contact Form