文字

imagecolorclosest

(PHP 4, PHP 5, PHP 7)

imagecolorclosest取得与指定的颜色最接近的颜色的索引值

说明

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

返回图像调色板中与指定的 RGB 值最“接近”的颜色。

指定的颜色与调色板中的每个颜色的“距离”的计算方法是把 RGB 值当成三维空间中点的坐标。

如果从文件创建了图像,只有图像中使用了的颜色会被辨析。仅出现在调色板中的颜色不会被辨析。

参见 imagecolorexact()

用户评论:

[#1] info at codeworx dot ch [2011-11-09 02:21:21]

Here is a function that compares two HEX colors for similarity. This can be useful if you want to detect colors that are not different enough to see for the naked eye. It returns a bool: TRUE if the colors are similar to each other (within tolerance) or FALSE if they are different enough.
I tested a few colors and came up with a tolerance of 35 (rgb value - should be between 0 and 255) but you can change that tolerance by passing a third parameter to the function.

<?php
function compareColors ($col1$col2$tolerance=35) {
    
$col1Rgb = array(
        
"r" => hexdec(substr($col102)),
        
"g" => hexdec(substr($col122)),
        
"b" => hexdec(substr($col142))
    );
    
$col2Rgb = array(
        
"r" => hexdec(substr($col202)),
        
"g" => hexdec(substr($col222)),
        
"b" => hexdec(substr($col242))
    );
    return (
$col1Rgb['r'] >= $col2Rgb['r'] - $tolerance && $col1Rgb['r'] <= $col2Rgb['r'] + $tolerance) && ($col1Rgb['g'] >= $col2Rgb['g'] - $tolerance && $col1Rgb['g'] <= $col2Rgb['g'] + $tolerance) && ($col1Rgb['b'] >= $col2Rgb['b'] - $tolerance && $col1Rgb['b'] <= $col2Rgb['b'] + $tolerance);
}
?>

[#2] MagicalTux at FF dot st [2005-06-28 06:22:15]

A way to get each time an answer :

<?php
function imagegetcolor($im$r$g$b) {
        
$c=imagecolorexact($im$r$g$b);
        if (
$c!=-1) return $c;
        
$c=imagecolorallocate($im$r$g$b);
        if (
$c!=-1) return $c;
        return 
imagecolorclosest($im$r$g$b);
}
?>


If the *exact* color is found in the image, it will be returned. If we don't have the exact color, we try to allocate it. If we can't allocate it, we return the closest color in the image.

[#3] Vikrant Korde <vakorde at hotmail dot com> [2003-10-11 08:10:51]

This functuion is useful when you are dealing with previously present images like .png, .jpg, etc. You can use this function for writing a text on the image.

For me the function imagecolorallocate() was returning the -1 value. after lot of RnD and site search i found a function use of imagecolorclosest(). This solved my problem of writing the text on the image with customised color. 

Actually previously it was writing on the image but the color was not distinct. It was using the same color as of that background image. 

The following code segment was fine with me.

header ("Content-type: image/jpeg"); 
$im = imagecreatefromjpeg("BlankButton.jpg");
$white = imageColorClosest($im, 255,255,255);
// this is for TTF fonts
imagettftext ($im, 20, 0, 16, 30, $white, "/usr/X11R6/lib/X11/fonts/TTF/luximb.ttf", "Vikrant"); 

//this is for notmal font
imagestring($im, 4, 0,0,"Korde", $white);
imagejpeg($im,"",150); 
imagedestroy ($im);

上一篇: 下一篇: