Home arrow PHP arrow Page 3 - Dynamic Watermarking with PHP

The Nuts and Bolts - 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



Just to keep things simple, let’s start by creating our image watermarking class. This class, called “watermarking”, should be created inside a file named “api.watermark.php”. Go ahead and create that file now, and insert the following lines of code:

class watermark{
      # given two images, return a blended watermarked image
function create_watermark() { }
      # average two colors given an alpha
      function _get_ave_color() {   }    
      # return closest pallette-color match for RGB values
      function _get_image_color() { }
} # END watermark API

So far we’ve just created a shell into which we can insert our code. Let’s begin by taking a look at our “create_watermark” function. Go ahead and replace our placeholder “create_watermark” function with the following code:

# given two images, return a blended watermarked image
function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) {
      $alpha_level      /= 100;     # convert 0-100 (%) alpha to decimal
      # calculate our images dimensions
      $main_img_obj_w   = imagesx( $main_img_obj );
      $main_img_obj_h   = imagesy( $main_img_obj );
      $watermark_img_obj_w    = imagesx( $watermark_img_obj );
      $watermark_img_obj_h    = imagesy( $watermark_img_obj );
      # determine center position coordinates
      $main_img_obj_min_x     = floor( ( $main_img_obj_w / 2 ) - ( $watermark_img_obj_w / 2 ) );
      $main_img_obj_max_x     = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) );
      $main_img_obj_min_y     = floor( ( $main_img_obj_h / 2 ) - ( $watermark_img_obj_h / 2 ) );
      $main_img_obj_max_y     = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) );
      # create new image to hold merged changes
      $return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h );
      # walk through main image
      # return the resulting, watermarked image for display
      return $return_img;
} # END create_watermark()
The first thing we did, as you may have noticed, was expand our function to receive 3 incoming parameters:
$main_img_obj           # plain image which our script should watermark
$watermark_img_obj      # watermark image, can be alpha-transparent
$alpha_level            # watermark alpha value, (0-100, default = 100)

(It is important to note at this time that our function is expecting image objects as incoming parameters, not simply paths to an image – but we will discuss this in greater detail shortly.)

Next we gathered some simple width and height information about each of our incoming images.  We then used that information to determine X and Y coordinates for center aligning our watermark on top of our main image.

Next our function creates a new, true color image object with the same dimensions as our main image object. This new image, “return_img”, will be used to store the merged image information from our other two images.

Our next major step is to walk through the images and merge them together, but we aren’t quite ready for that yet. Instead we’ve simply inserted a placeholder comment, “ADD CODE HERE”, until we are ready to return and complete that block of code.

Finally we simply return our modified image for display by the page that’s calling it. So far what we’ve done has been pretty basic. Before we move on to the core of our application though, let’s take a look at one more thing: our helper functions.

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