PHP Image2HTML Class
POSTED AT 15:35 on 23rd May 2008
Simple class to convert a jpg, png or gif into HTML.
Source Code
- <?php
- /*
- * PHP Image2HTML Class
- *
- * Class to convert an image into a HTML table.
- * Takes any jpg, gif or png and turns it into a HTML table
- *
- * Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com
- *
- * LICENSE
- * ——-
- * 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.
- *
- * USAGE
- * —–
- *
- * $objImage2HTML = new RVJ_Image2HTML("myImage.jpg");
- *
- * echo $objImage2HTML->returnHTML();
- *
- * You could also just use this class to get the color map of the image by just using the $arrColorMap variable.
- *
- * $arrColorMap = $objImage2HTML->arrColorMap;
- *
- * This map is just an array containing the RGB values of every pixel in the image.
- *
- */
- set_time_limit(0);
- class RVJ_Image2HTML {
- var $strImageLocation;
- var $resImage;
- var $arrImageDetails = array();
- var $strHTML;
- var $arrColorMap = array();
- /*
- *
- * @Method: RVJ_Image2HTML
- * @Parameters: 1
- * @Param-1: strImageLocation - String - The image path
- * @Description: Class constructor
- *
- */
- function RVJ_Image2HTML($strImageLocation){
- //make sure the image file exists
- if(file_exists($strImageLocation)){
- $this->strImageLocation = $strImageLocation;
- //get the image details
- $this->arrImageDetails = getimagesize($this->strImageLocation);
- //load the image
- $this->resImage = $this->loadImage();
- //parse the image to get the colors
- $this->parseImageForColors();
- //destroy the image resource
- $this->destroyImage();
- //create the HTML table for the image
- $this->createHTMLImage();
- }
- }
- /*
- *
- * @Method: loadImage()
- * @Parameters: 0
- * @Description: Loads the required image into a resource depending on the image type
- *
- */
- function loadImage(){
- //choose the correct function for the image type
- switch($this->arrImageDetails['mime']){
- case ‘image/jpeg’:
- return imagecreatefromjpeg($this->strImageLocation);
- break;
- case ‘image/png’:
- return imagecreatefrompng($this->strImageLocation);
- break;
- case ‘image/gif’:
- return imagecreatefromgif($this->strImageLocation);
- break;
- }
- }
- /*
- *
- * @Method: destroyImage
- * @Parameters: 0
- * @Description: Destroys the image resource and so cleans things up
- *
- */
- function destroyImage(){
- imagedestroy($this->resImage);
- }
- /*
- *
- * @Method: parseImageForColors
- * @Parameters: 0
- * @Description: Creates a full image color map of the image
- *
- */
- function parseImageForColors(){
- for($numY = 0;$numY<$this->arrImageDetails[1];$numY++){
- for($numX = 0;$numX<$this->arrImageDetails[0];$numX++){
- $this->arrColorMap[$numY][$numX] = $this->getColorAtPoint($numX, $numY);
- }
- }
- }
- /*
- *
- * @Method: getColorAtPoint
- * @Parameters: 2
- * @Param-1: numX - Number - The X pixel co-ordinate
- * @Param-2: numY - Number - The Y pixel co-ordinate
- * @Description: Returns an array containing the RGB value at a given point
- *
- */
- function getColorAtPoint($numX, $numY){
- $strRGB = imagecolorat($this->resImage, $numX, $numY);
- $arrColor = array(
- ’r’ => ($strRGB >> 16) & 0xFF,
- ’g’ => ($strRGB >> 8) & 0xFF,
- ’b’ => $strRGB & 0xFF,
- );
- return $arrColor;
- }
- /*
- *
- * @Method: createHTMLImage
- * @Parameters: 0
- * @Description: Create the HTML to map the image onto
- *
- */
- function createHTMLImage(){
- $this->strHTML = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"{$this->arrImageDetails[0]}\">\n";
- foreach($this->arrColorMap as $arrRow){
- $this->strHTML .= "\t<tr height=\"1\">\n";
- foreach($arrRow as $arrColor){
- $this->strHTML .= "\t\t<td bgcolor=\"".$this->getHexColor($arrColor)."\"></td>\n";
- }
- $this->strHTML .= "\t</tr>\n";
- }
- $this->strHTML .= "</table>\n";
- }
- /*
- *
- * @Method: getHexColor
- * @Parameters: 1
- * @Param-1: arrColor - Array - The RGB array of color
- * @Description: Returns the hex of a RGB color
- *
- */
- function getHexColor($arrColor){
- $strHex = "";
- foreach($arrColor as $numColor){
- $strHex .= str_pad(dechex($numColor), 2, "0", STR_PAD_LEFT);
- }
- return $strHex;
- }
- /*
- *
- * @Method: returnHTML
- * @Parameters: 0
- * @Description: Returns the HTML
- *
- */
- function returnHTML(){
- return $this->strHTML;
- }
- }
- ?>



