Laravel 8 Add Watermark on Image

Laravel 8 Add Watermark on Image
Laravel 8 Add Watermark on Image

Hello friends, Today we will show Laravel 8 Add Watermark on Image. In this example i will share with you how to adding watermark to image in laravel 8. In this post we will add watermark to images using intervention image composer package in laravel 8 application.

Here we will show add image or text as watermark on image in laravel. Sometimes we need to add watermark to our website images, so we can identify this all images by our website.

Add Watermark on Images in Laravel 8

Now, In this step i will Add Watermark on Images in Laravel. so let’s following to step by step our code.

Step 1: Create New Laravel Application For Laravel 8 Add Watermark on Image
composer create-project --prefer-dist laravel/laravel Blog

Step 2: Install Image Intervention Package For Add Watermark Text on Image in Laravel

composer require intervention/image

Step 3: Update Config/app.php File

<?php

return [

    $providers => [
        
        'Intervention\Image\ImageServiceProvider'
    ],

    $aliases => [
        
        'Image' => 'Intervention\Image\Facades\Image'
    ]

]

Step 4: Create Controller

php artisan make:controller AddImageController

AddImageController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Image;


class AddImageController extends Controller
{

    public function index()
    {
        return view('welcome');
    }
 
    public function imageFileUpload(Request $request)
    {
        $this->validate($request, [
            'file' => 'required|image|mimes:jpg,jpeg,png,gif,svg|max:4096',
        ]);

        $image = $request->file('file');
        $input['file'] = time().'.'.$image->getClientOriginalExtension();

        $imgFile = Image::make($image->getRealPath());

        $imgFile->text('© 2021 websolutionstuff.com', 100, 100, function($font) { 
            $font->size(50);  
            $font->color('#f1f1f1');  
            $font->align('center');  
            $font->valign('bottom');  
        })->save(public_path('/upload').'/'.$input['file']);          

        return back()
        	->with('success','File uploaded successfully ')
        	->with('fileName',$input['file']);         
    }
}

Note : Create upload folder in your public directory, path look like public/upload.

Step 5: Create Route

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AddImageController;


Route::get('/file-upload', [AddImageController::class, 'index']);

Route::post('/add-watermark', [AddImageController::class, 'imageFileUpload'])->name('image.watermark');

Step 6: Create Blade File For Upload image

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <title>Laravel 8 Add Watermark on Image- web-tuts.com</title>
</head>

<body>
    <div class="container">
        <h1>Laravel 8 Add Watermark on Image- web-tuts.com</h2>

        <form action="{{route('image.watermark')}}" enctype="multipart/form-data" method="post">
            @csrf
            @if ($message = Session::get('success'))
            <div class="alert alert-success">
                <strong>{{ $message }}</strong>
            </div>


            <div class="col-md-12 text-center">
                
                <img src="/uploads/{{ Session::get('fileName') }}" width="100%"/>
            </div>
            @endif

            @if (count($errors) > 0)
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif

            <div class="mb-3">
                <input type="file" name="file" class="form-control"  id="formFile">
            </div>

            <div class="d-grid mt-4">
                <button type="submit" name="submit" class="btn btn-primary">
                    Upload File
                </button>
            </div>
        </form>
    </div>
</body>

</html>

I hope you understand of Laravel 8 Add Watermark on Image and it can help you…

Read Also :  Laravel Carbon Get Previous Month Example