PHP Conference Japan 2024

imagecolorset

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecolorset設定指定調色盤索引的顏色

說明

imagecolorset(
    GdImage $image,
    int $color,
    int $red,
    int $green,
    整數 (int) $blue,
    整數 (int) $alpha = 0
): ?false

此函數將調色盤中指定的索引設定為指定的顏色。這對於在已設置調色盤的圖像中創建類似填充效果非常有用,而無需執行實際填充的開銷。

參數

image

一個 GdImage 物件,由圖像創建函數之一返回,例如 imagecreatetruecolor()

color

調色盤中的索引。

red

紅色成分的值。

green

綠色成分的值。

blue

藍色成分的值。

alpha

Alpha 成分的值。

返回值

成功時,此函數返回 null,失敗時返回 false

更新日誌

版本 說明
8.0.0 image 現在需要一個 GdImage 實例;先前需要一個有效的 gd 資源 (resource)

範例

範例 #1 imagecolorset() 範例

<?php
// 建立一個 300x100 的圖像
$im = imagecreate(300, 100);

// 將背景設為紅色
imagecolorallocate($im, 255, 0, 0);

// 取得背景的顏色索引
$bg = imagecolorat($im, 0, 0);

// 將背景設為藍色
imagecolorset($im, $bg, 0, 0, 255);

// 將圖像輸出到瀏覽器
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

參見

新增註釋

使用者貢獻的註釋 10 則註釋

moxleystratton.com
18 年前
如果您想將彩色圖像轉換為灰階圖像而不產生斑點狀圖像,請使用以下色彩計算方法:
<?php
function imagetograyscale($im)
{
if (
imageistruecolor($im)) {
imagetruecolortopalette($im, false, 256);
}

for (
$c = 0; $c < imagecolorstotal($im); $c++) {
$col = imagecolorsforindex($im, $c);
$gray = round(0.299 * $col['red'] + 0.587 * $col['green'] + 0.114 * $col['blue']);
imagecolorset($im, $c, $gray, $gray, $gray);
}
}
?>
heavyraptor2 at hotmail dot com
16 年前
我一直想找一個簡單的函式來「上色」圖片,但一直沒找到...看起來很多人對「上色」圖片的理解不同,因為實際上的上色是指將舊顏色的灰階值乘以新的顏色。因此,白色像素將變成 100% 的上色目標顏色,而黑色像素將保持黑色(我知道我解釋得不太好...希望您能理解,否則請參考函式程式碼下方的範例)。

<?php
函式 image_colorize(&$img,$rgb) {
imageTrueColorToPalette($img, true, 256);
$numColors = imageColorsTotal($img);

for (
$x = 0; $x < $numColors; $x++) {
list(
$r,$g,$b) = array_values(imageColorsForIndex($img,$x));

// 計算灰階百分比
$grayscale = ($r + $g + $b) / 3 / 0xff;

imageColorSet($img,$x,
$grayscale * $rgb[0],
$grayscale * $rgb[1],
$grayscale * $rgb[2]
);
}

return
true;
}
?>

使用範例
<?php
$color
= array(0xff,0xaa,0x2a); // 要轉換的顏色
$url = 'http://sundog.net/images/uploads/1_google_logo.jpg';
$img = imageCreateFromJpeg($url);

image_colorize($img,$color);

header('Content-type: image/gif');
imageGif($img);
exit;
?>

盡情享用
dade dot c at email dot it
20 年前
如果您想在您的網站上實現一個顏色主題系統,這會很有幫助... 試試看
Davide Candiloro 義大利

函式 colorize ($pngpath, $r, $g, $b)
/*
必要條件:$pngpath 必須是具有 64 色調色盤的有效灰階 PNG-8 影像路徑
$r, $g, $b 必須是 0..255 範圍內的整數
效果:返回以 $r, $g, $b 表示的顏色著色的 png 影像。
*/
{
header("Content-type: image/png");
$im = imagecreatefrompng("images/table.png");

imagetruecolortopalette($im, FALSE, 256);

for ($c = 0; $c < 64; $c++){ /*64 是 PNG-8 調色盤中的顏色數量*/
$col = imagecolorsforindex($im, $c);
imagecolorset ( $im, $c, $r*$col['red']/256, $g*$col['green']/256, $b*$col['blue']/256); /*將原始灰階調色盤替換為著色調色盤*/
}

imagepng($im);
imagedestroy($im);
}
m4551 at abasoft dot it
20 年前
以下是一個將圖片灰階化的函式,即使來源是真彩色 (jpeg 或 png) 也適用。

品質稍微差一點,但速度非常快...

function imagegreyscale(&$img, $dither=1) {
if (!($t = imagecolorstotal($img))) {
$t = 256;
imagetruecolortopalette($img, $dither, $t);
}
for ($c = 0; $c < $t; $c++) {
$col = imagecolorsforindex($img, $c);
$min = min($col['red'],$col['green'],$col['blue']);
$max = max($col['red'],$col['green'],$col['blue']);
$i = ($max+$min)/2;
imagecolorset($img, $c, $i, $i, $i);
}
}
Thomas Mueller
16 年前
這是一個以漸層樣式為圖片上色的函式。

您只需要傳入一個圖片物件和一個顏色陣列。

例如:

$arr = array('#000000', '#990000', '#00FFFF', '#FFFFDD');
colorize ($img, $arr);

