RedVodkaJelly Logo

PHP Image2HTML Class

POSTED AT 15:35 on 23rd May 2008

Simple class to convert a jpg, png or gif into HTML.

Source Code

  1. <?php 
  2. /* 
  3. * PHP Image2HTML Class 
  4. * Class to convert an image into a HTML table. 
  5. * Takes any jpg, gif or png and turns it into a HTML table 
  6. * Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com 
  7. *  
  8. * LICENSE 
  9. * ——- 
  10. * Feel free to use this as you wish, just give me credit where credits due and drop me an email telling me what your using it for so I can check out all the cool ways its been used. 
  11. * USAGE 
  12. * —– 
  13. *   $objImage2HTML = new RVJ_Image2HTML("myImage.jpg"); 
  14. *   echo $objImage2HTML->returnHTML(); 
  15. * You could also just use this class to get the color map of the image by just using the $arrColorMap variable. 
  16. *   $arrColorMap = $objImage2HTML->arrColorMap; 
  17. * This map is just an array containing the RGB values of every pixel in the image. 
  18. */ 
  19.  
  20. set_time_limit(0); 
  21.  
  22. class RVJ_Image2HTML { 
  23.  
  24.    var $strImageLocation; 
  25.    var $resImage; 
  26.    var $arrImageDetails = array(); 
  27.    var $strHTML; 
  28.    var $arrColorMap = array(); 
  29.     
  30.    /* 
  31.    * 
  32.    *   @Method:      RVJ_Image2HTML 
  33.    *   @Parameters:   1 
  34.    *   @Param-1:      strImageLocation - String - The image path 
  35.    *   @Description:   Class constructor 
  36.    * 
  37.    */ 
  38.    function RVJ_Image2HTML($strImageLocation){ 
  39.       //make sure the image file exists 
  40.       if(file_exists($strImageLocation)){ 
  41.          $this->strImageLocation = $strImageLocation; 
  42.           
  43.          //get the image details 
  44.          $this->arrImageDetails = getimagesize($this->strImageLocation); 
  45.           
  46.          //load the image 
  47.          $this->resImage = $this->loadImage(); 
  48.           
  49.          //parse the image to get the colors 
  50.          $this->parseImageForColors(); 
  51.           
  52.          //destroy the image resource 
  53.          $this->destroyImage(); 
  54.           
  55.          //create the HTML table for the image 
  56.          $this->createHTMLImage(); 
  57.       } 
  58.    } 
  59.     
  60.    /* 
  61.    * 
  62.    *   @Method:      loadImage() 
  63.    *   @Parameters:   0 
  64.    *   @Description:   Loads the required image into a resource depending on the image type 
  65.    * 
  66.    */ 
  67.    function loadImage(){    
  68.       //choose the correct function for the image type 
  69.       switch($this->arrImageDetails['mime']){ 
  70.          case ‘image/jpeg’: 
  71.             return imagecreatefromjpeg($this->strImageLocation); 
  72.             break; 
  73.          case ‘image/png’: 
  74.             return imagecreatefrompng($this->strImageLocation); 
  75.             break; 
  76.          case ‘image/gif’: 
  77.             return imagecreatefromgif($this->strImageLocation); 
  78.             break; 
  79.       } 
  80.    } 
  81.     
  82.    /* 
  83.    * 
  84.    *   @Method:      destroyImage 
  85.    *   @Parameters:   0 
  86.    *   @Description:   Destroys the image resource and so cleans things up 
  87.    * 
  88.    */ 
  89.    function destroyImage(){ 
  90.       imagedestroy($this->resImage); 
  91.    } 
  92.     
  93.    /* 
  94.    * 
  95.    *   @Method:      parseImageForColors 
  96.    *   @Parameters:   0 
  97.    *   @Description:   Creates a full image color map of the image 
  98.    * 
  99.    */ 
  100.    function parseImageForColors(){ 
  101.       for($numY = 0;$numY<$this->arrImageDetails[1];$numY++){ 
  102.          for($numX = 0;$numX<$this->arrImageDetails[0];$numX++){ 
  103.             $this->arrColorMap[$numY][$numX] = $this->getColorAtPoint($numX, $numY); 
  104.          } 
  105.       } 
  106.    } 
  107.     
  108.    /* 
  109.    * 
  110.    *   @Method:      getColorAtPoint 
  111.    *   @Parameters:   2 
  112.    *   @Param-1:      numX - Number - The X pixel co-ordinate 
  113.    *   @Param-2:      numY - Number - The Y pixel co-ordinate 
  114.    *   @Description:   Returns an array containing the RGB value at a given point 
  115.    * 
  116.    */ 
  117.    function getColorAtPoint($numX, $numY){ 
  118.       $strRGB = imagecolorat($this->resImage, $numX, $numY); 
  119.  
  120.       $arrColor = array( 
  121.          ’r’ => ($strRGB >> 16) & 0xFF, 
  122.          ’g’ => ($strRGB >> 8) & 0xFF, 
  123.          ’b’ => $strRGB & 0xFF, 
  124.       ); 
  125.       return $arrColor; 
  126.    } 
  127.     
  128.    /* 
  129.    * 
  130.    *   @Method:      createHTMLImage 
  131.    *   @Parameters:   0 
  132.    *   @Description:   Create the HTML to map the image onto 
  133.    * 
  134.    */ 
  135.    function createHTMLImage(){ 
  136.       $this->strHTML = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"{$this->arrImageDetails[0]}\">\n"; 
  137.        
  138.       foreach($this->arrColorMap as $arrRow){ 
  139.          $this->strHTML .= "\t<tr height=\"1\">\n"; 
  140.           
  141.          foreach($arrRow as $arrColor){ 
  142.             $this->strHTML .= "\t\t<td bgcolor=\"".$this->getHexColor($arrColor)."\"></td>\n"; 
  143.          } 
  144.           
  145.          $this->strHTML .= "\t</tr>\n"; 
  146.       } 
  147.        
  148.        
  149.       $this->strHTML .= "</table>\n"; 
  150.    } 
  151.     
  152.    /* 
  153.    * 
  154.    *   @Method:      getHexColor 
  155.    *   @Parameters:   1 
  156.    *   @Param-1:      arrColor - Array - The RGB array of color 
  157.    *   @Description:   Returns the hex of a RGB color 
  158.    * 
  159.    */ 
  160.    function getHexColor($arrColor){ 
  161.       $strHex = ""; 
  162.       foreach($arrColor as $numColor){ 
  163.          $strHex .= str_pad(dechex($numColor), 2, "0", STR_PAD_LEFT); 
  164.       } 
  165.       return $strHex; 
  166.    } 
  167.     
  168.    /* 
  169.    * 
  170.    *   @Method:      returnHTML 
  171.    *   @Parameters:   0 
  172.    *   @Description:   Returns the HTML 
  173.    * 
  174.    */ 
  175.    function returnHTML(){ 
  176.       return $this->strHTML; 
  177.    } 
  178.  
  179. ?>