Miscellaneous
  Home arrow Miscellaneous arrow Page 7 - Adding Drop Shadows with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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? 
MISCELLANEOUS

Adding Drop Shadows with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2005-01-24

    Table of Contents:
  • Adding Drop Shadows with PHP
  • Setting the Shadow Options
  • Opening the Canvas
  • Allocating the Color Pallette
  • Drawing on the Canvas
  • Overlay the Original
  • Combined Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Adding Drop Shadows with PHP - Combined Code


    (Page 7 of 7 )

    Here is the complete script:

    <?php
    /* set drop shadow options */

    /* offset of drop shadow from top left */
    define("DS_OFFSET",  5);
     
    /* number of steps from black to background color /*
    define("DS_STEPS", 10);

    /* distance between steps */
    define("DS_SPREAD"1);

    /* define the background color */
    $background = array("r" =&gt255"g" =&gt255"b" =&gt255);

    $src = isset($_REQUEST['src']) ? urldecode($_REQUEST['src']) : null;
    if(isset(
    $src) &amp;&ampfile_exists($src)) {

      
    /* create a new canvas.  New canvas dimensions should be larger than the original's */
      
    list($o_width$o_height) = getimagesize($src);
      
    $width  $o_width DS_OFFSET;
      
    $height $o_height DS_OFFSET;
      
    $image imagecreatetruecolor($width$height);

      
    /* determine the offset between colors */
      
    $step_offset = array("r" =&gt; ($background["r"] / DS_STEPS), "g" =&gt; ($background["g"] / DS_STEPS), "b" =&gt; ($background["b"] / DS_STEPS));

      
    /* calculate and allocate the needed colors */
      
    $current_color $background;
      for (
    $i 0$i &lt;= DS_STEPS$i++) {
        
    $colors[$i] = imagecolorallocate($imageround($current_color["r"]), round($current_color["g"]), round($current_color["b"]));

        
    $current_color["r"] -= $step_offset["r"];
        
    $current_color["g"] -= $step_offset["g"];
        
    $current_color["b"] -= $step_offset["b"];
      }

      
    /* floodfill the canvas with the background color */
      
    imagefilledrectangle($image0,0$width$height$colors[0]);

      
    /* draw overlapping rectangles to create a drop shadow effect */
      
    for ($i 0$i &ltcount($colors); $i++) {
        
    imagefilledrectangle($imageDS_OFFSETDS_OFFSET$width$height$colors[$i]);
        
    $width -= DS_SPREAD;
        
    $height -= DS_SPREAD;
      }

      
    /* overlay the original image on top of the drop shadow */
      
    $original_image imagecreatefromjpeg($src);
      
    imagecopymerge($image$original_image0,00,0$o_width$o_height100);

      
    /* output the image */
      
    header("Content-type: image/jpeg");
      
    imagejpeg($image""100);
      
      
    /* clean up the image resources */
      
    imagedestroy($image);
      
    imagedestroy($original_image);
    }
    ?>

    And here is a sample of generated output:

    Conclusion

    The process for adding a drop shadow dynamically is pretty straightforward. A slightly larger image is created, the shadow is drawn to the new canvas and then the original image is overlaid. The resulting image is then sent back to satisfy the content request made by the browser.

    The material presented here has room for many modifications and extensions. For example, you could make the script more flexible by using a constant to determine the shadow's angle, or perhaps even consider other methods of drawing a shadow. Your exploration doesn't have to end with this tutorial.

    About The Author

    Timothy Boronczyk lives in Syracuse, NY, where he works as an E-Services Coordinator for a local credit union. He has a background in elementary education, over 5 years experience in web design and has written tutorials on web design, PHP, Ruby, XML and various other topics. His hobbies include photography and composing music.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Nice, this could come in handy.IS it easy to do this with Text?
       · Is it possible to make the dropshadow fade to a transparent value?Not that its a...
       · I am trying to learn PHP, and these tutorials have been a good place to practice. ...
       · The script can be called as with any other PHP script with a direct URI such as...
       · it works but is just solid and goes all around the right and bottom side
       · It could be a few things. When I was developing it, this happened to me a few times...
       · Timothy,This is some great code and a great idea. However, there are a few...
       · i used the code and works perfect for the jpeg images of my website...but i have a...
       · I get the following errorParse error: parse error, unexpected '=', expecting ')'...
       · All types of challenges with this code with extra characters etc. So I worked out...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT