Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

integer imagecolorexact(integer image, integer red, integer
green, integer blue)


Use the imagecolorexact function to find the index of the color in the given image that
matches the given color exactly. If the color doesn't exist, negative one (-1) is returned.


?<
/
Check that an image contains black
If so, change all black to cyan.
/


//attempt to open image, suppress error messages
if(!($image = @imagecreatefromjpeg("php_lang.jpg")))
{
//error, so create an error image and exit
$image = imagecreate(200,200);
$colorWhite = imagecolorallocate($image, 255, 255, 255);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $colorWhite);
imagestring($image, 4, 10, 10, "Couldn't load image!",
$colorBlack);
header("Content-type: image/jpeg");
imagejpeg($image);
exit();
}


//find index of black
$blackIndex = imagecolorexact($image, 0, 0, 0);


if($blackIndex >= 0)
{
//make all black areas cyan
imagecolorset($image, $blackIndex, 0, 255, 255);
}


//send image
header("Content-type: image/jpeg");
imagejpeg($image);
?>


integer imagecolorresolve(integer image, integer red, integer
green, integer blue)


The imagecolorresolve function returns a color identifier based on a specified color. If
the color does not exist in the image's palette, it will be added. In the event that the color
cannot be added, an identifier for the closest color will be returned.


?<
/*

Free download pdf