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

Crop Image Before Upload Using Croppie.js in Laravel 9


Using Laravel 9, crop an image before uploading it using Croppie.js. Before uploading an image, Laravel uses Ajax to crop it with croppie. Cropping an...

How to Create a Barcode in a Laravel 9 Application


Laravel 9 barcode generator tutorial; This step-by-step tutorial will show you how to create a barcode in your Laravel app from scratch using the milo...

Laravel 9 Socialite Login with Facebook Tutorial


In this tutorial, we'll learn how to use the Socialite package in Laravel to login with a Facebook social networking account.

Laravel 8 Generate PDF with Graph Tutorial


Laravel 8 generates a graphed pdf. You will understand how to generate a pdf with a graph in the Laravel 8 app in this tutorial

How to perform CURL requests using ixudra/curl in Laravel


Laravel&#39;s ixudra package offers an effective method for making HTTP requests using curl.