Desafío Carácter Marciano
#3 Solución de txo_
Ver código
|
Ver comentarios (2)
|
Descargar código
Fecha: 27 agosto 2006
Tamaño: 34156 caracteres
Comentarios: 2
Solución online:
http://txoweb.com/desafios/marciano/
Valoración









6.25
(4 votos)
No ha realizado ningún comentario
Valora esta solución
<?php
/*
////////////////////////////////////////////////////////////////////
Desafío de PHP-Hispano: Carácter Marciano
Descripción desafío: http://php-hispano.net/desafios/11
Muestra funcionamiento: http://txoweb.com/desafios/marciano/
////////////////////////////////////////////////////////////////////
Creado por txo, Josetxo Iriarte García para php-hispano.net
////////////////////////////////////////////////////////////////////
*/
class c_marciano{
var $fuente = 5; // Fuente
var $fichero='marcianos.txt'; // Url fichero
var $caracteres=Array(); // Caracteres entontrados
var $porcentaje=Array(); // Densidades de las letras correspondientes al array caracteres
var $ancho=0; // Ancho imagen en px
var $alto=0; // Alto imagen en px
var $c_ancho=0; // Ancho fuente en px
var $c_alto=0; // Alto fuente en px
// Constructor
function c_marciano(){
$this->calcular_tam();
}
// Calcula el tamaño de la fuente
function calcular_tam(){
$this->c_ancho=imagefontwidth($this->fuente);
$this->c_alto=imagefontheight($this->fuente);
}
// Esta función dibuja la el caracter, calcula el porcentaje de imagen que ocupa el caracter.
// Parámetros
// $l: Carácter
// Devuelve:
// Número entero, densidad del caracter.
function porcentaje_letra($l){
// Busca el caracter, para evitar calcular mas de na vez el porcentaje
$pos=array_search($l,$this->caracteres);
if(is_int($pos))
return $this->porcentaje[$pos];
else{
// Creamos la imagen
$im=imagecreate($this->c_ancho, $this->c_alto);
// Colores para la imagen
$fondo=imagecolorallocate($im, 255, 255, 255);
$letra=imagecolorallocate($im, 0, 0, 0);
// Fondo de la imagen
imagefill($im, 0, 0, $fondo);
// Escribimos la letra
imagestring($im, $this->fuente, 0, 0, $l, $letra);
// Recorre la imagen buscando puntos blancos
$cont=0;
for($x=0;$x<$this->c_ancho;$x++)
for($y=0;$y<$this->c_alto;$y++)
if(imagecolorat($im,$x,$y)==$fondo) $cont++;
// Libera memoria
imagedestroy($im);
$this->caracteres[]=$l;
$this->porcentaje[]=(($this->c_alto*$this->c_ancho)-$cont);
return (($this->c_alto*$this->c_ancho)-$cont);
}
}
// Calcula el color según la densidad de la letra
// Parámetros
// $im: Variable por referencia, imagen para calcular el color
// $por: Número entero del 1 al 100 con la densidad del color a calcular
// Devuelve:
// Número entero. Color resultante (entre blanco y negro)
function calcular_color(&$im, $por){
$c=255-round($por*2.55);
return imagecolorallocate($im, $c, $c, $c);
}
// Abre el fichero y lee el número de líneas y el número de columnas.
// Estos datos definen la dimensión de la imagen en pixels.
// Devuelve:
// Cierto o falso
function dimension_fichero(){
$f = @fopen($this->fichero, "r");
if ($f){
$this->alto=$this->ancho=0;
while (!feof($f)) {
$linea = fgets($f, 4096);
$len=strlen($linea);
// Calcula la anchura máxima, por si falta algún carácter
if($len>$this->ancho) $this->ancho=$len;
// Cuenta las lineas
if($len) $this->alto++;
}
// Si tiene mas de una línea elimina las dos columnas correspondientes al salto de línea
if($this->alto>1) $this->ancho-=2;
fclose ($f);
return true;
}
return false;
}
// Muestra la imagen resultante de traducir el fichero
function dibujar_foto(){
$t_ini=$this->tiempo();
if($this->dimension_fichero()){
$f = @fopen($this->fichero, "r");
if ($f){
// Cabecera
header("Content-type: image/png");
// Creamos la imagen
$im = imagecreatetruecolor($this->ancho, $this->alto);
$imagen=imagecolorallocate($im, 0, 0, 0);
$fondo=imagecolorallocate($im, 255, 255, 255);
// Fondo, por si falta algún carácter
imagefilledrectangle($im, 0, 0, $this->ancho, $this->alto, $fondo);
$y=0;
// Leemos el fichero
while (!feof($f)) {
$linea = fgets($f, 4096);
$len=strlen($linea);
for($x=0;$x<$len && $x<$this->ancho;$x++)
imagesetpixel($im,$x,$y,$this->calcular_color($im,$this->porcentaje_letra($linea[$x])));
$y++;
}
$t_fin=$this->tiempo();
$tiempo=number_format($t_fin-$t_ini,4).' segundos';
imagestring($im, 2, 2, ($this->alto)-13, $tiempo, $fondo);
imagestring($im, 2, 1, ($this->alto)-14, $tiempo, $imagen);
// Genera la imagen
imagepng($im);
// Libera la memoria utilizada por la imagen
imagedestroy($im);
// Cierra el fichero
fclose ($f);
}
}
}
// Devuelve el tiempo actual
function tiempo(){
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
return $mtime;
}
}
$obj= new c_marciano;
$obj->dibujar_foto();
?>