2009-01-18 10 views

Respuesta

38

Esto reemplazará el color blanco con gris

$imgname = "test.gif"; 
$im = imagecreatefromgif ($imgname); 

$index = imagecolorclosest ($im, 255,255,255); // get White COlor 
imagecolorset($im,$index,92,92,92); // SET NEW COLOR 

$imgname = "result.gif"; 
imagegif($im, $imgname); // save image as gif 
imagedestroy($im); 

enter image description here

+4

¡esta respuesta me ayudó también después del hecho! GD tiene muchas funciones pero el documento es corto en ejemplos del mundo real. –

+0

@IlmariKaronen: Gracias –

0

No conozco ninguna función preconfigurada. Pero supongo que podría ir a través de cada píxel de la imagen y cambiar su color ...

0

No lo he probado pero puede ver la función imagecolorset() en la biblioteca GD Tiene un efecto de relleno de color, eso podría ayudar con el fondo blanco.

0

Puede probar la función del filtro de imagen http://lv.php.net/imagefilter, pero eso no le dará acceso directo para reemplazar un color por otro, simplemente cambiando los componentes r/g/b.

Se pudo implementar una solución de muy bajo nivel utilizando imagesetpixel http://nl2.php.net/imagesetpixel para establecer los nuevos valores de píxeles.

7

tuve problemas para hacer este trabajo solución. La imagen no puede ser una imagen en color verdadero. Convierta primero con imagetruecolortopalette();

$imgname = "test.gif"; 
$im = imagecreatefromgif ($imgname); 

imagetruecolortopalette($im,false, 255); 

$index = imagecolorclosest ($im, 255,255,255); // get White COlor 
imagecolorset($im,$index,92,92,92); // SET NEW COLOR 

$imgname = "result.gif"; 
imagegif($im, $imgname); // save image as gif 
imagedestroy($im); 
3

Sé que es tarde y después de los hechos, pero he juntado un script que lo hará en una escala ligeramente más grande. Con suerte, alguien que se encuentre con esta publicación puede usarla. Se necesitan varias imágenes de origen que son capas de un solo color (su elección). Usted le proporciona un color de fuente y el tinte de cada capa y la secuencia de comandos devuelve una imagen compuesta (con total transparencia) coloreada específicamente para su código hexadecimal provisto.

Echa un vistazo al código a continuación. Una explicación más detallada se puede encontrar en mi blog.

function hexLighter($hex, $factor = 30) { 
    $new_hex = ''; 

    $base['R'] = hexdec($hex{0}.$hex{1}); 
    $base['G'] = hexdec($hex{2}.$hex{3}); 
    $base['B'] = hexdec($hex{4}.$hex{5}); 

    foreach ($base as $k => $v) { 
     $amount = 255 - $v; 
     $amount = $amount/100; 
     $amount = round($amount * $factor); 
     $new_decimal = $v + $amount; 

     $new_hex_component = dechex($new_decimal); 

     $new_hex .= sprintf('%02.2s', $new_hex_component); 
    } 

    return $new_hex; 
} 

// Sanitize/Validate provided color variable 
if (!isset($_GET['color']) || strlen($_GET['color']) != 6) { 
    header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400); 

    exit(0); 
} 

if (file_exists("cache/{$_GET['color']}.png")) { 
    header('Content-Type: image/png'); 
    readfile("cache/{$_GET['color']}.png"); 

    exit(0); 
} 

// Desired final size of image 
$n_width = 50; 
$n_height = 50; 

// Actual size of source images 
$width = 125; 
$height = 125; 

$image = imagecreatetruecolor($width, $height); 
      imagesavealpha($image, true); 
      imagealphablending($image, false); 

$n_image = imagecreatetruecolor($n_width, $n_height); 
      imagesavealpha($n_image, true); 
      imagealphablending($n_image, false); 

$black = imagecolorallocate($image, 0, 0, 0); 
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127); 

imagefilledrectangle($image, 0, 0, $width, $height, $transparent); 

$layers = array(); 
$layers_processed = array(); 

$layers[] = array('src' => 'layer01.gif', 'level' => 0); // Border 
$layers[] = array('src' => 'layer02.gif', 'level' => 35);  // Background 
$layers[] = array('src' => 'layer03.gif', 'level' => 100); // White Quotes 

foreach ($layers as $idx => $layer) { 
    $img = imagecreatefromgif($layer['src']); 
    $processed = imagecreatetruecolor($width, $height); 

    imagesavealpha($processed, true); 
    imagealphablending($processed, false); 

    imagefilledrectangle($processed, 0, 0, $width, $height, $transparent); 

    $color = hexLighter($_GET['color'], $layer['level']); 
    $color = imagecolorallocate($image, 
     hexdec($color{0} . $color{1}), 
     hexdec($color{2} . $color{3}), 
     hexdec($color{4} . $color{5}) 
    ); 

    for ($x = 0; $x < $width; $x++) 
     for ($y = 0; $y < $height; $y++) 
      if ($black === imagecolorat($img, $x, $y)) 
       imagesetpixel($processed, $x, $y, $color); 

    imagecolortransparent($processed, $transparent); 
    imagealphablending($processed, true); 

    array_push($layers_processed, $processed); 

    imagedestroy($img); 
} 

foreach ($layers_processed as $processed) { 
    imagecopymerge($image, $processed, 0, 0, 0, 0, $width, $height, 100); 

    imagedestroy($processed); 
} 

imagealphablending($image, true); 

imagecopyresampled($n_image, $image, 0, 0, 0, 0, $n_width, $n_height, $width, $height); 

imagealphablending($n_image, true); 

header('Content-Type: image/png'); 
imagepng($n_image, "cache/{$_GET['color']}.png"); 
imagepng($n_image); 

// Free up memory 
imagedestroy($n_image); 
imagedestroy($image); 
Cuestiones relacionadas