Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Adding Drop Shadows with PHP
MISCELLANEOUS

Adding Drop Shadows with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    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

  •  
     

    SEARCH CODEWALKERS

    Adding Drop Shadows with PHP - Allocating the Color Pallette


    (Page 4 of 7 )

    Color in the world of computers and televisions is based on the additive RGB color scale. The idea is that each color can be obtained by mixing various intensities of red (R), green (G) and blue (B) light together.

    A minimum value is 0 and a maximum value is 255 for a combined total of 16.7 million available colors. The color white is represented by a full intensity of all colors: RGB 255,255,255. The color black is represented by an absence of all colors: RGB 0,0,0.

    Since a set of three values work together to form one color, we can handle them in terms of an array.

    <?php
    $background 
    = array("r" =&gt255"g" =&gt255"b" =&gt255);
    ?>

    An area completely covered by the shadow will be black. An area completely free of the shadow will be the canvas' background color, which we've set to white.

    The drop shadow will be drawn as a series of overlapping rectangles, one on top of the other. The first rectangle will be the background color then each successive rectangle will be slightly smaller and one shade closer to black. This will create the illusion of black fading to the background color.

    To "fade" from black to the background color, the value of each color component will need to change a certain amount per step.

    <?php
    $step_offset 
    = array("r" =&gt; ($background["r"] / DS_STEPS), "g" =&gt; ($background["g"] / DS_STEPS), "b" =&gt; ($background["b"] / DS_STEPS));
    ?>

    The $step_offset will hold the values each component will change between one color and the next to create a smooth gradient effect.

    Depending on the original background color and the defined value of the DS_STEPS constant, the division to determine the offset for a particular color component could result in a floating value. Don't round the value at this point or you may end up with a compounding round off error and the colors won't work out properly.

    Each color needed can now be calculated and allocated in the image's color palette. This is where the value may be rounded.

    <?php
    $current_color 
    $background;

    for (
    $i 0$i &lt;= DS_STEPS$i++) {
      
    $color[$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"];
    }
    ?>

    A for loop is used to assign each needed color to the palette. The imagecolorallocate function accepts the image and the color's components to add and returns an identifier that we'll use to reference the color.

    More Miscellaneous Articles
    More By bluephoenix

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Attention: Forum and Site Maintenance
    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength

    Developer Shed Affiliates

     



    © 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap