Tips, News and Comments on HTML Guard


Posts Tagged ‘PHP’

February 24th, 2010, by Andreas

Using .htaccess to Mask watermark.php Calls

In a previous article, I demonstrated how one could use PHP to dynamically add watermarks to images. Implementing the solution I presented in that article required you to change your image links to something like this:

<img src="watermark.php?image=sample1.jpg&watermark=watermark.png" />

In this article, I will show you how you can easily watermark all images in a certain directory without changing your links to point to a .php file.

Solution Details

Let’s say the images you want to watermark are kept in a sub directory called “gallery” right below your domain folder. At this point, you will have to upload the watermarking script (watermark.php) and the watermark image (watermark.png) to that exact folder and create an .htaccess file containing the following two lines of code:

RewriteEngine on
RewriteRule ^(.*\.(gif|jp?g|png))$ /gallery/watermark.php?image=$1&watermark=watermark.png [NC]

By using the RewriteRule directory, you are telling the web server that image requests shall be processed by the “watermark.php” script file, as opposed to serving the images directly to the browser. The “watermark.php” script then places the “watermark.png” as a watermark on the requested image. After completing the .htaccess file, it must be uploaded (in ASCII mode) to the “gallery” directory.

Download Sample Files

If you wish to play around a bit with my sample code, I have created a zip file that contains all the necessary files for you to do so.


May 25th, 2009, by Andreas

Dynamic Image Watermarks with PHP

Introduction

An effective way to protect the images of a web page from being copied or linked from other websites is to visually mark them with a logo, a copyright notice, or any other identifying marker. In this article, I will explain how PHP can be used to quickly and dynamically watermark images on a web server without having to modify the original image files.

Watermark Sample

The following two images provide an example of what an image looks before and after watermarking it with the PHP script:

Original image:
Sample Image: Central Park

Watermarked image:
Watermarked Sample Image: Central Park

As you can see, the watermark is discreetly placed in the lower right corner without inhibiting the viewer from seeing the image.

Download and Installation

Please click here to download the watermarking script (including a sample page and the SVG sources for the watermark image).

To install the script, just upload the “watermark.php” file to your web server and change image tags in your HTML code from something like this:

<img src="sample1.jpg" />

to this:

<img src="watermark.php?image=sample1.jpg&watermark=watermark.png" />

Instead of linking an image directly, pass the image’s file name to the PHP script and additionally reference a watermark file. Supported image types are JPEG, PNG, and GIF. Image files can also be placed in subfolders relative to the folder of the script file:

<img src="watermark.php?image=folder/sample1.jpg&watermark=folder/watermark.png" />

The Code in Detail

First, the script opens both the original image and the watermark image using one of the imagecreatefrom(jpeg/gif/png) functions. Like many other functions used in the script, these functions are provided by the GD library that has been bundled with PHP since version 4.3.

function imagecreatefromfile($image_path) {
  // retrieve the type of the provided image file
  list($width, $height, $image_type) = getimagesize($image_path);
 
  // select the appropriate imagecreatefrom* function based on the determined
  // image type
  switch ($image_type)
  {
    case IMAGETYPE_GIF: return imagecreatefromgif($image_path); break;
    case IMAGETYPE_JPEG: return imagecreatefromjpeg($image_path); break;
    case IMAGETYPE_PNG: return imagecreatefrompng($image_path); break;
    default: return ''; break;
  }
}

Next, the intended position of the watermark in the output image is calculated (the watermark is placed in the lower right corner).

$watermark_pos_x = imagesx($image) - imagesx($watermark) - 8;
$watermark_pos_y = imagesy($image) - imagesy($watermark) - 10;

Now the source image and the watermark are being merged:

imagecopy($image, $watermark,  $watermark_pos_x, $watermark_pos_y, 0, 0,
  imagesx($watermark), imagesy($watermark));

The result is outputted to the browser as a JPEG image with the best quality (100):

header('Content-Type: image/jpeg');
imagejpeg($image, '', 100);

Finally, we free the memory allocated for the images:

imagedestroy($image);
imagedestroy($watermark);