Home arrow PHP arrow Page 6 - Dynamic Watermarking with PHP

Taking a Test Drive - PHP

One of the things you can do with PHP's many functions for image handling and manipulation is create an image watermarking class. This will allow you to add a watermark to images, which can be useful for a number of purposes. Keep reading to learn how.

  1. Dynamic Watermarking with PHP
  2. Cast and Crew
  3. The Nuts and Bolts
  4. Helper Functions
  5. A Union of Images
  6. Taking a Test Drive
By: Brian Vaughn
Rating: starstarstarstarstar / 42
December 28, 2005

print this article



In order to test our image watermarking class, we’ll create two very simple files. The first one we will call “watermark_test.php”. It should contain the following:

<!-- original image -->
<img src="main.jpg">
<!-- watermarked image -->
<img src="image.php?main=main.jpg&watermark=watermark.png">

The purpose of our “watermark_test.php” file is very simple: first, display our original image, and second, display a watermarked copy of that image. In order to do that, we will need two image objects. For our example, I have chosen to use “main.jpg” and “watermark.png”, but you may use any two of your choosing.

You may have also noticed that our second image tag references a PHP file as its source, instead of an image. This is because our script will be creating and returning an image to be displayed by the browser. We can, therefore, call it directly as if it were an image. Let’s go ahead and create our “image.php” file now:

      # include our watermerking class
      include 'api.watermark.php';
      $watermark              = new watermark();
      # create image objects using our user-specified images
      # NOTE: we're just going to assume we're dealing with a JPG and a PNG here - for example purposes
      $main_img_obj                       = imagecreatefromjpeg(  $_GET['main']                 );
      $watermark_img_obj      = imagecreatefrompng(   $_GET['watermark']      );
      # create our watermarked image - set 66% alpha transparency for our watermark
      $return_img_obj               = $watermark->create_watermark( $main_img_obj, $watermark_img_obj, 66 );
      # display our watermarked image - first telling the browser that it's a JPEG,
      # and that it should be displayed inline
      header( 'Content-Type: image/jpeg' );
      header( 'Content-Disposition: inline; filename=' . $_GET['src'] );
      imagejpeg( $return_img_obj, '', 50 );

For those of you who may not have much experience with PHP’s ‘header’ function, the above script may appear a little confusing. Not to worry. All we’re doing with the header function is telling the browser that the content we are about to display is an image and to treat it as such.

As you can see, our script makes a few assumptions about the images we have been passed. Namely, that our main image is a JPEG and our watermarking image is a PNG. These assumptions are okay to make for testing purposes, but you could easily expand the script to handle any number of image formats for a production environment.

As previously mentioned, our “image.php” file creates image objects to pass to our watermarking class (as opposed to passing just the plaintext image path or location). Our watermarking class could have been expanded to receive the path/location, and create the objects itself -- but we opted not to for simplicity’s sake. Further information about how to create image objects using several of the more popular image-for-the-web formats may be found here:




Go ahead and run the test file by opening “watermark_test.php” in your browser. It should show you a copy of your original image, followed by a copy of a newly created watermarked image. How exciting!

In Summary

We’ve covered a lot today, but hopefully we haven’t lost anyone. For those of you who may like to research PHP’s image manipulation functions in a little more detail, I recommend visiting the online documentation found here: http://www.php.net/manual/en/ref.image.php

If you have not been following along, but would like to download a copy of the source code we have just created, please feel free to do so here; it is the same file you can find at the beginning of this article.

For a quick demo of the script we have just created, feel free to point your browser here: http://portfolio.boynamedbri.com/php/watermark/watermark_test.php

As usual, thanks for taking the time to read this article. I hope you had fun – and learned something useful. If you have any questions, please feel free to ask! Until next time…

>>> More PHP Articles          >>> More By Brian Vaughn

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: