Home arrow PHP arrow Page 5 - Dynamic Watermarking with PHP

A Union of Images - 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



Our class is almost complete! All that’s left is to finish off the “create_watermark” function so that it will merge our two images. Let’s replace the “ADD CODE HERE” placeholder with the following code (we’ll go over it in greater detail shortly):

# walk through main image
for( $y = 0; $y < $main_img_obj_h; $y++ ) {
      for( $x = 0; $x < $main_img_obj_w; $x++ ) {
            $return_color     = NULL;
            # determine the correct pixel location within our watermark
            $watermark_x      = $x - $main_img_obj_min_x;
            $watermark_y      = $y - $main_img_obj_min_y;
            # fetch color information for both of our images
            $main_rgb = imagecolorsforindex( $main_img_obj, imagecolorat( $main_img_obj, $x, $y ) );
            # if our watermark has a non-transparent value at this pixel intersection
            # and we're still within the bounds of the watermark image
            if (  $watermark_x >= 0 && $watermark_x < $watermark_img_obj_w &&
                              $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h ) {
                  $watermark_rbg = imagecolorsforindex( $watermark_img_obj, imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) );
                  # using image alpha, and user specified alpha, calculate average
                  $watermark_alpha  = round( ( ( 127 - $watermark_rbg['alpha'] ) / 127 ), 2 );
                  $watermark_alpha  = $watermark_alpha * $alpha_level;
                  # calculate the color 'average' between the two - taking into account the specified alpha level
                  $avg_red          = $this->_get_ave_color( $main_rgb['red'],            $watermark_rbg['red'],        $watermark_alpha );
                  $avg_green  = $this->_get_ave_color( $main_rgb['green'],      $watermark_rbg['green'],      $watermark_alpha );
                  $avg_blue         = $this->_get_ave_color( $main_rgb['blue'],      $watermark_rbg['blue'],       $watermark_alpha );
                  # calculate a color index value using the average RGB values we've determined
                  $return_color     = $this->_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue );
            # if we're not dealing with an average color here, then let's just copy over the main color
            } else {
                  $return_color     = imagecolorat( $main_img_obj, $x, $y );
            } # END if watermark
            # draw the appropriate color onto the return image
            imagesetpixel( $return_img, $x, $y, $return_color );
      } # END for each X pixel
} # END for each Y pixel 

We just added a lot of code, so let’s break it down into smaller bits and discuss what each bit is doing.

First our script sets up a series of ‘for’ loops that will walk through each pixel of our main image object. As it walks through, it also calculates the corresponding pixel-coordinates for our watermark image.

Next it retrieves RGB color information for the pixel we are currently examining on our main image. If this pixel is not within an overlapping area (and thus should not be watermarked), our method simply writes a duplicate pixel to our return image. However, if the pixel we are currently examining falls within an overlapping area, we will also need to determine the blended color value.

To determine a blended color value, we first retrieve an RGB value for our watermark image, using the coordinate information we calculated at the beginning of our ‘for’ loops. Next we update our watermark’s alpha value based on our user-specified alpha value. (If no alpha level is passed to our function, the watermark’s alpha will remain untouched). We then use our helper function,  "_get_ave_color”, to determine a weighted average of the color information for our two images. Finally we use this weighted average, along with our other helper function “_get_image_color”, to determine the closest available color match found within our merged image. This weighted color is then added to our merged image object, “return_img”, at which point our loop executes itself again.

Once the ‘for’ loops have finished executing, we will have constructed a brand new image containing merged information from both of the user-specified image objects – aka, a watermarked image.

But don’t take my word for it – test it out. Let’s talk about how.

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