Re: GD, changing an images pixel color, color matching, fuzzy picture

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mar 28, 2008, at 7:38 PM, Lamonte <php4food@xxxxxxxxx> wrote:

Casey wrote:
I have an annoying habit of not using comments :)

Explanations are inline.

On Mar 28, 2008, at 7:10 PM, Lamonte <php4food@xxxxxxxxx> wrote:

Casey wrote:
On Mar 28, 2008, at 4:27 PM, Lamonte <php4food@xxxxxxxxx> wrote:

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";
}
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Hi!

I have absolutely no clue if this will work. This has been typed directly into my mail client, so no guarantees.

<?php
function toRGB($color) {
return array('r' => ($color >> 16) & 0xFF, 'g' => ($color >> 8) & 0xFF, 'b' => $color & 0xFF);
}
This function returns an array of red, green, and blue from an integer like 0xFFFFFF.


function toColor($r, $g, $b) {
return $r * $g * $b;
}
This function is basically the opposite of the above. (the integer value from red, green, and blue)


$image = 'smiley.png';
list($w, $h) = getimagesize($image);
$im = imagecreatefrompng($image);

for ($y = 0; $y < $h; $y++) {
for ($x = 0; $x < $w; $x++) {
Loop through the pixels.

 extract(toRGB(imagecolorat($im, $x, $y)));
Take the values from the array return of toRGB so we can use $r, $g, and $b instead of $array['r'], etc.

 if ($r >= 0xCC && $g >= 0xCC && $b <= 0x33)
After some trial and error (looking at color charts), any red > CC, green > CC, and blue < 33 is some shade of yellow.

  imagesetpixel($im, $x, $y, toColor(($r + $g) / 2, 0, $b));
The expression inside toColor() is my attempt to calculate the correct shade of red from the shade of yellow.

}
}

header('Content-type: image/png');
imagepng($im);
?>

:)

I don't understand half of that, can you explain what you did? (it works) I was more trying to "fix" my problem then recoding the whole thing though.
I hope that helps.

The thing is, it returns black. not red.
Oh. Replace the toColor function with the imagecolorallocate function and add $im as the first parameter.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux