The technique of overlaying written information over an image, known as image watermarking, may include the display of copyright or other relevant information.

Today I am going to share how to add watermark on an image in Laravel. By using intervention image, we can easily add watermark on the image. So, let?s start:

Table of Contents

  • Install and Configure Package
  • Create a Controller
  • Register Routes
  • Create a Blade File
  • Run the Project and Test

Step 1 : Install and Configure Package

The intervention/image package will be used. Use CMD to access your project folder and enter the following command to install it:

composer require intervention/image

Laravel 5.4 and earlier After installation, we must modify the config>app.php file to configure the provider and alias. Open app.php and add the following two lines to the providers and aliases array:

.....
'providers' => [
	....
	Intervention\Image\ImageServiceProvider::class,
]
'aliases' => [
	....
	'Image' => Intervention\Image\Facades\Image::class,
]
.....

Step 2 : Create a Controller

Make a controller with the name "WaterMarkController." To make the controller, use the following command:

php artisan make:controller WaterMarkController

Copy and paste this code into the controller by going to app>Http>Controllers:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;

class WaterMarkController extends Controller
{
    public function imageWatermark()
    {
        $img = Image::make(public_path('images/background.png'));

        /* insert watermark at bottom-right corner with 10px offset */
        $img->insert(public_path('images/watermark.png'), 'bottom-right', 10, 10);

        $img->save(public_path('images/new-image.png'));

        $img->encode('png');
        $type = 'png';
        $new_image = 'data:image/' . $type . ';base64,' . base64_encode($img);

        return view('show_watermark', compact('new_image'));
    }

    public function textWatermark()
    {
        $img = Image::make(public_path('images/background.png'));

        $img->text('MyNotePaper', 710, 370, function ($font) {
            $font->file(public_path('font/amandasignature.ttf'));
            $font->size(30);
            $font->color('#f4d442');
            $font->align('center');
            $font->valign('top');
            $font->angle(0);
        });

        $img->save(public_path('images/new-image.png'));

        $img->encode('png');
        $type = 'png';
        $new_image = 'data:image/' . $type . ';base64,' . base64_encode($img);

        return view('show_watermark', compact('new_image'));
    }
}

I came up with two techniques. a text watermark function called textWatermark and an image watermark function called imageWatermark() ().

Step 3 : Register Routes

Create two routes by opening routes>web.php:

<?php

Route::get('watermark-image', 'WaterMarkController@imageWatermark');
Route::get('watermark-text', 'WaterMarkController@textWatermark');

Step 4 : Create a Blade File

Make a file called "show watermark.blade.php" under resources>views. Once the file is open, paste the following code:

<!doctype html>
<html lang="en">
<head>
    <title>Laravel Add Watermark on Images</title>
</head>
<body style="margin-top: 40px; text-align: center;">

<h1>Laravel Add Watermark</h1>

<img src="{{$new_image}}" alt="Watermark">

</body>
</html>

Step 5 : Run the Project and Test

Run the project now, then view the image watermark output at http://localhost:8000/watermark-image route.


Recommended Posts

View All

Laravel 9 Socialite Login with LinkedIn Tutorial Example


How to use the Laravel socialite, Livewire, and Jetstream libraries to create a LinkedIn login system from scratch in Laravel

Laravel 9 Install Vue Auth Tutorial with Example


In this article, I'll demonstrate how to use the Laravel UI and Vue Auth scaffolding to create login, register, logout, forget password, profile, and...

Create Zip file and Download in Laravel 9


After we have compressed the files, let's download the Laravel 9 zip files. In this tutorial, we'll use the ZipArchive class and a Laravel package to...

Laravel whereHas and orWhereHas Query Example


laravel whereHas and orWhereHas query example, wherehas and orWhereHas query in laravel, how to use wherehas in laravel

Laravel 9 Ajax Form Validation Example


In Laravel 9, we may ajax submit a form following validation. Laravel's ajax form validation is simple.