Desafío Carácter Marciano
#3 Solución de ninbox
Ver código
|
Ver comentarios (0)
|
Descargar código
Fecha: 03 septiembre 2006
Tamaño: 21748 caracteres
Comentarios: 0
Solución online:
http://www.carlosserrano.ninbox.com/phphispano/cg
Valoración









-
(0 votos)
"Esta versión es la version simlifacada del mi script , existe otra version donde se pueden personalizar las paletas y
diccionarios de simbolos.
Para poder verlo funcionando y descargar los dos scripts : http://www.carlosserrano.ninbox.com/phphispano/cg
Nada mas, espero que os guste y suerte para todos
PD: felicidades por este reto tan devertido"
Valora esta solución
<?php
class cg_engine{
var $gestor; //contenedor de la imagen en codigo ascii
var $size_X;
var $size_Y;
var $img;
var $color; //tabla de colores
var $dictionary; //tabla de simbolos
var $timer;
//Constructor del motor de renderizado
//Recibe como parámetros: cg_engine(file_name)
function cg_engine($source){
$this->timer=new crono(); //creamos crono
$this->timer->_start(); //arrancamos el crono
$this->gestor=file($source) or die("Cannot open file");
if(($tmp1=strpos($this->gestor[0], "\n"))==FALSE) $tmp1=strlen($this->gestor[0]);
if(($tmp2=strpos($this->gestor[0], "\r"))==FALSE) $tmp2=strlen($this->gestor[0]);
$tmp3=($tmp1>$tmp2)?$tmp2:$tmp1;
$this->size_X=$tmp3; //obviamos \n y/o \r
$this->size_Y=count($this->gestor);
$this->dictionary=array('M','N','H','#','Q','U','A','D','0','Y','2','$','%','+','.',' '); ;
//paleta de grises personalizada;
$this->ini_pal(array(50,65,80,95,110,125,145,160,175,195,210,220,235,240,245,250));
imagefill($this->img, 0, 0, $this->color[0]); //color de fondo de paleta, para ahorro de renderizado
$this->render();
$this->getImage();
}
//iniciamos paleta
function ini_pal($param){
$this->color=array();
$this->img = @imageCreateTrueColor($this->size_X, $this->size_Y)
or die("Cannot Initialize new GD image stream");
foreach ($param as $key => $value)
$this->color[$key]= imageColorAllocate($this->img,$value,$value,$value);
}
//Realiza el render por defecto con crono
function render(){
$fondo=$this->dictionary[0];
foreach ($this->gestor as $_y => $lineas){
$_x=0;
while($_x<$this->size_X){
while($lineas[$_x++]==$fondo){}//no se procesa el fondo de paleta
$c=$lineas[--$_x];
$_x_old=$_x;
while($c==$lineas[++$_x]/*&&$_x<$this->size_X*/){}
if($_x>($_x_old+1))
imageline($this->img, $_x_old, $_y, $_x-1, $_y, $this->color[array_search($c, $this->dictionary)]);
else
imagesetpixel($this->img, $_x_old, $_y,$this->color[array_search($c, $this->dictionary)]);
}
}
//mostramos crono
imagestring ($this->img, 2, 0, 0, $this->timer->_getTime()." s.", 205 );
}
//genera archivo temporal de imagen
function getImage(){
header("Content-type: image/png");
//generamos la imagen
imagepng($this->img);
//liberamos la memoria ocupada por la imagen
imagedestroy($this->img);
}
}
//clase que crea un cronometro
class crono{
var $timer_ini;
function _start(){
$this->timer_ini=$this->microtime_float();
}
function _reset(){
$this->timer_ini=$this->microtime_float();
}
function _getTime(){
return ($this->microtime_float()-$this->timer_ini);
}
function _print(){
echo "Timer: ".$this->_getTime();
}
function microtime_float()
{
list($useg, $seg) = explode(" ", microtime());
return ((float)$useg + (float)$seg);
}
}
?>
<?php
//Uso con paso de parametros: nombre_del_archivo.php?file=nombre__archivo
// file: nombre del archivo ascii
// La salida generada es una imagen png
if(isset($_GET['file']))
$ob=new cg_engine($_GET['file']);
?>