2024 年 PHP Conference Japan

imagefilledrectangle

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

imagefilledrectangle繪製填滿的矩形

說明

imagefilledrectangle(
    GdImage $image,
    int $x1,
    int $y1,
    整數 $x2,
    整數 $y2,
    整數 $color
): 布林值

在給定的 image 影像中,從點 1 開始到點 2 結束,建立一個填滿 color 顏色的矩形。0, 0 是影像的左上角。

參數

image

一個 GdImage 物件,由其中一個影像建立函式返回,例如 imagecreatetruecolor()

x1

點 1 的 x 座標。

y1

點 1 的 y 座標。

x2

點 2 的 x 座標。

y2

點 2 的 y 座標。

color

填充顏色。使用 imagecolorallocate() 建立的顏色識別碼。

返回值

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

更新日誌

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

範例

範例 #1 imagefilledrectangle() 用法

<?php
// 建立一個 55x30 的影像
$im = imagecreatetruecolor(55, 30);
$white = imagecolorallocate($im, 255, 255, 255);

// 畫一個白色的矩形
imagefilledrectangle($im, 4, 4, 50, 25, $white);

// 儲存影像
imagepng($im, './imagefilledrectangle.png');
imagedestroy($im);
?>

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

Output of example : imagefilledrectangle()

新增註解

使用者貢獻的註解 14 則註解

michal dot kocarek at seznam dot cz
20 年前
如果您想繪製一個圓角矩形,您可以使用這個簡單的函式...
矩形從 x1y1 開始,到 x2y2 結束。 $radius 定義圓角的半徑。

<?

