PHP Conference Japan 2024

imagefill

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

imagefill填滿

描述

imagefill(
    GdImage $image,
    int $x,
    int $y,
    int $color
): bool

以指定的 colorimage 中從給定的座標 (左上角為 0, 0) 開始執行填滿。

參數

image

一個 GdImage 物件,由影像建立函式之一傳回,例如 imagecreatetruecolor()

x

起始點的 x 座標。

y

起始點的 y 座標。

color

填滿色彩。使用 imagecolorallocate() 建立的色彩識別碼。

傳回值

成功時傳回 true,失敗時傳回 false

變更日誌

版本 描述
8.0.0 image 現在預期是 GdImage 實例;先前,預期是有效的 gd resource

範例

範例 #1 imagefill() 範例

<?php

$im
= imagecreatetruecolor(100, 100);

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

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

上述範例將輸出類似以下內容

Output of example : imagefill()

參見

新增筆記

使用者貢獻的筆記 16 則筆記

25
George Edison
12 年前
使用以下方式,可以建立新的真彩色影像、使用透明色彩填滿它,並將其儲存為 PNG 影像

<?php

$new
= imagecreatetruecolor(320, 320);
$color = imagecolorallocatealpha($new, 0, 0, 0, 127);
imagefill($new, 0, 0, $color);
imagesavealpha($new, TRUE); // 我花了整整 10 分鐘才弄清楚這部分
imagepng($new);

?>

影像需要使用 imagecreatetruecolor() 建立,您必須使用 imagefill() 而不是 imagefilledrectange(),而且您需要呼叫 imagesavealpha()。其他函式呼叫的組合似乎都無法產生預期的結果。
7
Christopher Kramer
15 年前
以下是一個產生漸層的函式。
您指定寬度、高度和 4 種色彩 (4 個角落的色彩)。

將傳回漸層影像的影像控制代碼。
您可以使用 imageecopy 將傳回的影像複製到另一個影像上。
如果您想要產生陰影,這可能會很有幫助。
(「發光效果範例」:產生 8 個漸層,每個邊各一個,每個角落各一個。漸層的外側具有背景色彩,而內側具有亮色,例如白色。)

但請注意:此函式並未針對效能進行最佳化,它在大型影像上可能會變慢。對於陰影,最好快取產生的漸層。

注意:對於漸層,強烈建議使用真彩色。

<?php

function gradient($w=100, $h=100, $c=array('#FFFFFF','#FF0000','#00FF00','#0000FF'), $hex=true) {

/*
產生漸層圖片

作者:Christopher Kramer

參數:
w: 寬度,單位為像素 (px)
h: 高度,單位為像素 (px)
c: 顏色陣列,包含 4 個元素:
$c[0]: 左上角顏色
$c[1]: 右上角顏色
$c[2]: 左下角顏色
$c[3]: 右下角顏色

如果 $hex 為 true (預設值),顏色為十六進位字串,例如 '#FFFFFF' (不是 '#FFF')
如果 $hex 為 false,顏色為包含 3 個元素的陣列,分別是 rgb 值,例如:
$c[0]=array(0,255,255);

*/

$im=imagecreatetruecolor($w,$h);

if(
$hex) { // 將十六進位值轉換為 rgb
for($i=0;$i<=3;$i++) {
$c[$i]=hex2rgb($c[$i]);
}
}

$rgb=$c[0]; // 從左上角顏色開始
for($x=0;$x<=$w;$x++) { // 迴圈處理每一欄
for($y=0;$y<=$h;$y++) { // 迴圈處理每一列
// 設定像素顏色
$col=imagecolorallocate($im,$rgb[0],$rgb[1],$rgb[2]);
imagesetpixel($im,$x-1,$y-1,$col);
// 計算新的顏色
for($i=0;$i<=2;$i++) {
$rgb[$i]=
$c[0][$i]*(($w-$x)*($h-$y)/($w*$h)) +
$c[1][$i]*($x *($h-$y)/($w*$h)) +
$c[2][$i]*(($w-$x)*$y /($w*$h)) +
$c[3][$i]*($x *$y /($w*$h));
}
}
}
return
$im;
}

function
hex2rgb($hex)
{
$rgb[0]=hexdec(substr($hex,1,2));
$rgb[1]=hexdec(substr($hex,3,2));
$rgb[2]=hexdec(substr($hex,5,2));
return(
$rgb);
}

// 使用範例

