PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imagecolorclosestalpha> <imagecolorat
Last updated: Fri, 29 Aug 2008

view this page in

imagecolorclosest

(PHP 4, PHP 5)

imagecolorclosest指定した色に最も近い色のインデックスを取得する

説明

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

指定した RGB 値に「近い」 画像パレット中の色のインデックスを返します。

指定した色とパレット上の各色の「距離」は、 RGB 値が三次元空間上の点の座標を表すと考えて計算します。

もし ファイルからイメージを生成した場合、イメージに使用されている色だけが 解決されます。パレットだけに存在する色は解決されません。

パラメータ

image

imagecreatetruecolor() のような画像作成関数が返す画像リソース。

red

赤コンポーネントの値。

green

緑コンポーネントの値。

blue

青コンポーネントの値。

色のパラメータは、0 から 255 までの整数値か 0x00 から 0xFF までの十六進値を指定します。

返り値

画像パレット内で、指定した色にいちばん近い色のインデックスを返します。

例1 画像内での色セットの検索

<?php
// 画像を作成し、パレット画像に変換します
$im imagecreatefrompng('figures/imagecolorclosest.png');
imagetruecolortopalette($imfalse255);

// 探したい色 (RGB)
$colors = array(
    array(
254145154),
    array(
153145188),
    array(
15390145),
    array(
25513792)
);

// それぞれを検索し、パレット内でもっとも近い色を見つけます
// 検索番号、検索した RGB、そして見つかった RGB を返します
foreach($colors as $id => $rgb)
{
    
$result imagecolorclosest($im$rgb[0], $rgb[1], $rgb[2]);
    
$result imagecolorsforindex($im$result);
    
$result "({$result['red']}, {$result['green']}, {$result['blue']})";

    echo 
"#$id: Search ($rgb[0], $rgb[1], $rgb[2]); Closest match: $result.\n";
}

imagedestroy($im);
?>

上の例の出力は、たとえば 以下のようになります。

#0: Search (254, 145, 154); Closest match: (252, 150, 148).
#1: Search (153, 145, 188); Closest match: (148, 150, 196).
#2: Search (153, 90, 145); Closest match: (148, 90, 156).
#3: Search (255, 137, 92); Closest match: (252, 150, 92).



add a note add a note User Contributed Notes
imagecolorclosest
MagicalTux at FF dot st
28-Jun-2005 10:22
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.
Vikrant Korde <vakorde at hotmail dot com>
12-Oct-2003 12:10
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);

imagecolorclosestalpha> <imagecolorat
Last updated: Fri, 29 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites