easy way to add watermark on images in Laravel

Masoud Jahromi
2 min readOct 4, 2019

How can I add a watermark to images in Laravel? What is the easiest way to accomplish this?

The First Step:

First of all, you should make sure to enable the GD extension on your server.

GD is an open source code library for the dynamic creation of images.

The GD Graphics Library used for creating PNG, JPEG and GIF images and is commonly used to generate charts, graphics, thumbnails on the fly.

Enable GD on CentOS:

yum install gd gd-devel php-gd

Enable GD on Ubuntu:

sudo apt-get update
//For 7.0 PHP
sudo apt-get install php7.0-gd
//For 7.2 PHP
sudo apt-get install php7.2-gd

Intervention Image is a powerful package that can be used to add watermarks to images in Laravel.

The Second Step:

After that, you should install the Intervention Image package.

Install with the composer:

composer require intervention/image

After you have installed Intervention Image, open your Laravel config file config/app.php and add the following lines.

In the $providers array add the service providers for this package.

Intervention\Image\ImageServiceProvider::class

Add the facade of this package to the $aliases array.

'Image' => Intervention\Image\Facades\Image::class

By default, the Intervention Image uses PHP’s GD library extension to process all images. If you want to switch to Imagick, you can pull a configuration file into your application by running one of the following artisan commands.

Publish configuration in Laravel 5

php artisan vendor:publish provider="Intervention\Image\ImageServiceProviderLaravel5"

Publish configuration in Laravel 4

php artisan config:publish intervention/image

In Laravel 5 applications the configuration file is copied to config/image.php, in older Laravel 4 applications you will find the file at app/config/packages/intervention/image/config.php. With this copy, you can alter the image driver settings for your application locally.

The Third Step:

Now, you can add a watermark to your images, where you want to do.

basic example:

$waterMarkUrl = public_path('images/watermark.png');
$image = Image::make(public_path('images/pic.png'));
/* insert watermark at bottom-left corner with 5px offset */
$image->insert($waterMarkUrl, 'bottom-left', 5, 5);
$image->save(public_path('images/pic-new.png'));

Done!

For more information about of insert() function, follow this link.

And More API: http://image.intervention.io

--

--