| In addition to drawing objects on an existing image, the PHP image functions can also perform transformations on its colors. The main advantage of using index color images with the PHP image functions is that you can globally manipulate colors and color ranges in a predictable manner by altering the index table. This isn't always the case with true color images. |
bool imageColorSet ( resource image, int index, int red, int green, int blue)
Sets the specified index in the palette to the specified color. This is useful for creating flood-fill-like effects in paletted images without the overhead of performing the actual flood-fill. |
| This function basically reassigns the color value of an index in the table. Any pixel in the image set to that index will now display the new color. |
|
|
| There are several functions provided to determine the index of a color within an image by position or color: |
int imageColorAt ( resource image, int x, int y)
Returns the index of the color of the pixel at the specified location in the image specified by image.
int imageColorClosest ( resource image, int red, int green, int blue)
Returns the index of the color in the palette of the image which is "closest" to the specified RGB value. The "distance" between the desired color and each color in the palette is calculated as if the RGB values represented points in three-dimensional space.
int imageColorClosestHWB ( resource image, int x, int y, int color)
Returns the index of the color which has the hue, white and blackness nearest to the given color.
int imageColorExact ( resource image, int red, int green, int blue)
Returns the index of the specified color in the palette of the image. If the color does not exist in the image's palette, -1 is returned.
int imageColorResolve ( resource image, int red, int green, int blue)
This function is guaranteed to return a color index for a requested color, either the exact color or the closest possible alternative.
|
| Note that the colors in an image you might think are 'exact' and 'closest' aren't always the ones that these functions are going to pick. Using these functions with your images may require some trial and erorr.
|
<?PHP
error_reporting(E_ALL ^ E_NOTICE);
/* colorSetPHP.php uses imageColorSet with an input color to alter the PHP logo */
$phpHex = '5B69A6'; $phpColor = hex2int($phpHex);
$newColor = hex2int(validHexColor($_REQUEST['color'],$phpHex)) ;
// should use php_logo_guid(), but that returns a .gif $im = imageCreateFromPNG('php.png');
$phpCIndex = imageColorExact($im,$phpColor['r'],$phpColor['g'],$phpColor['b']);
imageColorSet($im,$phpCIndex,$newColor['r'],$newColor['g'],$newColor['b']);
header('Content-type: image/png'); imagePNG($im); imageDestroy($im);
/** * @param $hex string 6-digit hexadecimal color * @return array 3 elements 'r', 'g', & 'b' = int color values * @desc Converts a 6 digit hexadecimal number into an array of * 3 integer values ('r' => red value, 'g' => green, 'b' => blue) */ function hex2int($hex) { return array( 'r' => hexdec(substr($hex, 0, 2)), // 1st pair of digits 'g' => hexdec(substr($hex, 2, 2)), // 2nd pair 'b' => hexdec(substr($hex, 4, 2)) // 3rd pair ); }
/** * @param $input string 6-digit hexadecimal string to be validated * @param $default string default color to be returned if $input isn't valid * @return string the validated 6-digit hexadecimal color * @desc returns $input if it is a valid hexadecimal color, * otherwise returns $default (which defaults to black) */ function validHexColor($input = '000000', $default = '000000') { // A valid Hexadecimal color is exactly 6 characters long // and eigher a digit or letter from a to f return (eregi('^[0-9a-f]{6}$', $input)) ? $input : $default ; }
?>
|
| A few more useful color functions: |
int imageColorsTotal ( resource image)
Returns the number of colors in the specified image's palette.
int imageColorDeallocate ( resource image, int color)
This function de-allocates a color.
array imageColorsForIndex ( resource image, int index)
This returns an associative array with red, green, blue and alpha keys that contain the appropriate values for the specified color index.
void imageTrueColorToPalette ( resource image, bool dither, int ncolors)
Converts a truecolor image to a palette image. If dither is TRUE then dithering will be used which will result in a more speckled image but with better color approximation. ncolors sets the maximum number of colors that should be retained in the palette. It is usually best to simply produce a truecolor output image instead.
int imageGammaCorrect ( resource image, float inputgamma, float outputgamma)
Applies gamma correction to a GD image stream (image) given an input gamma, inputgamma and an output gamma, outputgamma.
|
| Gamma correction is the ability to correct for differences in how computer systems interpret color values. Macintosh-generated images tend to look too dark on PCs, and PC-generated images tend to look too light on Macs. |