Zend - The PHP Company




Graphics

Add Code


GIF to HTML  

Type: code fragment
Added by: itodd
Entered: 27/10/2000
Last modified: 01/12/2000
Rating: ***** (12 votes)
Views: 11856
Creates a html document which resembles a gif file. requires gd.


<?
        
//      Gif to HTML (image.php) (c)2000 Todd M. Boland.
        //      Email: itodd@itodd.org
        //      Web: http://www.itodd.org
?>
<html>
<head>
<title>Gif to HTML</title>
</head>
<body bgcolor="black">
<font size="1"><b><PRE STYLE="font-size: 6pt;line-height: 6pt;">
<?
// RETURN HEX VALUE FROM RGB
function RGB_to_HEX($r$g$b) { 
        
// RED
        
$red dechex($r);
        if(!isset(
$red[1])) { $red "0".$red; } // FIX SINGLE VALUE
        // GREEN
        
$green dechex($g);
        if(!isset(
$green[1])) { $green "0".$green; } // FIX SINGLE VALUE
        // BLUE
        
$blue dechex($b);
        if(!isset(
$blue[1])) { $blue "0".$blue; } // FIX SINGLE VALUE
        
return "#".$red.$green.$blue
}
// OPEN GIF FILE
if(!$file$file "zend.gif";
$image imagecreatefromgif($file);
// DECIDE IF IT'S OF LEGAL SIZE
$width imagesx($image);
$height imagesy($image);
// USE CHAR #
unset($text);
$text[0] = "#";
// START PARSING IMAGE
$y=1; while($y<$height) {
        
$x=1; while($x<$width) {
                
// SNAG COLOR INDEX FROM PIXEL
                
$index imagecolorat($image$x$y);
                
// CONVERT INDEX TO ARRAY
                
$array imagecolorsforindex($image$index);
                
// CONVERT RGB TO HEX
                
$color RGB_TO_HEX($array["red"],$array["green"],$array["blue"]);
                if(!isset(
$text[$i])) $i=0;
                
// OPTIMIZE
                
if($lastcolor==$color) {
                        
// SAME COLOR AS LAST, JUST PRINT THE CHAR
                        
print $text[$i];
                } else {
                        if(
$y==1&&$x==1) {
                                
// FIRST CHAR, OPEN A FONT TAG
                                
print "<font color=".$color.">".$text[$i];
                        } else {
                                
// NEW COLOR, CLOSE AND REOPEN FONT
                                
print "</font><font color=".$color.">".$text[$i];
                        }
                }
                
// NEXT COL
                
$lastcolor $color;
                
$x++; $i++;
        }
        
// NEXT ROW
        
print "n";
        
$y++;
}
?>
</font></pre></b></font>
</body>
</html>


Usage Example


See the example


Rate This Script





Search



This Category All Categories