Home / Snippets / How to Watermark Image with Text in Laravel
How to Watermark Image with Text in Laravel cover

How to Watermark Image with Text in Laravel

1K

3 years ago

0 comments

In this short snippet, you will learn how to use the Intervention Image package to watermark images in Laravel. The steps are very simple so let's get started.

Step 1: Install Intervention Image Package to Project


Firstly you have to install the package to your project using the composer command below.
composer require intervention/image

Step 2: Retrieve the Image to be Watermarked


For this example, we'll show you how to retrieve the image and do the processing to a custom route but for your use case, do put this code anywhere necessary (controller or repository class).
<?php

Route::get('watermark-image', function (Request $request) {
    // Option 1: if you are getting the image from the client do get it through the request object
    $image = $request->file('image');

    // Option 2: otherwise you can retrieve it from your storage disk
    $user_profile = User::first()->profile_picture; // image path e.g user-profile-picture.jpeg
    $image = \Storage::disk('s3')->get($user_profile); // getting the user profile image form "s3"
});
The important part to take from the code above is that you can either get the image to process from either the requested file that the user has just uploaded or an existing file that has already existed on the server.

Step 3: Add Watermark to Image


Now that you have the image data already, you can apply the text watermark to the image itself using the Intervention Package "text()" method. This method accepts the text to be placed as the watermark as the first parameter.
<?php

Route::get('watermark-image', function () {
    $user_profile = User::first()->profile_picture;
    $image = \Storage::disk('s3')->get($user_profile);

    $interventionInstance = Intervention\Image::make($image)
        ->text('© hello world. 2021'); // watermark the image using "text()" method

    Storage::disk('s3')->put($user_profile, $interventionInstance, 'public');

    return 'the image has been successfully watermarked';
});

Other Options For Text Watermark


Do note that there are many other options to change the appearance of the watermark and below are the options that you can optionally add if necessary.
<?php

// write text at position
$interventionInstance->text('The quick brown fox jumps over the lazy dog.', 120, 100);

// use callback to define details
$interventionInstance->text('foo', 0, 0, function($font) {
    $font->file('foo/bar.ttf');
    $font->size(24);
    $font->color('#fdf6e3');
    $font->align('center');
    $font->valign('top');
    $font->angle(45);
});

// draw transparent text
$interventionInstance->text('foo', 0, 0, function($font) {
    $font->color(array(255, 255, 255, 0.5));
});
By now when you visit "/watermark" path of your Laravel project you will execute the code to watermark the image. Do note that the above example is meant only to show you how to perform the watermarking and the actual implementation goes according to your respective use case.

If you do find this tutorial to be helpful do make sure to share it with your friends and cheers, happy coding!
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this