PHP
  Home arrow PHP arrow Page 6 - 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 - Taking a Test Drive
    ( Page 6 of 6 )

    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 -->
    <img src="main.jpg">
     
    <br><br>
     
    <!-- watermarked image -->
    <img src="image.php?main=main.jpg&watermark=watermark.png">

    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:

    <?php
     
          # include our watermerking class
          include 'api.watermark.php';
          $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:

    http://www.php.net/manual/en/function.imagecreatefromgif.php

    http://www.php.net/manual/en/function.imagecreatefromjpeg.php

    http://www.php.net/manual/en/function.imagecreatefrompng.php

    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!


    In Summary

    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…



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