GD_Image, imagecopyresample, dithering the transparent color
am 22.10.2007 08:42:41 von Andy McAllisterHello - I'm using imagecopyresample() to resize a GIF line-art type
image onto a polygon created with GD_Image in PHP. Each has a
transparent color (same color, I think!). The transparent color is
dithering during the resize, though. Result is, some pixels aren't
transparent anymore until I search the whole image for colors close
rgb-wise to the original transparent color, and put it back to
transparent. This color replacement step is a 400% performance hit in
my case (these aren't huge images).
Is there a way to just dither the non-transparent colors?
Or can I more easily find/replace the dithered colors back to the
original color? Here's the color replace function:
// replace a color with a new color
function change_color($image, $old_color, $new_color, $threshold) {
$image_width = imagesx($image);
$image_height = imagesy($image);
$r_old = ($old_color >> 16) & 0xFF;
$g_old = ($old_color >> 8) & 0xFF;
$b_old = $old_color & 0xFF;
// iterate through x axis
for ($x = 0; $x < $image_width; $x++) {
// iterate through y axis
for ($y = 0; $y < $image_height; $y++) {
// look at current pixel
$pixel_color = imagecolorat($image, $x, $y);
$rgb = imagecolorsforindex($image, $pixel_color );
$delta = abs( $rgb['red'] - $r_old ) + abs( $rgb['green'] -
$g_old ) + abs( $rgb['blue'] - $b_old );
// sum RGB deltas, and compare to thresh
if ( $delta <= $threshold && $delta != 0 ) {
// replace with new color
imagesetpixel($image, $x, $y, $new_color);
}
}
}
}