Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Ringeril

[Resolvido] Trocar cor - GD

Recommended Posts

Olá pessoal,

 

Estou tentando trocar a cor de uma imagem pelo PHP. A imagem tem AZUL e LARANJA, quero trocar o AZUL por uma cor vinda do GET.

Consegui fazer algo, mas só troca um pixel.

<?php
header("Content-type: image/png");

function HexParaRGB($hex) {
        $hex = ereg_replace("#", "", $hex);
        $cor = array();

        if(strlen($hex) == 3) {
                $cor['r'] = hexdec(substr($hex, 0, 1) . $r);    
                $cor['g'] = hexdec(substr($hex, 1, 1) . $g);
                $cor['b'] = hexdec(substr($hex, 2, 1) . $B);

        }elseif(strlen($hex) == 6) {
                $cor['r'] = hexdec(substr($hex, 0, 2));
                $cor['g'] = hexdec(substr($hex, 2, 2));
                $cor['b'] = hexdec(substr($hex, 4, 2));
        }
        return $cor;
}

$im = imagecreatefrompng("http://localhost:8080/images/teste.png");
$x = 15;
$y = 15;

$rgb = imagecolorat($im, $y, $x); //Procura qual cor está na posição X-Y na imagem.
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$cor = $r.'-'.$g.'-'.$b; //Monta cor em RGB

if($cor == '0-0-255'){ //Se a cor for 0-0-255 (azul) muda para a cor vinda do GET
   $mudaCor = "#".$_GET['cor'];
   $corHex = HexParaRGB($mudaCor); //Converte a cor HEX para RGB
   $color = imagecolorallocate($im, $corHex['r'], $corHex['g'], $corHex['b']); 
   imagesetpixel($im, $x, $y, $color); //Pinta o pixel na posição X-Y
}

imagepng($im);
imagedestroy($im);
?>
Acho que teria que usar um WHILE e aumentar as variveis X e Y. Mas não estou conseguindo fazer.

 

Abraço e obrigado.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Tá vendo essa parte:

 

$x = 15;
$y = 15;

$rgb = imagecolorat($im, $y, $x); //Procura qual cor está na posição X-Y na imagem.

Você está definindo a posição EXATA do pixel a ser trocado. Você deve aplicar a função em dois loops-for aninhados.

 

No primeiro, vai de zero até a largura da imagem, pode chamar de $x.

O segundo, vai de zero até a altura da imagem, pode chamar de $y.

 

Daí dentro desse segundo laço você verifique se é azul ou laranja e troca http://forum.imasters.com.br/public/style_emoticons/default/thumbsup.gif

Compartilhar este post


Link para o post
Compartilhar em outros sites

Tentei fazer, mas agora ele pinta uma listra em diagonal...

<?php
header("Content-type: image/png");

function HexParaRGB($hex) {
        $hex = ereg_replace("#", "", $hex);
        $cor = array();

        if(strlen($hex) == 3) {
                $cor['r'] = hexdec(substr($hex, 0, 1) . $r);    
                $cor['g'] = hexdec(substr($hex, 1, 1) . $g);
                $cor['b'] = hexdec(substr($hex, 2, 1) . $B);

        }elseif(strlen($hex) == 6) {
                $cor['r'] = hexdec(substr($hex, 0, 2));
                $cor['g'] = hexdec(substr($hex, 2, 2));
                $cor['b'] = hexdec(substr($hex, 4, 2));
        }
        return $cor;
}

$im = imagecreatefrompng("http://localhost/images/teste.png");
$x = 0;
$y = 0;
$lar = imagesx($im);
$alt = imagesy($im);

while($y < $alt){
  $rgb = imagecolorat($im, $x, $y);
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8) & 0xFF;
  $b = $rgb & 0xFF;
  $cor = $r.'-'.$g.'-'.$b;

  if($cor == '0-0-255'){ 
    $mudaCor = $_GET['cor'];
    $corHex = HexParaRGB($mudaCor);
    $color = imagecolorallocate($im, $corHex['r'], $corHex['g'], $corHex['b']); 
    imagesetpixel($im, $x, $y, $color);
  }
  ++$y;
  
  $rgb = imagecolorat($im, $x, $y);
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8) & 0xFF;
  $b = $rgb & 0xFF;
  $cor = $r.'-'.$g.'-'.$b;

  if($cor == '0-0-255'){ 
    $mudaCor = $_GET['cor'];
    $corHex = HexParaRGB($mudaCor);
    $color = imagecolorallocate($im, $corHex['r'], $corHex['g'], $corHex['b']); 
    imagesetpixel($im, $x, $y, $color);
  }
  ++$x;
}

imagepng($im);
imagedestroy($im);
?>

O que eu fiz de errado? http://forum.imasters.com.br/public/style_emoticons/default/natal_happy.gif

 

Abraço.

Compartilhar este post


Link para o post
Compartilhar em outros sites

OK, consegui fazer.

<?php
header("Content-type: image/png");

function HexParaRGB($hex) {
        $hex = ereg_replace("#", "", $hex);
        $cor = array();

        if(strlen($hex) == 3) {
                $cor['r'] = hexdec(substr($hex, 0, 1) . $r);    
                $cor['g'] = hexdec(substr($hex, 1, 1) . $g);
                $cor['b'] = hexdec(substr($hex, 2, 1) . $B);

        }elseif(strlen($hex) == 6) {
                $cor['r'] = hexdec(substr($hex, 0, 2));
                $cor['g'] = hexdec(substr($hex, 2, 2));
                $cor['b'] = hexdec(substr($hex, 4, 2));
        }
        return $cor;
}

$im = imagecreatefrompng("http://localhost/images/teste.png");
$x = 0;
$y = 0;
$lar = imagesx($im);
$alt = imagesy($im);

for ($x=0;$x<=$lar;$x++){
  for ($y=0;$y<=$alt;$y++){
    $rgb = imagecolorat($im, $x, $y);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    $cor = $r.'-'.$g.'-'.$b;

    if($cor == '0-0-255'){ 
      $mudaCor = $_GET['cor'];
      $corHex = HexParaRGB($mudaCor);
      $color = imagecolorallocate($im, $corHex['r'], $corHex['g'], $corHex['b']); 
      imagesetpixel($im, $x, $y, $color);
    }
  }
}

imagepng($im);
imagedestroy($im);
?>

Pode fechar.

 

Obrigado Imaggens.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.