PHP
  Home arrow PHP arrow Page 3 - 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 - The Nuts and Bolts
    ( Page 3 of 6 )

    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:

    <?php
    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
          # ADD CODE HERE
               
          # 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
     

       

    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 2 Hosted by Hostway
    Stay green...Green IT