PHP Classes

File: image2html.class.php

Recommend this page to a friend!
  Classes of Enéas Gesing   Image2Char   image2html.class.php   Download  
File: image2html.class.php
Role: Class source
Content type: text/plain
Description: Class File
Class: Image2Char
Convert image to HTML with ASCII art characters
Author: By
Last change:
Date: 16 years ago
Size: 1,894 bytes
 

Contents

Class file image Download
<?
class Image2HTML{

    var
$image;
    var
$width;
    var
$height;
   
// chars used to compose image
   
var $chars = 'B';

    function
Image2HTML($image){
       
$file_array = explode('.',$image);
       
$extension = $file_array[sizeof($file_array)-1];

        switch (
$extension){
            case
'jpg':
            case
'jpeg':
               
$this->image = imagecreatefromjpeg($image);
                break;
            case
'gif':
               
$this->image = imagecreatefromgif($image);
                break;
            case
'png':
               
$this->image = imagecreatefrompng($image);
                break;
        }
       
$this->width = imagesx($this->image);
       
$this->height = imagesy($this->image);
    }

    function
PickColor($x, $y){
       
$rgb = ImageColorAt($this->image, $x, $y);

        if(
imageistruecolor($this->image)) {
           
$r = strtoupper(dechex(($rgb >> 16) & 0xFF));
           
$g = strtoupper(dechex(($rgb >> 8) & 0xFF));
           
$b = strtoupper(dechex($rgb & 0xFF));
        } else {
           
$i = imagecolorsforindex($this->image, $rgb);
           
$r = strtoupper(dechex($i['red']));
           
$g = strtoupper(dechex($i['green']));
           
$b = strtoupper(dechex($i['blue']));
        }
        if (
strlen($r) == 1){
           
$r = '0'.$r;
        }
        if (
strlen($g) == 1){
           
$g = '0'.$g;
        }
        if (
strlen($b) == 1){
           
$b = '0'.$b;
        }

       
$rgb = '#'.$r.$g.$b;

        return (
$rgb);
    }

    function
Show(){
       
$print_font = true;

        for (
$i=0; $i<$this->height; $i++){
            for (
$j=0; $j<$this->width; $j++){
               
$color = $this->PickColor($j,$i);
                if (
$print_font)
                    echo
'<font color="'.$color.'">';
                echo
$this->chars[rand(0,strlen($this->chars)-1)].$this->chars[rand(0,strlen($this->chars)-1)];
               
$print_font = false;
                if (
$j != $this->width){
                   
$next_color = $this->PickColor($j+1,$i);
                    if (
$color != $next_color){
                        echo
'</font>';
                       
$print_font = true;
                    }
                }
            }
            echo
"</font><br>\n";
           
flush();
           
$print_font = true;
        }
    }
}
?>