PHP
  Home arrow PHP arrow Page 2 - An Image is Worth a Thousand Words in PHP Continued
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? 
Google.com  
PHP

An Image is Worth a Thousand Words in PHP Continued
By: Brian Vaughn
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2005-12-21


    Table of Contents:
  • An Image is Worth a Thousand Words in PHP Continued
  • Examining the Code
  • Final Preparation
  • Converting An Image
  • In Conclusion

  • 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


    An Image is Worth a Thousand Words in PHP Continued - Examining the Code
    ( Page 2 of 5 )

    We've just added a lot of code at once, but each method is pretty straight-forward. First we have our 'init' methods: init_hex_array and init_monochrome_array. These methods simply make sure that our 'HEX' (hexadecimal) and 'monochrome' value arrays have been created and indexed properly. The monochrome array is probably pretty straightforward, but the HEX array may be a little more confusing. Basically, we start by specifying a string of all possible HEX values, in ascending order (0-9, A-F). Our 'init' function simply iterates through that list and expands it to a two-character representation (00 through FF).

    Next we have our various conversion methods, rgb_2_*. Each of these methods is highly specified, and takes a series of arguments specifying the red, green, and blue color values for a given image pixel. Each function then uses the helper, 'rgb_2_hex' function to convert the various pieces of color data into an appropriate return format.

    The color function then simply converts the RGB value to a direct HEX equivalent (0,0,0 = 000000 and 255, 255, 255 = FFFFFF). Next, our grayscale function averages the three color values together, and returns a HEX string representing that average. (This is because a grayscale image is comprised of an equal amount of red, green, and blue, with a varying brightness or intensity). Next, our matrix conversion function averages the RGB values, and then returns a modified HEX string including only green values (000000 through 00FF00). (Similar to our grayscale function, a matrix image is made up of all green colors, so our function simply discards any red or blue color information and averages in the brightness of all three values).

    Finally, our monochrome conversion function doesn't return a color at all; it returns a character. Why is this? Because a monochrome image consists of only two colors - in our case, white and black (white characters on a black background). So rather than return a color, the 'rgb_2_monochrome' function determines the brightness of the pixel (by averaging the three colors present) and then returns a character of text that represents that approximate brightness level. For our example application, we have specified the characters ' .:xW©'. As you may be able to tell, each character is slightly heavier than the character before it. In our application this will result in a very cool, 'monochrome' effect - but we'll see that shortly.



     
     
    >>> More PHP Articles          >>> More By Brian Vaughn
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek