PHP Classes

File: blocks.php

Recommend this page to a friend!
  Classes of Andrey Nikishaev   Draw Blocks   blocks.php   Download  
File: blocks.php
Role: Class source
Content type: text/plain
Description: class
Class: Draw Blocks
Generate an image with mapped colored squares
Author: By
Last change: fix
Date: 15 years ago
Size: 5,220 bytes
 

Contents

Class file image Download
<?php

class blocks {
    var
$block_size;
    var
$margin_size;
    var
$colors=array();
    var
$matrix='';
       
    public function
__construct($block_size,$margin_size,$border,$colors) {
       
$this->block_size=$block_size;
       
$this->margin_size=$margin_size;
       
$this->border=$border;
       
$this->colors=$colors;
    }
    public function
addMatrix($str,$clear=false) {
       
$lines1=explode(chr(10),$this->matrix);
        if((
strlen($this->matrix)>0) && !$clear) {
           
$lines2=explode(chr(10),$str);
            for(
$rows=0;$rows<count($lines1);$rows++) {
               
$lines1[$rows].=$lines2[$rows];
            }
           
$this->matrix=implode(chr(10),$lines1);
        } else {
           
$this->matrix=$str;
        }
    }
    private function
hexToDec($hex) {
       
$dec = array();
          
$hash = array( 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15);
           for (
$i = 0; $i < strlen($hex); $i++) {
                   if(
array_key_exists(strtoupper($hex[$i]),$hash)) {
                      
$num=$hash[strtoupper($hex[$i])];
                   } else {
                      
$num=$hex[$i];
                   }
                  
$dec[]=$num;
           }
        return
$dec;
    }
    private function
getRGB($hex) {
       
$decs=$this->hexToDec($hex);
        for(
$i=0;$i<=5;$i=$i+2) {
           
$col[]=$decs[$i]*16+$decs[$i+1];
        }
        return
$col;
    }
    private function
getHEX($col) {
       
$hash = array( 0,1,2,3,4,5,6,7,8,9,'A', 'B', 'C', 'D', 'E', 'F');
       
$r=$hash[floor($col['red']/16)].$hash[($col['red']%16)];
       
$g=$hash[floor($col['green']/16)].$hash[($col['green']%16)];
       
$b=$hash[floor($col['blue']/16)].$hash[($col['blue']%16)];
        return
$r.$g.$b;
    }
    public function
render($type) {
       
$lines=explode(chr(10),$this->matrix);
       
array_shift($lines);
       
array_pop($lines);
       
$height=$this->block_size*count($lines)+$this->margin_size*(count($lines)-1);
       
$width=$this->block_size*strlen($lines[0])+$this->margin_size*(strlen($lines[0])-1);
       
$left=$this->border;
       
$top=$this->border;
       
$image = imagecreatetruecolor($width+$this->border*2, $height+$this->border*2);
       
$c=$this->getRGB($this->colors["0"]);
       
$col = imagecolorallocate($image, $c[0], $c[1], $c[2]);
       
imagefill ($image, 0, 0, $col);
        for(
$rows=0;$rows<count($lines);$rows++) {
            for(
$cols=0;$cols<strlen($lines[0]);$cols++) {
               
$symb=$lines[$rows][$cols];
               
$right=$left+$this->block_size;
               
$bottom=$top+$this->block_size;
               
$c=$this->getRGB($this->colors[$symb]);
               
$col = imagecolorallocate($image, $c[0], $c[1], $c[2]);
               
imagefilledrectangle($image, $left, $top, $right, $bottom, $col);
               
$left+=$this->block_size+$this->margin_size;
            }
           
$left=$this->border;
           
$top+=$this->block_size+$this->margin_size;
        }
        switch(
$type) {
            case
"png":
               
header("Content-type: image/png");
               
imagepng($image);
                break;
            case
"gif":
               
header("Content-type: image/gif");
               
imagegif($image);
                break;
            case
"jpg":
               
header("Content-type: image/jpeg");
               
imagejpeg($image,null,100);
                break;
        }
    }
    public function
get_avg_color($im,$sl,$st,$el,$et) {
       
$indexes=array();
        for(
$t=$st;$t<=$et;$t++){
            for(
$l=$sl;$l<=$el;$l++) {
               
$indexes[] = imagecolorsforindex($im, imagecolorat($im, $l, $t));
            }
        }
       
$color=array_shift($indexes);
        while(
count($indexes)>0) {
           
$color['red']=floor(($color['red']+$indexes[0]['red'])/2);
           
$color['green']=floor(($color['green']+$indexes[0]['green'])/2);
           
$color['blue']=floor(($color['blue']+$indexes[0]['blue'])/2);
           
$color['alpha']=floor(($color['alpha']+$indexes[0]['alpha'])/2);
           
array_shift($indexes);
        }
        return
$color;
    }
    public function
get_near_color($col,$precision=10) {
       
$ret=false;
        foreach(
$this->colors as $k=>$c) {
           
$c=$this->getRGB($c);
            if((
abs($c[0]-$col['red'])<=$precision) && (abs($c[1]-$col['green'])<=$precision) && (abs($c[2]-$col['blue'])<=$precision)) {
               
$ret=$k;break;
            }
        }
        return
$ret;
    }
    public function
get_matrix_form_img($file,$opt) {
         if(
is_resource($file)) {
            
$im=$file;
            
$width_im=imagesx($im);
            
$height_im=imagesy($im);
         } else {
             if(!
file_exists($file)) die("File doesn't exist!");
             list(
$width_im, $height_im, $type_im, $attr_im) = getimagesize($file);
                 switch(
$type_im) {
                 case
"1":
                 @
$im = imagecreatefromgif($file);
                 break;
                 case
"2":
                 @
$im = imagecreatefromjpeg($file);
                 break;
                 case
"3":
                 @
$im = imagecreatefrompng($file);
                 break;
             }
         }
         if(!
$im) die('File type not recognized');
        
$block=$width_im/$opt["width_in_blocks"];
       
        
$work_width=$block*$opt["width_in_blocks"];
       
        
$work_height=floor($height_im/$block)*$block;
       
        
$block_h=floor($height_im/$block);
       
        
$block_w=$opt["width_in_blocks"];
       
        
$ch=30;
        
$matrix=chr(10);
         for(
$t=0;$t<$block_h;$t++) {
             for(
$l=0;$l<$block_w;$l++) {
                
                
$col=$this->get_avg_color($im,($l*$block),($t*$block),($l*$block+$block),($t*$block+$block));
                
$near=$this->get_near_color($col,$opt["precision_color_match"]);
                 if(
$near!==false) {
                    
$symb=$near;
                 } else {
                     if(
ord('0')==$ch) $ch++;
                    
$this->colors[chr($ch)]=$this->getHEX($col);
                    
$symb=chr($ch);
                    
$ch++;
                 }
                
$matrix.=$symb;
             }
            
$matrix.=chr(10);
         }
         return
$matrix;
    }
}