How To Add Watermark Text To Image in PHP Tutorial

Reading Time: 6 minutes
1,725 Views

Adding watermark text on images is a very interesting topic. Inside this article we will see the concept i.e How to add watermark text to image in php. This concept you can use when you want to add your brand value to your copyright content. For example – Adding company name to any of your images, Adding company logo, etc.

This article will be step by step. Very super easy to learn and implement it in your project. PHP provides several functions by which we can work with image manipulation task. Functions are imagecreatefrompng(), imagecolorallocate(), getimagesize(), imagettftext(), imagepng(), imagedestroy().

Before working with images related tasks, you should have these extensions enabled.

  • GD Extension
  • Imagick Extension

Learn More –

Let’s get started.

Create PHP Project

Go to your localhost directory.

Create a folder named as watermark-img. Create files inside it. Files are –

  • index.php -> Create this file. This will generate the image with watermark text on it.
  • watermark-php-text.png -> Put any image of your own, an Image file where we add watermark text.

Download Font Family For WaterMark Text

You can use any font family for your text.

We will use Roboto Font, Click here to go to download.

It will download a zipped folder. Unzip that, and put inside your project directory.

Required PHP Extensions

While working with image related stuff in PHP, make sure your PHP version should have these two most common image processing libraries GD Library and Imagick extensions enabled.

Whether your system already contains these extensions or not you can verify like this –

  • Create info.php file at your localhost directory
  • <?php phpinfo(); ?> Add this code into info.php file.
  • Run this file into browser

You should see these information into php information page.

GD Extension Enabled

Imagick Extension Enabled

Application Programming

Open index.php file and write this code into it.

Use “.png” Image

<?php

$sourceImage = "watermark-php-text.png";

$imagetobewatermark = imagecreatefrompng($sourceImage);
$watermarktext = "Online Web Tutor by Sanjay";
$font = "Roboto/Roboto-Bold.ttf";
$fontsize = "20";
$white = imagecolorallocate($imagetobewatermark, 51, 102, 0);

$image = imagecreatefrompng($sourceImage);
$imageSize = getimagesize($sourceImage);

$wmX = $imageSize[0] - 350;
$wmY = $imageSize[1] - 20;

imagettftext($imagetobewatermark, $fontsize, 0, $wmX, $wmY, $white, $font, $watermarktext);

header("Content-type:image/png");

/*
  For Save Image
  imagepng($imagetobewatermark, $sourceImage);
*/

imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);

Use “.jpg” or “.jpeg” Image

In case if you have other image extensions like of .jpeg or .jpg then use this code for it.

<?php

$sourceImage = "sample-image.jpg";

$imagetobewatermark = imagecreatefromjpeg($sourceImage);
$watermarktext = "Online Web Tutor by Sanjay";
$font = "Roboto/Roboto-Bold.ttf";
$fontsize = "20";
$white = imagecolorallocate($imagetobewatermark, 51, 102, 0);

$image = imagecreatefromjpeg($sourceImage);
$imageSize = getimagesize($sourceImage);

$wmX = $imageSize[0] - 350;
$wmY = $imageSize[1] - 20;

imagettftext($imagetobewatermark, $fontsize, 0, $wmX, $wmY, $white, $font, $watermarktext);

header("Content-type:image/png");

/*
  For Save Image
  imagepng($imagetobewatermark, $sourceImage);
*/

imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);

Application Testing

Open application into browser.

URL: http://localhost/watermark-img/

It will execute index.php and opens application as

We hope this article helped you to learn How To Add Watermark Text To Image in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.