GD, changing an images pixel color, color matching, fuzzy picture
| From: | Lamonte | Date: | Fri, 28 Mar 2008 23:27:24 +0000 |
| Subject: | GD, changing an images pixel color, color matching, fuzzy picture | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-272265@lists.php.net to get a copy of this message | ||
Okay I created a script that changes a basic smiley face into a red smiley face..but it doesn't replace all the yellow, it looks like a yellow shadow in the background:
<?php
$image = "smiley.png";
$data = getimagesize($image);
$width = intval($data[0]);
$height = intval($data[1]);
$cloneH = 0;
$hex = "FF0000";
$oldhex = "FCFF00";
$im = imagecreatefrompng($image);
$color = imagecolorallocate($im,hexdec(substr($hex,0,2)),hexdec(substr($hex,2,2)),hexdec(substr($hex,4,6)));
for($cloneH=0;$cloneH<$height;$cloneH++)
{
for($x=0;$x<$width;$x++)
{
if( colormatch($im,$x,$cloneH, $oldhex) )
imagesetpixel($im, $x, $cloneH, $color);
} }
header("Content-Type: {$data['mime']}");
imagepng($im);
function colormatch($image,$x,$y,$hex)
{
$rgb = imagecolorat($image,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$r2 = hexdec(substr($hex,0,2));
$g2 = hexdec(substr($hex,2,2));
$b2 = hexdec(substr($hex,4,6));
if( $r == $r2 && $b == $b2 && $g == $g2 )
return true;return false; //echo "$r $r2, $g $g2, $b $b2"; } ?>