note 68253 deleted from ref.image by tularis
| From: | tularis@php.net | Date: | Sun, 29 Jun 2008 21:01:49 +0000 |
| Subject: | note 68253 deleted from ref.image by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140923@lists.php.net to get a copy of this message | ||
Note Submitter: g8z at yahoo dot com
----
<?php
/**
change_color replaces $old_color with $new_color. $threshold allows you to specify how far away from
the specified color a pixel can be and still be considered a match. $old_color and $new_color must
be decimal color values.
Courtesy of the $5 Script Achive: http://www.tufat.com
*/
function change_color($image, $old_color, $new_color, $threshold = 15)
{
$image_width = imagesx($image);
$image_height = imagesy($image);
// 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);
if (($pixel_color <= $old_color + $threshold) && ($pixel_color >= $old_color -
$threshold)) {
// replace with new color
imagesetpixel($image, $x, $y, $new_color);
}
}
}
}
// EXAMPLE:
// convert all red in the image to green
$image = imagecreatefromjpeg('./test.jpg');
$red_rbg = 16646146;
$green_rbg = 65341;
change_color($image, $red_rbg, $green_rbg, 15);
imagejpeg($image, './test_changed.jpg');
?>