function ImageRectangleWithRoundedCorners(&$im, $x1, $y1, $x2, $y2, $radius, $color) {
// 繪製沒有圓角的矩形
imagefilledrectangle($im, $x1+$radius, $y1, $x2-$radius, $y2, $color);
imagefilledrectangle($im, $x1, $y1+$radius, $x2, $y2-$radius, $color);
// 繪製圓角
imagefilledellipse($im, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
}

?>
olivier dot pons at google dot mail dot com
12 年前
我想要清除一張圖片,並將其設為完全透明。
imagefilledrectangle() 似乎忽略了 Alpha 色板和 Alpha 混合。
請改用 imagefill()

<?php
$w
= imagesx($final);
$h = imagesy($final);
$grande = imagecreatetruecolor($w, $h);

// 開啟 Alpha 混合以使用 Alpha 色板
imagealphablending($grande, true);
// 配置一個透明顏色並用它填充新圖像。
// 如果沒有這個,圖像將會有一個黑色背景而不是透明的。
$transparent = imagecolorallocatealpha($grande, 0, 0, 0, 127);
// 透明 Alpha 值將被 _忽略_:
imagefilledrectangle($grande, 0, 0, $w, $h, $transparent);
// 好的,透明度將會被使用並將整個 Alpha 色板設為透明:
imagefill($grande, 0, 0, $transparent);
?>
administrador(ensaimada)sphoera(punt)com
18 年前
我製作了一個用於製作全彩漸層的函式

<?php

// The image must be in truecolor mode!!
function gradient_region($img, $x, $y, $width, $height,$src_color, $dest_color=0){
$src_alpha = ($src_color) >> 24;
$src_red = ($src_color & 0xFF0000) >> 16;
$src_green = ($src_color & 0x00FF00) >> 8;
$src_blue = ($src_color & 0x0000FF);

$dest_alpha = ($dest_color) >> 24;
$dest_red = ($dest_color & 0xFF0000) >> 16;
$dest_green = ($dest_color & 0x00FF00) >> 8;
$dest_blue = ($dest_color & 0x0000FF);


$inc_alpha = ($dest_alpha - $src_alpha) / $width;
$inc_red = ($dest_red - $src_red)/$width;
$inc_green = ($dest_green - $src_green)/$width;
$inc_blue = ($dest_blue - $src_blue)/$width;

// If you need more performance, the step can be increased
for ($i=0;$i<$width;$i++){
$src_alpha += $inc_alpha;
$src_blue += $inc_blue;
$src_green += $inc_green;
$src_red += $inc_red;
imagefilledrectangle($img,
$x+$i,$y,
$x+$i,$y+$height,
imagecolorallocatealpha($img,
$src_red,$src_green,$src_blue,$src_alpha));
}
}
?>

更多函式請見 http://www.sphoera.com
saramg at uclink dot berkeley dot edu
23 年前
需要注意的重要特性

雖然 imagerectangle 允許您使用不同的座標順序(例如左下到右上),但 imagefilledrectangle 只有在您使用文件中指示的左上到右下時才能正常工作。
info at 555webdesign dot com
18 年前
謝謝 terereese。我花了兩個多小時才搞清楚這個問題。
它在本機可以運作:imagefilledrectangle(imagresource, int x1, int x2, int y1, inty2, color)

但在我的遠端供應商上,只有這樣才能運作:imagefilledrectangle(imagresource, int x1, int y2, int x1, inty1, color)

有什麼想法嗎?為什麼會這樣?
Google
17 年前
<?php
//index.php
//set your year, month, daym hour, minute, second you want to cuuntdown to.

//ONLY CHANGE FROM HERE
$year="2006";
$month="12";
$day="25";
$hour="00";
$minute="00";
$second="00";
$event="Christmas Day 2006";

$time=mktime($hour, $minute, $second, $month, $day, $year);

$timecurrent=date('U');
$cdtime=$time-$timecurrent;
$cdmonths=$cddays/30;
$cdyears=$cddays/365;

//Used this case only...
$cdminutes=round($cdtime/60);
//cdtime is seconds
$cdhours=round($cdtime/3600);
$cddays=round($cdhours/24);

//String the date
$currentdate = date('l, F j, Y');
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(701, 355);

//Temp BGCOLOR (center of c-finder)
$bg1 = 208;
$bg2 = 130;
$bg3 = 208;

$s1 = $bg1 - 20;
$s2 = $bg2 - 20;
$s3 = $bg3 - 20;

$t1 = $bg1 + 30;
$t2 = $bg2 + 30;
$t3 = $bg3 + 30;

$cArray=array();
$c1 = imagecolorallocate($im, $bg1, $bg2, $bg3); //Background
$c8 = imagecolorallocate($im, 255, $bg2, 255); //Background
$c2 = imagecolorallocate($im, $s1, $s2, $s3); //Shadow
$c3 = imagecolorallocate($im, $t1, $t2, $t3); //Text

imagefilledrectangle($im, 0, 0, 701, 50, $c1);

imagefilledrectangle($im, 0, 0, 701, 50, $c8);

// The text to draw
$text = $string;
// Replace path by your own font path
$fnum = rand(1, 9);
$font = "/f/font ($fnum)";

// Add some shadow to the text
imagettftext($im, 29, 1, 17, 42, $c2, $font, "Today is:");
imagettftext($im, 28, -1, 15, 40, $c3, $font, "Today is:");
imagettftext($im, 29, 1, 17, 92, $c2, $font, "...$currentdate...");
imagettftext($im, 28, -1, 15, 90, $c3, $font, "...$currentdate...");
imagettftext($im, 29, 1, 17, 142, $c2, $font, "So there are exactly:");
imagettftext($im, 28, -1, 15, 140, $c3, $font, "So there are exactly:");
imagettftext($im, 29, 1, 17, 192, $c2, $font, "$cddays with just...");
imagettftext($im, 28, -1, 15, 190, $c3, $font, "$cddays days with just...");
imagettftext($im, 29, 1, 17, 242, $c2, $font, "$cdminutes minutes and only...");
imagettftext($im, 28, -1, 15, 240, $c3, $font, "$cdminutes minutes and only...");
imagettftext($im, 29, 1, 17, 292, $c2, $font, "$cdseconds seconds until...");
imagettftext($im, 28, -1, 15, 290, $c3, $font, "$cdseconds seconds until...");
imagettftext($im, 29, 1, 17, 342, $c2, $font, "- + $event + -");
imagettftext($im, 28, -1, 15, 340, $c3, $font, "- + $event + -");

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Darren Edale
11 年前
使用矩形填滿的問題是由於您的程式碼在渲染填滿的矩形之前開啟了 Alpha 混合。Alpha 混合會導致您在影像上繪製的內容根據每個 Alpha 通道與影像上已有的內容混合。因此,由於混合已開啟,並且由於矩形的填充顏色完全透明,因此現有影像內容與透明矩形的混合不會導致現有影像發生任何變化。

關閉混合後,在影像上繪製時,您繪製的內容會完全取代已有的內容。因此,在這種情況下繪製矩形會導致影像的原始內容被完全透明的矩形取代。

因此,為了使用 imagefilledrectangle() 將影像擦除為透明,您需要先關閉 Alpha 混合。

我猜 imagefill() 在開啟 Alpha 混合的情況下可以運作的原因是因為它不執行任何 Alpha 混合 - 無論設定如何,它始終在沒有 Alpha 混合的情況下運作。我懷疑這樣做的原因與 Alpha 通道使邊緣檢測複雜化有關。

我建議使用 imagefilledrectangle() 來建立空白透明影像資源,而不是 imagefill(),因為它在幾乎所有情況下無疑都更快。

以下是一些將影像清空為透明的範例程式碼,假設 $im 是一個成功建立的影像

<?php
$transparent
= imagecolorallocatealpha($im, 0, 0, 0, 127);
imagealphablending($im, false);
imagefilledrectangle($im, 0, 0, imagesx($im) - 1, imagesy($im) - 1, $transparent);
imagecolordeallocate($transparent);
imagealphablending($im, true);
?>
mike at delenk dot net
16 年前
由於對文字在方塊中的定位方式有所誤解,imagettfbbox() 函數中的範例給我帶來了許多問題。

所以我做了一個新的範例
- 撰寫文字
- 在方框中的正確位置
- 周圍帶有填充

享受吧!
Mike

<?

// 一些設定
$angle = 0;
$text = 'Ühg 0123456789';
$font_face = dirname(__FILE__).'/verdana.ttf'; // 將檔案放在同一個目錄中
$font_size = 9; //(int) GD 1 中的像素,或 GD 2 中的點數

// 取得文字方框
$box = imagettfbbox($font_size, 0, $font_face, $text);
$bottom_left_x = $box[0]; // 下方使用
$bottom_left_y = $box[1]; // 下方使用
$bottom_right_x = $box[2]; // 下方使用
$bottom_right_y = $box[3];
$top_right_x = $box[4];
$top_right_y = $box[5];
$top_left_x = $box[6];
$top_left_y = $box[7]; // 下方使用

// 定義文字方框的寬度和高度
$box_w = abs($bottom_left_x) + abs($bottom_right_x);
$box_h = abs($bottom_left_y) + abs($top_left_y);

// 加入填充
$padding_x = 5;
$padding_y = 5;
$box_w = $box_w + 2 * $padding_x;
$box_h = $box_h + 2 * $padding_y;

// 文字的原點 = 第一個字元的基線
$text_x = abs($bottom_left_x) -1 + $padding_x;
$text_y = $box_h -1 - abs($bottom_left_y) - $padding_y;

// 對於像 pgjq 這樣向下延伸的字元的修正
if(abs($bottom_left_y) <= 1) $box_h--;

// 建立影像
$img = imagecreatetruecolor($box_w, $box_h);

// 定義一些顏色
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
$lightgrey = imagecolorallocate($img, 200, 200, 200);
$grey = imagecolorallocate($img,100,100,100);
$yellow = imagecolorallocate($img, 0xFF, 0xFF, 0x00);

// 指定顏色
$font_color = $black;
$padding_color = $lightgrey;
$background_color = $yellow;

// 以背景顏色填滿影像
imagefill($img, 0, 0, $background_color);

// 以填充顏色填滿影像
imagefilledrectangle($img, 0, 0, $box_w, $padding_y - 1, $padding_color); // 上方
imagefilledrectangle($img, 0, 0, $padding_x - 1, $box_h - 1, $padding_color); // 左方
imagefilledrectangle($img, 0, $box_h - $padding_y -1, $box_w, $box_h - 1, $padding_color); // 下方
imagefilledrectangle($img, $box_w - $padding_x, 0, $box_w - 1, $box_h - 1, $padding_color); // 右方

//寫入文字
imagettftext($img, $font_size, 0, $text_x, $text_y, $font_color, $font_face, $text);

//旋轉影像
if ($angle > 0) $img = imagerotate($img, $angle, $white);

// 傳送標頭
header("Content-type: image/gif");

//傳送影像
imagegif($img);
imagedestroy($img);
?>
booga
20 年前
使用 imagerectangle 建立百分比條的簡單方法

<?php
//這段程式碼需要放在一個獨立的 PHP 檔案中
//您可以在 HTML 中引用這個 PHP 檔案,就像引用圖片一樣:
//<IMG SRC="ratingpng.php?rating=25.2" border="0">

function drawRating($rating) {
$image = imagecreate(102,10);
$back = ImageColorAllocate($image,255,255,255);
$border = ImageColorAllocate($image,0,0,0);
$red = ImageColorAllocate($image,255,60,75);
$fill = ImageColorAllocate($image,44,81,150);
ImageFilledRectangle($image,0,0,101,9,$back);
ImageFilledRectangle($image,1,1,$rating,9,$fill);
ImageRectangle($image,0,0,101,9,$border);
imagePNG($image);
imagedestroy($image);
}

Header("Content-type: image/png");
drawRating($rating);

?>
terereese at hotmail dot com
18 年前
我想要告知開發者們我在使用 imagefilledrectangle 時遇到的問題。

我注意到起始和結束 y 座標列出的順序非常重要。
如下面的程式碼所示。
if($this->d_values[$i]['unit_value'] < 0)
imagefilledrectangle($this->img,$position_x, $start_y , $end_x, $end_y ,$d_color);
else
imagefilledrectangle($this->img, $position_x,$ end_y , $end_x, $start_y,$d_colour);


謝謝
me [a.t.] forestfactory [d.o.t.] de
19 年前
在 PHP 5 中,似乎不再需要從左上角到右下角繪製矩形。這讓我將在 PHP 5 下開發的腳本移植到 PHP 4 時遇到了很大的麻煩。
Mice32
14 年前
該腳本繪製的是謝爾賓斯基三角形 (Sierpinski carpet)。

<?php
set_time_limit
(5);

$i = 4; // Iterations
$xy = 500; // Picture size

$img = imagecreatetruecolor($xy, $xy);

$white = imagecolorallocate($img, 255, 255, 255);

drawCarpet(0, 0, $xy, $xy, $i);
function
drawCarpet($a, $b, $c, $d, $n) {
global
$img, $white;

if(
$n <= 0) return;

$a1 = 2 * $a / 3 + $c / 3;
$c1 = $a / 3 + 2 * $c / 3;
$b1 = 2 * $b / 3 + $d / 3;
$d1 = $b / 3 + 2 * $d / 3;

imagefilledrectangle($img, $a1, $b1, $c1, $d1, $white);

drawCarpet($a, $b, $a1, $b1, $n - 1);
drawCarpet($a1, $b, $c1, $b1, $n - 1);
drawCarpet($c1, $b, $c, $b1, $n - 1);

drawCarpet($c1, $b, $c, $b1, $n - 1);
drawCarpet($a, $b1, $a1, $d1, $n - 1);
drawCarpet($c1, $b1, $c, $d1, $n - 1);

drawCarpet($a, $d1, $a1, $d, $n - 1);
drawCarpet($a1, $d1, $c1, $d, $n - 1);
drawCarpet($c1, $d1, $c, $d, $n - 1);
}

header('Content-Type: image/png');
imagepng($img);
?>
Tobi
14 年前
看起來點與點之間像是波浪狀,但實際上是直的。

<?php
$maxwert
= 300;
$size = 10;
$img = imagecreatetruecolor($maxwert, $maxwert);
imagecolorallocate($img, 0, 0, 0);

for(
$y=0;$y<$maxwert;$y += $size){
for(
$x=0;$x<$maxwert;$x+=$size){
$r = rand(0,255);
$g = rand(0,255);
$b = rand(0,255);
$color = imagecolorallocate($img, $r, $g, $b);

imagefilledrectangle ($img, $x, $y, $x+$size ,$y+$size, $color);

}
}
header("Content-type: image/png");
imagepng($img);
?>
ivank at 2xtreme dot net
22 年前
如上所述,它需要從左上角到右下角。如果需要,可以使用以下程式碼翻轉它。

// 如果需要,翻轉它們($x3, $y3 是臨時變數)
if($x1 > $x2) { $x3 = $x2; $x2 = $x1; $x1 = $x3; }
if($y1 > $y2) { $y3 = $y2; $y2 = $y1; $y1 = $y3; }
ImageFilledRectangle($im, $x1, $y1, $x2, $y2, $color);
To Top