Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Hàm imagecolorclosest() trong PHP

imagecolorclosest() tìm index màu trong palette gần nhất với màu RGB cho trước (theo khoảng cách Euclid trong không gian RGB).

Cú pháp

imagecolorclosest(resource $image, int $red, int $green, int $blue): int

Ví dụ

<?php
$img = imagecreatefrompng('https://example.com/image.png');
imagetruecolortopalette($img, false, 255); // chuyển true color -> palette
$idx = imagecolorclosest($img, 20, 90, 140);
$rgb = imagecolorsforindex($img, $idx);
echo "Closest = ({$rgb['red']}, {$rgb['green']}, {$rgb['blue']})"; // (44, 118, 140)
imagedestroy($img);
?>

Lưu ý: Ảnh true color phải chuyển palette trước (imagetruecolortopalette).