Image Watermark

Protect your photos and videos with watermarks using php

It is possible to watermark photos and files embedded in your content without modifying the originals. One scenario is displaying photos from your server or photo server with different watermarks.

In the past, watermarked content required editing original photos and videos to change watermarks. The process of editing photos and videos to add watermarks becomes tedious when changes are required or different content is separated into different categories.

Using a simple php script to serve all your embedded content simplifies the process of adding custom watermarks because the only change required is modifying the watermark in the php file.

Tools Required:
Text editor for creating and modifying the php script and permissions
Web server such as apache
Php installed on your web server
Knowledge of the files to be served.

Php file

 *      my_image_watermark.php
 *      Copyright 2010 edward <>
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      GNU General Public License for more details.
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 * 		USAGE:
 * 		/my_image_watermark.php?fileName=myfile.jpg
 * 		chmod 644 for directory

$fileName = basename(urldecode($_GET["fileName"]));
$watermarkText = " watermarked text";
$fileDir = "../images/";  //relative is best "../"

	// Make sure file exists 
	if (file_exists($fileDir . $fileName)) {
		// Create the image
		$watermarkImage = imagecreatefrompng($fileDir . $fileName);
		// Create color alpha goes from 0 to 127
		$grey = imagecolorallocatealpha($watermarkImage, 128, 128, 128, 75);
		// Coordinates of the text
		$px = (imagesx($watermarkImage) - 7.5 * strlen($watermarkText)) / 2;
		// Add the text
		imagestring($watermarkImage, 3, $px, 9, $watermarkText, $grey);
		// Set the content-type png gives better text
		header("Content-type: image/png");
		// Using imagepng() results in clearer text


The beginning of the file after the php tag shows comments on its usage. $fileName is the name of the file that is sent as a server request. $fileDir is the location of the files. The location can be changed to be outside the webroot. It is possible to use absolute paths, but relative paths are best for added security.

Next, check whether the file exists. If the file exists create a png file. Create the watermark colour. Create the text coordinates. Add the text to the created png. Php requires a header in order to display files on the web server. The Content-Type will be for the png file. The file is displayed using imagepng, and then will be destroyed.

Create a folder called images and place it anywhere on your server or on in a remote location, be sure to edit the $fileDir in the php file. The process of creating a permissions file was discussed for the image proxy article.

Image Missing
Test non-watermarked

Watermarked image

Image Missing
Test Watermarked

How to use


<a href="my_image_watermark.php?fileName=photo.jpg">My Photo File </a>

All files should have their permissions set to 644 or read only. This post will be updated as new technology emerges especially php and apache.