<?php
function colorize($imgdata, $palette)
{
imageTrueColorToPalette($imgdata,false,0xFF);
$l = count($palette)-1;
$i = imagecolorstotal($imgdata);
while (
$i--)
{
list(
$r,$g,$b) = array_values(imageColorsForIndex($imgdata,$i));

$grayscale = ($r*.3 + $g*.59 +$b*.11) / 0xFF;

$pos = $l*$grayscale;

$perc = $pos-floor($pos);

$tbase = str_replace("#", '', $palette[$pos]);
$baseR = hexdec(substr($tbase,0,2));
$baseG = hexdec(substr($tbase,2,2));
$baseB = hexdec(substr($tbase,4,2));

$tmix = str_replace("#", '', $palette[$pos+1]);
$mixR = hexdec(substr($tmix,0,2));
$mixG = hexdec(substr($tmix,2,2));
$mixB = hexdec(substr($tmix,4,2));

$resR = $baseR+($mixR-$baseR)*$perc;
$resG = $baseG+($mixG-$baseG)*$perc;
$resB = $baseB+($mixB-$baseB)*$perc;

imagecolorset($imgdata, $i, $resR, $resG, $resB);
}
}
?>
Daniel Klein
9 年前
如果您要從頭開始建立調色盤圖片,則必須先為每個索引使用 imagecolorallocate(),然後才能在其上使用 imagecolorset()。
Bartman
15 年前
我遇到了與 heavyraptor2 相同的問題,所以我寫了這個函式...
<?php
function imagecolorize($im,$endcolor){
//funcion takes image and turns black into $endcolor, white to white and
//everything in between in corresponding gradient
//$endcolor should be 6 char html color

//make sure it has usable palette
if (imageistruecolor($im)) {
imagetruecolortopalette($im, false, 256);
}

//first make it gray to be sure of even results (thanks moxleystratton.com)
//comment this loop if you want the output based on, for example,
//the red channel (for this take a look at the $gray-var in the last loop)
for ($c = 0; $c < imagecolorstotal($im); $c++) {
$col = imagecolorsforindex($im, $c);
$gray = round(0.299 * $col['red'] + 0.587 * $col['green'] + 0.114 * $col['blue']);
imagecolorset($im, $c, $gray, $gray, $gray);
}

//determine end-colors in hexdec
$EndcolorRGB['r'] = hexdec( substr($endcolor, 0, 2));
$EndcolorRGB['g'] = hexdec( substr($endcolor, 2, 2));
$EndcolorRGB['b'] = hexdec( substr($endcolor, 4, 2));

//determine gradient-delta's
$stepR = (255-$EndcolorRGB['r'])/255.0;
$stepG = (255-$EndcolorRGB['g'])/255.0;
$stepB = (255-$EndcolorRGB['b'])/255.0;

//aColor contains the 256 gradations between endcolor(i=0) and white(i=255)
$aColor = array();
for (
$i = 0; $i<=255; $i++){
$aColor[$i]['r'] = $EndcolorRGB['r'] + ($i*$stepR);
$aColor[$i]['g'] = $EndcolorRGB['g'] + ($i*$stepG);
$aColor[$i]['b'] = $EndcolorRGB['b'] + ($i*$stepB);
}

//for every color-index we now replace $gray-values for $aColor
for ($c = 0; $c < imagecolorstotal($im); $c++){
$currentColorRGB = imagecolorsforindex($im, $c);
$gray = $currentColorRGB['red'];//image is grayscale, so red,green and blue
// should be equal. We use this number as key of aColor
imagecolorset($im,$c,(int)$aColor[$gray]['r'], (int)$aColor[$gray]['g'], (int)$aColor[$gray]['b']);
}
}
?>
olivier at moostik dot net
23 年前
此函式會更改圖片的顏色,每個原色的範圍為 0>100

int ImageSelectiveColor (int im, int red, int green, int blue)

im 是圖片指標
紅色、綠色和藍色的範圍是 0-100

function ImageSelectiveColor($im,$red,$green,$blue)
{
for($i=0;$i<imagecolorstotal($im);$i++)
{
$col=ImageColorsForIndex($im,$i);
$red_set=$red/100*$col['red'];
$green_set=$green/100*$col['green'];
$blue_set=$blue/100*$col['blue'];
if($red_set>255)$red_set=255;
if($green_set>255)$green_set=255;
if($blue_set>255)$blue_set=255;
imagecolorset($im,$i,$red_set,$green_set,$blue_set);

}
return $im;

}
admin at phpgfx dot com
17 年前
以下是一個將圖片轉換為純黑白的函式

<?php

函數 imagepurebw( $img, $amount = 383 ) {
$total = imagecolorstotal( $img );
for (
$i = 0; $i < $total; $i++ ) {
$index = imagecolorsforindex( $img, $i );
array_pop( $index );
$color = ( array_sum( $index ) > $amount ) ? 255 : 0;
imagecolorset( $img, $i, $color, $color, $color );
}
}

?>
info at devking dot com
22 年前
這是一個簡單的將圖片灰階化的函數...

函數 imagecolorgrey( &$img ) {
for( $i=0; $i<imagecolorstotal( $img ); $i++ ) {
$c = ImageColorsForIndex( $img, $i );
$t = ($c["red"]+$c["green"]+$c["blue"])/3;
imagecolorset( $img, $i, $t, $t, $t );
}
}
To Top