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 -->
<!-- watermarked image -->
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
$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!
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…
blog comments powered by Disqus