$image=gradient(300, 300, array('#000000', '#FFFFFF', '#FF0000', '#0000FF'));

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

?>
2
Gromitt
17 年前
如果你需要填滿整個圖片 (例如在建立圖片後立即填滿),請考慮使用 imagefilledrectangle() 來繪製填滿的矩形,這樣效率會更高。

<?php

$gdImage
= imagecreatetruecolor(100, 100);
$gdColor = imagecolorallocate($gdImage, 255, 0, 0); // 紅色
imagefilledrectangle($gdImage, 0, 0, 99, 99, $gdColor);

?>

這樣會比使用 GD 處理像素邏輯更快速。
1
pisu at estmail dot hu
19 年前
棋盤
<?php

$kep
= imagecreate(400,400);
$fekete = imagecolorallocate($kep,0,0,0);
$feher = imagecolorallocate($kep,255,255,255);

//imagefill($kep,50,50,$fekete);
imagefill($kep,50,20,$feher);

for (
$i=1;$i<8;$i++) {
$k = $i*50;
imageline($kep,0,$k,400,$k,$fekete);
imageline($kep,$k,0,$k,400,$fekete);
}

for (
$i=0;$i<8;$i++) {
for (
$j=0;$j<8;$j++) {
$x = $i*50 + 2;
$y = $j*50 + 2;
if ( ( (
$i % 2) + ($j % 2) ) % 2 == 0 ) {
imagefill($kep,$x,$y,$fekete);
}

}
}

// imagecolortransparent($kek,$piros);

header('content-type : image/png');
imagepng($kep);

?>
0
Cypog
16 年前
imagefill 無法處理 alpha 色彩,請改用 imagefilledrectangle。

<?php

header
("Content-Type: image/png");
$im = imagecreatefrompng("img/button.png");
if (empty(
$_GET['alpha']))
{
$_GET['alpha'] = 10;}
$color = imagecolorallocatealpha($im, 255, 255, 255, $_GET['alpha']);
imagefillalpha($im, $color);
imagepng($im);
imagedestroy($im);

function
ImageFillAlpha($image, $color)
{
imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), $color);
}

?>
0
razonklnbd at yahoo dot com
18 年前
我花了兩個多小時尋找一個可以將圖案或檔案填充為背景而不是顏色的函數。但我找不到。所以我開發了以下函數。我認為這個函數可以節省需要它的人的時間…

函數會取得四個參數
1. 主要圖像識別符
2. 圖案圖像識別符
3. 最終圖像寬度
4. 最終圖像高度

如果您設定最終圖像寬度或高度小於主要圖像寬度或高度,則可能會得到錯誤的結果

<?php

function fill_with_patternfile($p_main_im, $p_patternfile_im, $p_width, $p_height){
$pimiX=$p_patternfile_im;
$pw=imagesx($pimiX);
$ph=imagesy($pimiX);
$targetImageIdentifier=imagecreatetruecolor($p_width,$p_height);
if(
$pw<$p_width && $ph<$p_height){
for(
$pX=0;$pX<$p_width;$pX+=$pw){
for(
$pY=0;$pY<$p_height;$pY+=$ph){
imagecopy($targetImageIdentifier,$pimiX,$pX,$pY,0,0,$pw,$ph);
}
}
}else
imagecopy($targetImageIdentifier,$pimiX,0,0,0,0,$pw,$ph);
$w=imagesx($p_main_im);
$h=imagesy($p_main_im);
$nX=0;
if(
$w<$p_width) $nX=intval(($p_width-$w)/2);
$nY=0;
if(
$h<$p_height) $nY=intval(($p_height-$h)/2);
imagecopy($targetImageIdentifier,$p_main_im,$nX,$nY,0,0,$w,$h);
return
$targetImageIdentifier;
}
// 如果您想使用 gif 或 png 檔案作為
// 圖案檔案,您需要變更下面的函式 :)
$pattern_im=imagecreatefromjpeg('logo.jpg');
// 如果您想使用 gif 或 png 檔案作為
// 主要檔案,您需要變更下面的函式 :)
$main_im=imagecreatefromjpeg('r2.jpg');
// 以寬度 500 和高度 500 呼叫函數
// 如果您的寬度和高度小於主要圖像的
// 寬度和高度,那麼您將無法理解任何變更!
$final=fill_with_patternfile($main_im, $pattern_im, 500, 500);
// 檢視圖像並銷毀所有實例
header('Content-type: image/jpeg');
imagejpeg($final);
imagedestroy($final);
imagedestroy($main_im);
imagedestroy($pattern_im);
exit();

