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.
imagecolorclosest
(PHP 4, PHP 5)
imagecolorclosest — Belirtilen rengin en yakın benzerinin indisini döndürür
Açıklama
Bu işlev, bileşenleri belirtilen rengin en yakın benzerinin indisini döndürür.
İstenen renk ile paletteki renk arasındaki "mesafe" rengin bileşenlerinden oluşan üç boyutlu uzayda hesaplanır.
Eğer resmi bir dosyadan oluşturmuşsanız sadece resimde kullanılan renkler çözümlenir. Palette bulunan renklerden resimde kullanılmamış olanlar çözümlenmez.
Değiştirgeler
- resim
-
imagecreatetruecolor() gibi bir resim oluşturma işlevinden dönen bir resim verisi.
- kırmızı
-
Rengin kırmızı bileşeninin değeri.
- yeşil
-
Rengin yeşil bileşeninin değeri.
- mavi
-
Rengin mavi bileşeninin değeri.
Renk bileşenlerinin değerleri onluk tabanda 0-255 aralığında, onaltılık tabanda 0x00-0xFF aralığında belirtilebilir.
Dönen Değerler
Paletteki en yakın rengin indisi döner.
Örnekler
Örnek 1 - Bir resimde renk aramak
<?php
// Bir resim açıp paletli resme dönüştürelim
$im = imagecreatefrompng('figures/imagecolorclosest.png');
imagetruecolortopalette($im, false, 255);
// Aranacak renkler (RGB)
$colors = array(
array(254, 145, 154),
array(153, 145, 188),
array(153, 90, 145),
array(255, 137, 92)
);
// Renkleri bulmak için bir döngü kuralım.
// En yakın rengin indisini alıp o indisin rengini öğrenelim
foreach($colors as $id => $rgb)
{
$res = imagecolorclosest($im, $rgb[0], $rgb[1], $rgb[2]);
$res = imagecolorsforindex($im, $res);
$res = "({$res['red']}, {$res['green']}, {$res['blue']}, {$res['alpha']})";
echo "#$id: Aranan ($rgb[0], $rgb[1], $rgb[2], $rgb[3]); En yakın $res.\n";
}
imagedestroy($im);
?>
Yukarıdaki örnek şuna benzer bir çıktı üretir:
#0: Aranan (254, 145, 154); En yakın (252, 150, 148). #1: Aranan (153, 145, 188); En yakın (148, 150, 196). #2: Aranan (153, 90, 145); En yakın (148, 90, 156). #3: Aranan (255, 137, 92); En yakın (252, 150, 92).
Ayrıca Bakınız
- imagecolorexact() - Belirtilen rengin indisini döndürür
- imagecolorclosestalpha() - Alfası ile birlikte belirtilen rengin en yakın benzerinin renk indisini verir
- imagecolorclosesthwb() - En yakın renk sıcaklığına, beyaz ve siyahlığa sahip renk indisini verir
imagecolorclosest
28-Jun-2005 01:22
11-Oct-2003 03: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);
