How to add a Text Watermark to an Image in Laravel

AuthorSumit Dey Sarkar

Pubish Date30 Mar 2023

categoryLaravel

In this tutorial we will learn how to add a text watermark to an image in Laravel.

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

How to add a Text Watermark to an Image in laravel

How to add a Text Watermark to an Image in Laravel

This is also used to confirm the legitimacy of the content(Digital signature).

We will explain how to add a text overlay watermark to an image in the Laravel application. We will use the PHP image intervention package to apply the watermarking text to the image.

A powerful open-source package called Intervention Image is used specifically for image editing in PHP-based apps.

It includes with two sublime processing librariesImagick even more GD library.

 

Overview

Step 1 - Install Intervention Image package

Step 2 - Create a route to handle the image processing

Step 3 - Create a controller

Step 4 - In the controller's addWatermark method, use the make method to open the image and add the text watermark

Step 4 - Create a view to display the watermarked image

 

Here are the steps:

 

Step 1 - Install Intervention Image package:

composer require intervention/image

 

Step 2 - Create a route to handle the image processing:

Route::get('/image/watermark', 'ImageController@addWatermark');

 

Step 3 - Create a controller:

php artisan make:controller ImageController

 

Step 4 - In the controller's addWatermark method, use the make method to open the image and add the text watermark:

use Intervention\Image\Facades\Image;

public function addWatermark()
{
    $image = Image::make('path/to/image.jpg');
    $image->text('Your Watermark Text', $image->getWidth() / 2, $image->getHeight() / 2, function ($font) {
        $font->file(public_path('fonts/arial.ttf'));
        $font->size(50);
        $font->color('#ffffff');
        $font->align('center');
        $font->valign('middle');
    });
    return $image->response('jpg');
}

In the above code, we used the text method to add the watermark text to the image. Here we declaire the font, size, color, alignment, and vertical alignment of the text according to requirement.

 

Step 5 - Finally, create a view to display the watermarked image:

<img src="/image/watermark" alt="Watermarked Image">

That's it! Now, when you visit the route, the controller will add the text watermark to the image and return the watermarked image.

 

Optionally, we can also add more options to the make method to control the image's quality, format, and other properties:

$image = Image::make('path/to/image.jpg')
            ->encode('jpg', 75) // set image format and quality
            ->resize(800, 600) // resize the image to fit the dimensions
            ->insert('path/to/watermark.png', 'bottom-right', 10, 10); // add an image watermark

in the above we used the encode method to set the output image format and quality, the resize method to resize the image to fit the specified dimensions, and the insert method to add an image watermark.

 

Note: The insert method accepts the path to the watermark image, the position where the watermark should be inserted the top-left, top-right, bottom-left, bottom-right, center, or any combination of these and the offset values for the X and Y axes.

 

 

Don't forget to import the Image class at the top of your controller:

use Intervention\Image\Facades\Image;

 

Output

How to add a Text Watermark to an Image in laravel

Comments 0

Leave a comment