Location: PHPKode > scripts > Image2HTML > image2html/image2html.class.php
<?php
// Developed by Reza Salehi, hide@address.com
class Image2Html
{
	var $_image;	
	var $_textData;
	var $_outputfile;
  	var $_character;
	var $_maxWidth;
	var $_maxHeight;
	
	function Image2Html($imagename, $outputfile, $character, $maxWidth, $maxHeight)
	{
		if($imagename == "" || $imagename == NULL)
			die("The imagename parameter passed to the constructor is not valid.");

		if($outputfile == "" || $outputfile == NULL)
			die("The outputfile parameter passed to the constructor is not valid.");
      
    		if($character == "" || $character == NULL)
			die("The character parameter passed to the constructor is not valid.");

		if($maxWidth == "" || $maxWidth == NULL)
			die("The maxWidth parameter passed to the constructor is not valid.");

		if($maxHeight == "" || $maxHeight == NULL)
			die("The maxHeight parameter passed to the constructor is not valid.");

		$this->_image = $imagename;
		$this->_outputfile = $outputfile;
    		$this->_character = $character;
		$this->_maxWidth = $maxWidth;
		$this->_maxHeight = $maxHeight;
	}

	function WriteToFile()
	{
		$Handle = fopen($this->_outputfile, 'w');
		fwrite($Handle, $this->_textData); 				
		fclose($Handle);
	}

	function GetHtmlFromImage()
	{
		if(!file_exists($this->_image))
			die("The source image file does not exist.");
  
		$im = imagecreatefromjpeg($this->_image);
		if(!$im)
			die("Failed to load the source image.");
		
		list($width, $height, $type, $attr) = getimagesize($this->_image);

		if($width > $this->_maxWidth || $height > $this->_maxHeight)
			die("Image width or height exceeded the maximum size!");

		$this->_textData = '<p style="font-size:1px;line-height:1;">';

		$pixelindex=0;
		$pixelarray = Array();
		for($row=1; $row<$height; $row++)
		{      
			for($col=1; $col<$width; $col++)
			{          
				  $rgb = imagecolorat($im, $col, $row);
          
          			$r = dechex(($rgb >> 16) & 0xFF);         
          	$g = dechex(($rgb >> 8) & 0xFF);          
          	$b = dechex($rgb & 0xFF);  
          
          	$htmlcolor = $r.''.$g.''.$b;
          	$this->_textData .= ('<font color="#'.$htmlcolor.'">'.$this->_character.'</font>');
			}
      		$this->_textData .= "<br />";
		}

		$this->_textData .= "</p>";
	}	
}
?>
Return current item: Image2HTML