Zend - The PHP Company




Graphics

Add Code


Image to HTML Text Converter Class  

Type: application
Added by: mangled
Entered: 02/06/2001
Last modified: 06/12/2000
Rating: - (fewer than 3 votes)
Views: 5446
Image to HTML Text Converter Class


<?php 
/* 
 * Image to HTML Text Converter Class 
 * Version 1.5 
 * 
 * Copyright 2000    by Markus Dobel (mdobel@kawo2.rwth-aachen.de) 
 *  
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.      
 * 
 * 
 * Requirements: PHP with GD support for the desired image format 
 * 
 * Note: Tested with Netscape Communicator 4.7 (Linux, Windows) 
 *               and MS Internet Explorer  5.0 (Windows) 
 *       The only font-size which seems to work nicely with all of  
 *       them at once is 12px. (with the default monospace font) 
 *  
 * 
 * Functions: 
 *   LoadImage (string filename);  
 *   LoadText (string filename); 
 *   SetText (string text); 
 *   ImageInfo (); 
 *   PrintInfo ();   
 *   PrintStylesheet (); 
 *   PrintImage (); 
 *    
 *   print a whole page with this function: 
 *   PrintPage (string imagefilename, string textfilename); 
*/ 

class HTMLImage 
   
  
/* background and font size */ 
  
var $stylesheet=
    <style type="
text/css"><!-- 
      BODY { 
        background-color: #202020; 
      } 
      PRE { 
        font-family: monospace; 
        font-size: 12px; 
      } 
    //--></style> 
"

   
  
/* Do not set these variables */ 
  
var $image;       // Image handle 
  
var $imagetype;   // Image type (numerical) 
  
var $imageheight// Dimensions of image 
  
var $imagewidth
  var 
$text;        // Text to print 
  
var $textlength;   
    
  var 
$imagetypes=array(1=>"GIF"2=>"JPEG"3=>"PNG"); 

  function 
LoadImage($filename) { 
    
/* Load Image, set type and size */ 
    
unset($this->imageheight); 
    unset(
$this->imagewidth); 
    unset(
$this->imagetype); 
    if (isset(
$this->image)) { ImageDestroy($this->image); } 
    unset(
$this->image); 
    if(
is_readable($filename)) { 
      list(
$width,$height,$type)=GetImageSize($filename); 
      switch (
$type) { 
        case 
1
        case 
2
        case 
3
          if (
function_exists("ImageCreateFrom".$this->imagetypes[$type])) { 
            
$cleartype $imagetypes[$type]; 
            eval(
"$this->image=ImageCreateFrom".$this->imagetypes[$type]."("".$filename."");"); 
            
$this->imageheight $height
            
$this->imagewidth  $width
            
$this->imagetype   $type
            return(
true); 
          } else { 
            print(
"Warning: This version of PHP has no ".$this->imagetypes[$type]." Support<br>"); 
            return(
false); 
          } 
          break; 
        default: 
        print(
"Warning: Cannot determine image type of ".$filename."n"); 
        return(
false); 
      } 
    } else { 
      print(
"Warning: Cannot access ".$filename.". (Does not exist or has wrong permissions)<br>"); 
      return(
false); 
    } 
  } 

  function 
LoadText($filename) { 
    
/* Load textfile into class, return true on success */ 
    
if(is_readable($filename)) { 
      
$fp fopen($filename,"r"); 
      
$text fread($fp,filesize($filename)); 
      
fclose($fp); 
      return(
$this->SetText($text)); 
    } else { 
      print(
"Warning: Cannot access ".$filename.". (Does not exist or has wrong permissions)<br>"); 
      return(
false); 
    } 
  } 

  function 
SetText($text) { 
    
/* Set text from a variable, return true on success */ 
    
$text=preg_replace("/s+/","",$text); 
    
$len=strlen($text); 
    if(
$len>0) { 
      
$this->text=$text
      return(
true); 
    } else { 
      unset(
$this->text); 
      print(
"Warning: Text contains no printable characters<br>"); 
      return(
false); 
    }     
  } 

  function 
PixColortoHTML($img$x$y) { 
    
/* internal function to get the HTML color code for a pixel */ 
    
$rgb=imagecolorsforindex($img,imagecolorat($img,(int)$x,(int)$y)); 
    return(
sprintf("#%02x%02x%02x",$rgb["red"],$rgb["green"],$rgb["blue"]));  
  } 
   
  function 
ImageInfo() { 
    if(isset(
$this->image)){ 
      return(
$this->imagetypes[$this->imagetype]." Image (".$this->imagewidth "x" $this->imageheight." pixels)"); 
    } else { 
      return(
"No image loaded"); 
    }   
  }   

  function 
PrintInfo() { 
    print(
$this->ImageInfo()); 
  } 
  
  function 
PrintStylesheet() { 
    print(
$this->stylesheet); 
  } 
   
  function 
PrintImage() { 
    
/* print image block */   
    /* check for existance of image and text */ 
     
    
if(isset($this->image) and isset($this->text)) { 
      
/* 
       * Create rescaled version of the image for text output. 
       * Rendering the image unscaled in monospace font ends up  
       * with a very ugly aspect ratio. 
      */ 
      
$newheight=(int)($this->imageheight 0.5); 
      
$scaledimg=ImageCreate($this->imagewidth,$newheight); 

      
ImageCopyResized($scaledimg$this->image,0,0,0,0,$this->imagewidth,$newheight,$this->imagewidth,$this->imageheight); 
      
$pixelcount=$this->imagewidth*$newheight
      
$len=strlen($this->text); 
      
$i 1

      
/* Calculation and output of first pixel */ 
      
$oldcol=$this->PixColortoHTML($scaledimg,0,0); 
      print(
"<pre><font color=".$oldcol.">"); 
      print(
htmlspecialchars($this->text[0])); 

      
/* Calculate and output the remaining image in a loop */ 
      
while($i<$pixelcount) { 
        
$curcol=$this->PixColortoHTML($scaledimg,$i%$this->imagewidth,$i/$this->imagewidth); 
        if (
$oldcol!=$curcol) { 
          print(
"</font><font color=".$curcol.">"); 
        } 
        print(
htmlspecialchars($this->text[$i%$len])); 
        
$i++; 
        if(
$i%$this->imagewidth==0) {  
          print(
"n");  
          @
set_time_limit(1); 
        } 
        
$oldcol=$curcol
      } 
      print(
"</font></pre>n"); 
      
ImageDestroy($scaledimg);      
    } 
  } 

  function 
PrintPage($imgfile,$textfile) { 
    if(
$this->LoadImage($imgfile) and $this->LoadText($textfile)) { 
      print(
"<html><head><title>"); 
      
$this->PrintInfo(); 
      print(
"</title>"); 
      
$this->PrintStylesheet();  
      print(
"</head><body>"); 
      
$this->PrintImage(); 
      print(
"</body></html>"); 
    } 
  } 

?>


Usage Example




Rate This Script





Search



This Category All Categories