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

<?php
/*
 *      my_image_watermark.php
 *      
 *      Copyright 2010 edward <http://ojambo.com>
 *      
 *      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
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      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 = "Ojambo.com 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
		imagepng($watermarkImage);
		imagedestroy($watermarkImage);
	}

?>

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 Ojambo.com image proxy article.

Image Missing
Test non-watermarked

Watermarked image

Image Missing
Test Watermarked

How to use

Usage:

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

Recommendations:
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.

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP and Mastering Blender Python API, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📖 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.