PHP
  Home arrow PHP arrow Page 5 - Dynamic Watermarking with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Dynamic Watermarking with PHP
By: Brian Vaughn
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 28
    2005-12-28


    Table of Contents:
  • Dynamic Watermarking with PHP
  • Cast and Crew
  • The Nuts and Bolts
  • Helper Functions
  • A Union of Images
  • Taking a Test Drive

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Dynamic Watermarking with PHP - A Union of Images
    ( Page 5 of 6 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT