Laravel Image Text Watermarking Tutorial

Jul 01, 2021 . Admin

Hello Friends,

Now let's see example of how to image text watermarking in laravel. This is a short guide on laravel if image text watermarking. We will use how to use image text watermarking in laravel. Here you will learn how to use image text watermarking in laravel. Let's get started with how to image text watermarking in laravel.

Here i will give you many example how you can image text watermarking in laravel.

Step 1 : Install Laravel 8

we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Install composer require package

Second step to install composer package('intervention/image') in just following command through.

composer require intervention/image
Step 3 : Configure package

Now we need to add provider path and alias path in config/app.php file

Path: config/app.php

return [
    ......
    $provides => [
        ......,
        Intervention\Image\ImageServiceProvider::class,
    ],
    $aliases => [
        .....,
        'Image' => Intervention\Image\Facades\Image::class,
    ]
]
Step 4 : Add Route

Now, we will add routes file so first add bellow route in your routes.php file.

Path: routes\web.php

<?php
    use Illuminate\Support\Facades\Route;
    use App\Http\Controllers\PhotoFileController;

    /*
    |--------------------------------------------------------------------------
    | 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('file-upload', [PhotoFileController::class, 'index'])->name('photo');
        Route::post('add-watermark', [PhotoFileController::class, 'photoFileUpload'])->name('photo.watermark');
    });
?>
Step 5 : Create Controller

Fifth step to create a PhotoFileController in just following command through.

php artisan make:controller PhotoFileController

Path: app\Http\Controllers\PhotoFileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;

class PhotoFileController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
    */
    public function index()
    {
        return view('photo');
    }
    
    /**
     * Write code on Method
     *
     * @return response()
    */
    public function photoFileUpload(Request $request)
    {
        $this->validate($request, [
            'file' => 'required|image|mimes:jpg,jpeg,png,gif,svg|max:4096',
        ]);

        $photo = $request->file('file');
        $input['file'] = time().'.'.$photo->getClientOriginalExtension();
        $imgFile = Image::make($photo->getRealPath());
        $imgFile->text('© 2016-2020 positronX.io - All Rights Reserved', 120, 100, function($font) { 
            $font->size(35);  
            $font->color('#ffffff');  
            $font->align('center');  
            $font->valign('bottom');  
            $font->angle(90);  
        })->save(public_path('/uploads').'/'.$input['file']);          

        return back()
            ->with('success','File successfully uploaded.')
            ->with('fileName',$input['file']);         
    }
}
Step 5 : Blade File and Create Upload directory

In this last step we will create resizePhoto.blade.php file for photo upload form and manage error message and also success message.

Path: resources\views\photo.blade.php

<!DOCTYPE html>
<html>
    <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 Image Text Watermarking Tutorial</title>
    </head>
    <body>
        <div class="container mt-4" style="max-width: 600px">
            <h2 class="mb-5">Laravel Image Text Watermarking Example</h2>
            <form action="{{route('photo.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 mb-3 text-center">
                        <strong>Manipulated photo:</strong><br />
                        <img src="/uploads/{{ Session::get('fileName') }}" width="600px"/>
                    </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>

After, last create directory in your public folder uploads.

Now run bellow command for quick run:

php artisan serve

Now open bellow URL on your browser:

localhost:8000/admin/file-upload

It will help you....

#Laravel