Desafío Carácter Marciano
#3 Solución de sencev
Ver código
|
Ver comentarios (0)
|
Descargar código
Fecha: 13 noviembre 2007
Tamaño: 8668 caracteres
Comentarios: 0
Solución online:
Lo siento, pero no está disponible
Valoración









-
(0 votos)
"<?php
$file = file_get_contents('index.php');
$lines = explode("\n", $file);
$countLines = sizeof($lines);
$countChars = strlen($file);
$countColsPerLine = $countChars / $countLines;
echo "<pre>"
. "Lines : $countLines\n"
. "Chars : $countChars\n"
. "Cols per Line : $countColsPerLine\n"
. "</pre>";"
Valora esta solución
<?php
$file = file_get_contents('marcianos.txt');
$lines = explode("\n", $file);
$dotWidth = 1;
$dotHeight = 1;
$imageWidth = ( strlen($lines[0]) - 1 ) * $dotWidth;
$imageHeight = ( sizeof($lines) - 1 ) * $dotHeight;
$image = imagecreate($imageWidth, $imageHeight);
$background = imagecolorallocate($image, 0, 255, 0);
$colors = array();
$colorsChars = array('#', 'M', 'N', 'A', 'H', 'D', 'U', 'Y', 'Q', '0', '2', '$', '%', '+', '.', ' ');
$rgb = 0;
foreach ( $colorsChars as $char )
{
$colors[$char] = imagecolorallocate($image, $rgb, $rgb, $rgb);
$rgb += 16;
}
$colors['_'] = imagecolorallocate($image, 255, 0, 0);
$x = 0; $y = 0;
foreach ( $lines as $line )
{
for ( $i = 0, $l = strlen($line); $i < $l; $i++ )
{
$char = $line[$i];
if ( isset($colors[$char]) )
$color = $colors[$char];
else
$color = $colors['_'];
imagefilledrectangle($image, $x, $y, $x + $dotWidth, $y + $dotHeight, $color);
$x += $dotWidth;
}
$x = 0;
$y += $dotHeight;
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);