?>
0
gelak
19 年前
//一個笑臉 ;]

<?php

header
('Content-type: image/png');

$smile=imagecreate(400,400);
$kek=imagecolorallocate($smile,0,0,255);
$feher=imagecolorallocate($smile,255,255,255);
$sarga=imagecolorallocate($smile,255,255,0);
$fekete=imagecolorallocate($smile,0,0,0);
imagefill($smile,0,0,$kek);

imagearc($smile,200,200,300,300,0,360,$fekete);
imagearc($smile,200,225,200,150,0,180,$fekete);
imagearc($smile,200,225,200,123,0,180,$fekete);
imagearc($smile,150,150,20,20,0,360,$fekete);
imagearc($smile,250,150,20,20,0,360,$fekete);
imagefill($smile,200,200,$sarga);
imagefill($smile,200,290,$fekete);
imagefill($smile,155,155,$fekete);
imagefill($smile,255,155,$fekete);
imagepng($smile);

?>
-1
dunhamzzz [A] gmail
16 年前
我覺得有必要指出,您無法使用 imagecolorallocatealpha 指定的透明顏色進行填充,顏色會填充,但不會有透明度。

我發現最簡單的解決方法是使用 imagefilledrectangle() 函數繪製一個矩形來填充顏色。
-1
aqmprod at iname dot com
24 年前
這個函數似乎無法處理已經透明的圖片。如果你在 x=0, y=0 的位置填充,且圖片仍有你未填充到的透明部分,它們會變成不同的顏色。
未填充到的透明部分,它們會變成不同的顏色。

ImageColorSet 函數似乎是解決方案,但我無法使用透明度。
-1
norxh
21 年前
對於新的圖片,你必須先配置一個顏色,這個函數才能運作。
-3
oleh at it dot wihola dot com
7 年前
使用 $x 和 $y 座標有任何意義嗎?它們對輸出沒有任何影響。我無法繪製一半填充顏色的矩形。這些參數是無用的。
-3
jonathan dot aquino at gmail dot com
18 年前
使用 imageSaveAlpha($image, true); 來保留透明度。
-5
info at educar dot pro dot br
17 年前
範例 1
如果初始座標位於劃定區域之外,則填充區域將位於該區域之外。

<?php
$src621
= imagecreate(200,200);
$clr_1_621 = imagecolorallocate($src621, 255, 255, 0);
$clr_2_621 = imagecolorallocate($src621, 0, 0, 250);
$clr_4_621 = imagecolorallocate($src621, 2,2,55);
imagerectangle($src621, 100, 100, 150, 150, $clr_4_621);
imagefill($src621, 110, 110, $clr_2_621);
header("Content-Type: image/png");
imagepng ($src621);
imagedestroy($src621);
?>

範例 2
如果初始座標位於劃定區域內部,則填充區域將位於該區域內部。
<?php
$src622
= imagecreate(200,200);
$clr_1_622 = imagecolorallocate($src622, 255, 255, 0);
$clr_2_622 = imagecolorallocate($src622, 0, 0, 250);
$clr_4_622 = imagecolorallocate($src622, 2,2,55);
imagerectangle($src622, 100, 100, 150, 150, $clr_4_622);
imagefill($src622, 10, 10, $clr_2_622);
header("Content-Type: image/png");
imagepng ($src622);
imagedestroy($src622);
?>

請參閱其他範例:
http://www.educar.pro.br/a/gdlib/index.php?pn=50&tr=97
-3
colin at galaxyinteractive dot net
19 年前
雖然 imagefilledrectangle 有概述,但我沒有看到這個被記錄下來,一開始對我來說不太明顯

imageSetTile($image,$imageBack);
imagefill($image,0,0,IMG_COLOR_TILED);

將會用紋理填充圖片 (這很棒,因為我正在建立一個 Logo/範本產生器)
-3
Igor Garcia
21 年前
這個函數無法處理透明度。
因此,您需要使用 imagecolorallocate 而不是 imagecolorallocatealpha。
因此,請小心那些已使用 imageallocatecoloralpha 設定的顏色變數,因為這可能會減慢或使您的系統掛起。
-3
Anonymous
21 年前
實際上,它可以處理預先透明的圖片。要移除它,您需要執行類似的操作:
imagecolortransparent($img, 0);
將先前的透明顏色設為 null。 ;)
To Top