Home  >  Article  >  Backend Development  >  imagecolorclosest() function in PHP

imagecolorclosest() function in PHP

WBOY
WBOYforward
2023-09-09 20:01:16830browse

imagecolorclosest() function in PHP

imagecolorclosest() function gets the index of the color closest to the specified color.

Syntax

imagecolorallocatealpha (img, red, green, blue)

Parameters

  • img: Image resource created using imagecreatetruecolor().

  • red: Red component

  • green: Green component

  • blue: Blue component

Return value

The imagecolorclosest() function returns the closest color in the image palette index of.

Example

The following is an example:

Demonstration

<?php
   $img = imagecreatefrompng(&#39;https://www.tutorialspoint.com/assets/videos/courses/19/images/course_19_image.png&#39;);
   imagetruecolortopalette($img, false, 255);
   $val = imagecolorclosest($img, 20, 90, 140);
   $val = imagecolorsforindex($img, $val);
   $val = "({$val[&#39;red&#39;]}, {$val[&#39;green&#39;]}, {$val[&#39;blue&#39;]})";
   echo "Closest = " . $val;
   imagedestroy($img);
?>

Output

The following is the output:

Closest = (44, 118, 140)

Example

Let’s see another example where we have different image and color components:

Live Demo

<?php
$img = imagecreatefrompng(&#39;http://www.tutorialspoint.com/images/Swift.png&#39;);
imagetruecolortopalette($img, false, 255);
$val = imagecolorclosest($img, 10, 130, 80);
$val = imagecolorsforindex($img, $val);
$val = "({$val[&#39;red&#39;]}, {$val[&#39;green&#39;]}, {$val[&#39;blue&#39;]})";
echo "Closest = " . $val;
imagedestroy($img);
?>

Output

Below is the output Result:

Closest = (228, 74, 76)

The above is the detailed content of imagecolorclosest() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete