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

(singke) #1

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


The imagecolorclosest function returns the index of the color in the given image
closest to the given color. Colors are treated as three-dimensional coordinates, and
closeness is defined by the distance between two points.


<?
/*
* Compare closest color to real color
/


//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 color closest to pure magenta
$magentaIndex = imagecolorclosest($image, 255, 0, 255);


//get RGB values
$colorArray = imagecolorsforindex($image, $magentaIndex);


//allocate closest color
$colorMagenta = imagecolorallocate($image,
$colorArray["red"],
$colorArray["green"],
$colorArray["blue"]);


//draw a square
imagefilledrectangle($image, 10, 10, 100, 100, $colorMagenta);


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


***

Free download pdf