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

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


    Table of Contents:
  • An Image is Worth a Thousand Words in PHP
  • Project Overview
  • Setting Up Our Class
  • The Browser Problem

  • 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 - Setting Up Our Class
    ( Page 3 of 4 )

    Since our program will be working with images, which may easily be quite large, it is a good first step for us to increase the maximum script-execution time allotted for our PHP page. The default amount of time a PHP script is allowed to execute is 30 seconds. For our example application, we will probably be well under that limit. Still, just to be on the safe side let’s increase it to a minute. To do that, let’s add the following constructor to our class:

     # constructor: allow for longer max-execution script time
     function img_to_text() {
          set_time_limit ( 100 );  
     } # END img_to_text()

    Let’s also add a few attributes and their associated helper ‘set’ methods to our class:

     # set default configurable values for class
     var $char_mode = 'NORMAL';
     var $chars  = 'IMGTOTEXT';
     var $return_format = 'COLOR';
     var $aspect_ratio = 1;

     # used to track position of current char in $chars string
     # (only if user has selected ‘NORMAL’ char order mode)
     var $char_index = 0;

     # designates whether to display chars in 'random' or 'ascending' order
     function set_char_mode( $char_mode ) {
      $this->char_mode = ( strtoupper( $char_mode ) == 'RANDOM' ) ? 'RANDOM' : 'NORMAL';
     } # END set_char_mode()
     
     # chars used for return text-image (not applicable for 'matrix' return format)
     function set_chars( $chars ) {
        $chars  = str_replace( ' ', '', $chars );
        $this->chars = ( strlen( $chars ) > 0 ) ? $chars : $this->chars;
     } # END set_char_mode()
     
     # return format for our image: 'color' (default), 'grayscale', 'monochrome', or 'matrix'
     function set_return_format( $return_format ) {
      $this->return_format = strtoupper( $return_format );
     } # END set_return_format()
     
     # specify image aspect ratio (0.5 = half size, 1 = the same size, 2 = twice size, etc)
     function set_aspsect_ratio( $aspect_ratio ) {
      $this->aspect_ratio = $aspect_ratio;
     } # END set_aspsect_ratio()

    Each of the above attributes and methods pertain to a specific area of our ‘img_to_txt’ class. Although they are pretty straightforward, we’ll take a brief look at each before proceeding. The first, ‘set_char_mode’, will allow users to specify whether they want the text that will comprise the converted picture to be printed in a linear order, or randomly. The second and related function, ‘set_chars’, allows the user to specify the text they want their image to be made of.

    For instance, if we called ‘set_chars’ and passed the string “THIS IS A PICTURE” – our resulting image would be made up of the repeating phrase, “THISISAPICTURE”. However, if we then called ‘set_char_mode’ and passed it the string “random”, the resulting image would be a random arrangement of the letters “ACEHIPRSTU” (the letters found within the string, “THISISAPICTURE”). You may notice that before we set the allowable characters within the ‘set_chars’ function, we removed any blank space from the user’s string. This is because an empty space character is of very little use to our program, (which the exception of the ‘monochrome’ format), so it is best to remove any spaces up front.

    Next, we have the ‘set_return_format’ function. This allows users to choose an output format for their images. The default mode is ‘color’, but other possible modes are ‘grayscale’, ‘monochrome’, and ‘matrix’. We’ll see what each of these modes looks like in a bit, so for now we’ll move on.

    The last function we just added is called ‘set_aspect_ratio’. This function allows users to choose the size of their text-only image relative to the size of their original image. (An aspect ratio of 0.5 would result in a converted image that’s half of the size of the original, and so on.)



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek