<?php
// Convert an image into a palette-based image
$image = imagecreatefrompng(
'https://media.geeksforgeeks.org/wp-content/uploads/col1.png');
imagetruecolortopalette($image, false, 255);
// Search the given rgb color.
$color = array(
array(155, 40, 200, 50),
array(235, 205, 188, 127),
array(135, 00, 132, 0),
);
// Loop to return the closest color match.
foreach($color as $id => $rgb)
{
$output = imagecolorclosestalpha($image, $rgb[0],
$rgb[1], $rgb[2], $rgb[3]);
$output = imagecolorsforindex($image, $output);
$output = "({$output['red']}, {$output['green']},
{$output['blue']}, {$output['alpha']})";
echo "Given color: ($rgb[0], $rgb[1], $rgb[2], $rgb[3])
=> Closest match: $output <br>";
}
imagedestroy($image);
?>