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

imagecolorclosest() function in PHP

WBOY
Release: 2023-09-09 20:02:01
forward
859 people have browsed it

imagecolorclosest() function in PHP

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

Syntax

imagecolorallocatealpha (img, red, green, blue)
Copy after login

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);
?>
Copy after login

Output

The following is the output:

Closest = (44, 118, 140)
Copy after login

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);
?>
Copy after login

Output

Below is the output Result:

Closest = (228, 74, 76)
Copy after login

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

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template