Contacto | #php_para_torpes | Enlázanos | ¿Quiénes somos?
23 usuarios Online (0)
Darse de alta en la web | Recuperar password   
Inicio / Desafios / Pintando a Nemo / Solucion de Arias

Desafío Pintando a Nemo

#3 Solución de Arias

Código de la Solución Ver código | Comentarios Ver comentarios (14) | Descargar Descargar código

Fecha: 16 abril 2005

Tamaño: 20613 caracteres

Comentarios: 14

Solución online:
http://www.elleondeoro.com/nemo.php

Premios
Código premiado Mejor Presentación!

Valoración PuntuaciónPuntuaciónPuntuaciónPuntuaciónPuntuaciónPuntuaciónPuntuaciónPuntuaciónPuntuaciónPuntuación 9.69 (13 votos)

"Ahi queda eso :)"

Valora esta solución

<?php

/**
*
* Creado y desarrollado por Iván Arias
* para el desafio Pintando a Nemo de PHP-Hispano
*
* www.php-hispano.net
* arias@elleondeoro.com
*
*/

set_time_limit(0);

$inicio = getmicrotime();

$archivo = "nemo/".str_replace(".", "-", $_SERVER['REMOTE_ADDR']).".png";

if (isset(
$_GET['clear']) && is_file($archivo))
  
unlink($archivo);

if (!
is_file($archivo)) {
  
copy ("nemo.png", $archivo);
  
chmod($archivo, 0666);
}

if (!empty(
$_POST)) {
  
$nemo = imagecreatefrompng($archivo);
  
pintar($nemo, $_POST['posX'], $_POST['posY'], color2int($nemo, $_POST['color']), color2int($nemo, '#000000'));
  
imagepng($nemo, $archivo);
}

function
pintar ($imagen, $x, $y, $color, $border) {
  
$cola = array();
  
$cola[] = array($x, $y);
  
$sizex = imagesx($imagen)-1;
  
$sizey = imagesy($imagen)-1;
  while (
count($cola)) {
    
$aux = array_shift($cola);
    if (
$aux[0] < 0 || $aux[1] < 0 || $aux[0] > $sizex || $aux[1] > $sizey) continue;
    
$color_actual = imagecolorat($imagen, $aux[0], $aux[1]);
    if (
$color_actual == $color || $color_actual == $border) continue;
    
imagesetpixel($imagen, $aux[0], $aux[1], $color);
    
array_push($cola, array($aux[0]-1, $aux[1]), array($aux[0]+1, $aux[1]), array($aux[0], $aux[1]-1), array($aux[0], $aux[1]+1));
  }
}

function
int2color ($imagen, $rgb) {
  
$color = imagecolorsforindex($imagen, $rgb);
  return
strtoupper('#'.sprintf('%02s%02s%02s', dechex($color['red']), dechex($color['green']), dechex($color['blue'])));
}

function
color2int ($imagen, $color) {
  
$r = hexdec(substr($color, 1, 2));
  
$g = hexdec(substr($color, 3, 2));
  
$b = hexdec(substr($color, 5, 2));
  return ((
$return = imagecolorexact($imagen, $r, $g, $b)) == -1)? imagecolorallocate($imagen, $r, $g, $b) : $return;
}

function
getmicrotime() {
  list (
$micro, $time) = explode(" ", microtime());
  return
$time + $micro;
}

?>

<html>
<head>
<script>
if (!document.all) document.captureEvents(Event.MOUSEMOVE)

var posX = 0
var posY = 0

function mousePos(e) {
  if (document.all) {
    posX = e.clientX + document.body.scrollLeft;
    posY = e.clientY + document.body.scrollTop;
  } else {
    posX = e.pageX;
    posY = e.pageY;
  }
  if (posX < 0) posX = 0;
  if (posY < 0) posY = 0;
  document.getElementById('posX').value = posX-8;
  document.getElementById('posY').value = posY-8;
}

function setColor(color) {
  if (color.length !=7) return;
  document.getElementById('actual').style.background = color;
  document.getElementById('color').value = color;
  document.getElementById('introducir').value = color;
}
</script>
<style>
body {
  padding: 8;
  margin: 0;
}
.imagen {
  border: 1px solid black;
  padding: 0;
}
.colores {
  border: 1px solid black;
  padding: 8px;
  vertical-align: top;
}
.color {
  border: 1px solid black;
  width: 30%;
}
</style>
</head>
<body>
<table width="100%" cellspacing="0" style="padding: 0; margin: 0; spacing: 0;">
<tr>
  <td class="imagen"><img src="<?=$archivo."?".md5(time());?>" onmousemove="mousePos(event);" onclick="document.getElementById('pintar').submit();"></td>
  <td class="colores" width="100%">
  <p>Colores:</p>
  <center><table width="80%">
  <tr>
    <td class="color" style="background-color: #fff;" onclick="setColor('#ffffff');">&nbsp;</td>
    <td class="color" style="background-color: #787878;" onclick="setColor('#787878');">&nbsp;</td>
    <td class="color" style="background-color: #ccc;" onclick="setColor('#cccccc');">&nbsp;</td>
  <tr>
  <tr>
    <td class="color" style="background-color: #f00;" onclick="setColor('#ff0000');">&nbsp;</td>
    <td class="color" style="background-color: #ff7878;" onclick="setColor('#ff7878');">&nbsp;</td>
    <td class="color" style="background-color: #fcc;" onclick="setColor('#ffcccc');">&nbsp;</td>
  <tr>
  <tr>
    <td class="color" style="background-color: #0f0;" onclick="setColor('#00ff00');">&nbsp;</td>
    <td class="color" style="background-color: #78ff78;" onclick="setColor('#78ff78');">&nbsp;</td>
    <td class="color" style="background-color: #cfc;" onclick="setColor('#ccffcc');">&nbsp;</td>
  <tr>
  <tr>
    <td class="color" style="background-color: #00f;" onclick="setColor('#0000ff');">&nbsp;</td>
    <td class="color" style="background-color: #7878ff;" onclick="setColor('#7878ff');">&nbsp;</td>
    <td class="color" style="background-color: #ccf;" onclick="setColor('#ccccff');">&nbsp;</td>
  <tr>
  <tr>
    <td class="color" style="background-color: #ff0;" onclick="setColor('#ffff00');">&nbsp;</td>
    <td class="color" style="background-color: #ffff78;" onclick="setColor('#ffff78');">&nbsp;</td>
    <td class="color" style="background-color: #ffc;" onclick="setColor('#ffffcc');">&nbsp;</td>
  <tr>
  <tr>
    <td class="color" style="background-color: #f0f;" onclick="setColor('#ff00ff');">&nbsp;</td>
    <td class="color" style="background-color: #ff78ff;" onclick="setColor('#ff78ff');">&nbsp;</td>
    <td class="color" style="background-color: #fcf;" onclick="setColor('#ffccff');">&nbsp;</td>
  <tr>
  <tr>
    <td class="color" style="background-color: #0ff;" onclick="setColor('#00ffff');">&nbsp;</td>
    <td class="color" style="background-color: #78ffff;" onclick="setColor('#78ffff');">&nbsp;</td>
    <td class="color" style="background-color: #cff;" onclick="setColor('#ccffff');">&nbsp;</td>
  <tr>
  <tr>
    <td>Introducir:</td>
    <td colspan="2"><input type="text" id="introducir" onkeyup="setColor(this.value);"></td>
  </tr>
  <tr>
    <td>Actual:</td>
    <td id="actual" class="color">&nbsp;</td>
    <td></td>
  </tr>
  <tr>
  <td colspan="3"><a href="nemo.php?clear">Reiniciar imagen</a></td>
  </tr>
  </table></center>
  </td>
</tr>
<tr>
<td colspan="2" align="center"><a href="mailto:arias@elleondeoro.com">Iván Arias</a> para <a href="http://www.php-hispano.net">PHP-Hispano</a> - Generado en <?=(getmicrotime()-$inicio);?> segundos</td>
</tr>
</table>
<form id="pintar" action="nemo.php" method="post">
<input type="hidden" id="posX" name="posX" value="">
<input type="hidden" id="posY" name="posY" value="">
<input type="hidden" id="color" name="color" value="#ffffff">
</form>
</body>
</html>

Web alojada en Zilos

php-hispano.net 2002 - 2008 | XHTML 1.0
Datos Legales | Webmaster