PHP Conference Japan 2024

imagefilledarc

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

imagefilledarc繪製並填滿部分弧形

說明

imagefilledarc(
    GdImage $image,
    int $center_x,
    int $center_y,
    int $width,
    int $height,
    int $start_angle,
    int $end_angle,
    int $color,
    int $style
): 布林值

在指定的 image 影像中,以指定的座標為中心繪製部分圓弧。

參數

image

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

center_x

中心的 x 座標。

center_y

中心的 y 座標。

width

圓弧的寬度。

height

圓弧的高度。

start_angle

圓弧的起始角度,以度為單位。

end_angle

圓弧的結束角度,以度為單位。 0° 位於三點鐘位置,圓弧順時針繪製。

color

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

style

以下選項的位元或 (OR) 運算結果

  1. IMG_ARC_PIE
  2. IMG_ARC_CHORD
  3. IMG_ARC_NOFILL
  4. IMG_ARC_EDGED
IMG_ARC_PIEIMG_ARC_CHORD 互斥;IMG_ARC_CHORD 僅使用直線連接起始角度和結束角度,而 IMG_ARC_PIE 產生圓角邊緣。IMG_ARC_NOFILL 表示應該繪製圓弧或弦的輪廓,而不是填充。IMG_ARC_EDGEDIMG_ARC_NOFILL 一起使用時,表示起始角度和結束角度應連接到中心 - 這是繪製(而不是填充)「餅圖切片」輪廓的好方法。

返回值

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

更新日誌

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

範例

範例 #1 建立一個看起來像 3D 的餅圖

<?php

// create image
$image = imagecreatetruecolor(100, 100);

// allocate some colors
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);

// make the 3D effect
for ($i = 60; $i > 50; $i--) {
imagefilledarc($image, 50, $i, 100, 50, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 75, 360 , $darkred, IMG_ARC_PIE);
}

imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE);


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

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

Output of example : Creating a 3D looking pie

新增筆記

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

Mike
17 年前
先前的範例效果不佳。這個更好也更快

<?php
$Randomized
= rand(1,20);
for(
$i=0;$i<=$Randomized;$i++){$data[$i]=rand(2,20);};//full array with garbage.
$imgx='600';$imgy='400';//Set Image Size. ImageX,ImageY
$cx = '300';$cy ='150'; //Set Pie Postition. CenterX,CenterY
$sx = '600';$sy='300';$sz ='100';// Set Size-dimensions. SizeX,SizeY,SizeZ

$data_sum = array_sum($data);
//convert to angles.
for($i=0;$i<=$Randomized;$i++){
$angle[$i] = (($data[$i] / $data_sum) * 360);
$angle_sum[$i] = array_sum($angle);
};
$im = imagecreate ($imgx,$imgy);
$background = imagecolorallocate($im, 255, 255, 255);
//Random colors.
for($i=0;$i<=$Randomized;$i++){
$r=rand(100,255);$g=rand(100,255);$b=rand(100,255);
$colors[$i] = imagecolorallocate($im,$r,$g,$b);
$colord[$i] = imagecolorallocate($im,($r/1.5),($g/1.5),($b/1.5));
}
//3D effect.
for($z=1;$z<=$sz;$z++){
// first slice
imagefilledarc($im,$cx,($cy+$sz)-$z,$sx,$sy,0
,$angle_sum[0],$colord[0],IMG_ARC_EDGED);
for(
$i=1;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,($cy+$sz)-$z,$sx,$sy,$angle_sum[$i-1]
,
$angle_sum[$i],$colord[$i],IMG_ARC_NOFILL);
};
};
//Top pie.
// first slice
imagefilledarc($im,$cx,$cy,$sx,$sy,0 ,$angle_sum[0], $colors[0], IMG_ARC_PIE);
for(
$i=1;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,$cy,$sx,$sy,$angle_sum[$i-1] ,$angle_sum[$i], $colors[$i], IMG_ARC_PIE);
};
//Output.
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
splogamurugan at gmail dot com
13 年前
一個簡單的餅圖生成腳本。
在中心圓弧處顯示百分比,並以隨機顏色顯示圖例。

<?php
class simplepie
{
function
__construct($width, $height, $dataArr)
{
$font = './verdana.ttf'; /** get it from c:/windows/fonts dir */
$this->image = imagecreate($width,$height);
$piewidth = $width * 0.70;/* pie area */
$x = round($piewidth/2);
$y = round($height/2);
$total = array_sum($dataArr);
$angle_start = 0;
$ylegend = 2;
imagefilledrectangle($this->image, 0, 0, $width, $piewidth, imagecolorallocate($this->image, 128, 128, 128));
foreach(
$dataArr as $label=>$value) {
$angle_done = ($value/$total) * 360; /** angle calculated for 360 degrees */
$perc = round(($value/$total) * 100, 1); /** percentage calculated */
$color = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagefilledarc($this->image, $x, $y, $piewidth, $height, $angle_start, $angle_done+= $angle_start, $color, IMG_ARC_PIE);
$xtext = $x + (cos(deg2rad(($angle_start+$angle_done)/2))*($piewidth/4));
$ytext = $y + (sin(deg2rad(($angle_start+$angle_done)/2))*($height/4));
imagettftext($this->image, 6, 0, $xtext, $ytext, imagecolorallocate($this->image, 0, 0, 0), $font, "$perc %");
imagefilledrectangle($this->image, $piewidth+2, $ylegend, $piewidth+20, $ylegend+=20, $color);
imagettftext($this->image, 8, 0, $piewidth+22, $ylegend, imagecolorallocate($this->image, 0, 0, 0), $font, $label);
$ylegend += 4;
$angle_start = $angle_done;
}
}
function
render()
{
header('Content-type: image/png');
imagepng($this->image);
}
}
/** usage */
$dataArr = array(2001=>10, 2002=>30, 2003=>50, 2004=>10);
$width=600;
$height=480;
$pie = new simplepie($width, $height, $dataArr);
$pie->render();
?>
vrp76 at mail dot ru
8 年前
此函數的類似物

<?php

function myimagefilledarc($image, $cx, $cy, $width, $height, $start, $end, $color, $style = IMG_ARC_PIE){

$delta = 0.1;
$twoPi = 2*pi();

$w = $width/2;
$h = $height/2;

if(
$h<=$w){
$kx=$w;
$ky=$w*$h/$w;
}else{
$kx=$h*$w/$h;
$ky=$h;
}

$StartRad = deg2rad($start);
$EndRad = deg2rad($end);

$array_points[] = $cx;
$array_points[] = $cy;

$a = $StartRad;

if(
$style==IMG_ARC_PIE
or $style==IMG_ARC_EDGED
or $style==(IMG_ARC_PIE|IMG_ARC_NOFILL)
or
$style==(IMG_ARC_EDGED|IMG_ARC_NOFILL)){

if(
$StartRad>=$EndRad){

$b[] = $twoPi;
$b[] = $EndRad;

}else
$b[] = $EndRad;

}else
$b[] = 0;

foreach(
$b as $vb){
do {
$array_points[] = $cx + $kx*cos($a);
$array_points[] = $cy + $ky*sin($a);
$a += $delta;
} while (
$a<$vb);
$a = 0;
}

$array_points[] = $cx + $kx*cos($EndRad);
$array_points[] = $cy + $ky*sin($EndRad);

$count_array_points = count($array_points);
$num_points = $count_array_points/2;

if(
$style==IMG_ARC_PIE or $style==IMG_ARC_EDGED or $style==IMG_ARC_CHORD){
imagefilledpolygon($image, $array_points, $num_points, $color);
}elseif(
$style==(IMG_ARC_PIE|IMG_ARC_NOFILL)){

$i = 1;
$c = $count_array_points - 1;

$x1 = $array_points[++$i];
$y1 = $array_points[++$i];

do {
$x2 = $array_points[++$i];
$y2 = $array_points[++$i];
imageline($image, $x1, $y1, $x2, $y2, $color);
$x1 = $x2;
$y1 = $y2;
} while (
$i<$c);

}elseif(
$style==(IMG_ARC_CHORD|IMG_ARC_NOFILL) or $style==(IMG_ARC_PIE|IMG_ARC_NOFILL)){
imageline($image, $array_points[2], $array_points[3], $array_points[4], $array_points[5], $color);
}else{
imagepolygon($image, $array_points, $num_points, $color);
}

}

$image = imagecreatetruecolor(900, 1250);

$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray[] = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$gray[] = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy[] = imagecolorallocate($image, 0x00, 0x00, 0x80);
$navy[] = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red[] = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$red[] = imagecolorallocate($image, 0x90, 0x00, 0x00);
$yellow[] = imagecolorallocate($image, 0xFF, 0xFF, 0x00);
$yellow[] = imagecolorallocate($image, 0x90, 0x90, 0x00);

$Cx = 200;
$Cy = 100;

$W = 300;
$H = 100;

$Dx = 500;
$Dy = 0;
$Dy_3d = 40;

$Angles['yellow'] = array(180,0);
$Angles['gray'] = array(0,88);
$Angles['navy'] = array(88,92);
$Angles['red'] = array(92,180);

$styles['IMG_ARC_PIE'] = IMG_ARC_PIE;

$styles['IMG_ARC_CHORD'] = IMG_ARC_CHORD;

$styles['IMG_ARC_PIE|IMG_ARC_NOFILL'] = IMG_ARC_PIE|IMG_ARC_NOFILL;

$styles['IMG_ARC_CHORD|IMG_ARC_NOFILL'] = IMG_ARC_CHORD|IMG_ARC_NOFILL;

$styles['IMG_ARC_PIE|IMG_ARC_EDGED|IMG_ARC_NOFILL'] = IMG_ARC_PIE|IMG_ARC_EDGED|IMG_ARC_NOFILL;

$styles['IMG_ARC_CHORD|IMG_ARC_EDGED|IMG_ARC_NOFILL'] = IMG_ARC_CHORD|IMG_ARC_EDGED|IMG_ARC_NOFILL;

imagestring($image, 5, 130, 15, 'imagefilledarc', $white);

imagestring($image, 5, 130 + $Dx, 15, 'myimagefilledarc', $white);

foreach(
$styles as $name_style => $style){


for (
$i = $Cy+$Dy_3d; $i > $Cy; $i--) {
foreach(
$Angles as $colors=>$angle){
imagefilledarc($image, $Cx, $i+$Dy, $W, $H, $angle[0], $angle[1],$$colors[1], $style);
}
}

foreach(
$Angles as $colors=>$angle){
imagefilledarc($image, $Cx, $Cy+$Dy, $W, $H, $angle[0], $angle[1],$$colors[0], $style);
}


for (
$i = $Cy+$Dy_3d; $i > $Cy; $i--) {
foreach(
$Angles as $colors=>$angle){
myimagefilledarc($image, $Cx+$Dx, $i+$Dy, $W, $H, $angle[0], $angle[1],$$colors[1], $style);
}
}

foreach(
$Angles as $colors=>$angle){
myimagefilledarc($image, $Cx+$Dx, $Cy+$Dy, $W, $H, $angle[0], $angle[1],$$colors[0], $style);
}

imagestring($image, 5, 450-strlen($name_style)*8/2, $Cy+$Dy+$H-10, $name_style, $yellow[0]);

$Dy+=200;
}


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

?>
caist - www.caist.com
20 年前
如果您想將文字放置在圓的邊緣上,您需要
取得圓上的一個點

$pos_x=$radius*sin(deg2rad($angle));
$pos_y=sqrt($radius*$radius-$pos_x*$pos_x);

如果您希望該點位於餅圖的中心
您需要一個起始角度和結束角度

$pos_x=$radius*sin(deg2rad($angle_end-($angle_start)/2));
$pos_y=sqrt($radius*$radius-$pos_x*$pos_x);

希望它有幫助
xcoda3 at web dot de
9 年前
提供給所有會使用 imageellipse() 繪製帶邊框橢圓的人(imagesetthickness() 無法正常運作)。
這個函式模擬標準 GD 函式庫的邊框效果。

<?php

function imageEllipseWithBorder($image, $centerX, $centerY, $width, $height, $color, $borderWidth)
{
// Calculate inner and outer strength of border
$borderOuterStrength = (($borderWidth - 1) / 2);
$borderInnerStrength = ((($borderWidth - 1) / 2) + 1);

// Caculate x-/y-offset from 0/0 position to ellipse center
$ellipseXOffset = $centerX - ($width / 2) - $borderOuterStrength;
$ellipseYOffset = $centerY - ($height / 2) - $borderOuterStrength;

// Create temp image for editing
$tempImageWidth = $width + ($borderOuterStrength * 2) + 1;
$tempImageHeight = $height + ($borderInnerStrength * 2) + 1;
$tempImage = imagecreatetruecolor($tempImageWidth, $tempImageHeight);
imagealphablending($tempImage, false);

// Fill temp image with "transparent" color
$transparent = imagecolorallocatealpha($tempImage, 255, 255, 255, 127);
imagefill($tempImage, 0, 0, $transparent);

// Draw outer ellipse (representing the border)
imagefilledellipse(
$tempImage,
$centerX - $ellipseXOffset,
$centerY - $ellipseYOffset,
$width + $borderOuterStrength * 2,
$height + $borderOuterStrength * 2,
$color
);

// Draw inner ellipse (transparent area)
imagefilledellipse(
$tempImage,
$centerX - $ellipseXOffset,
$centerY - $ellipseYOffset,
$width - $borderInnerStrength * 2,
$height - $borderInnerStrength * 2,
$transparent
);

// "Paste" ellipse (with transparent inner area) into image at original position
imagealphablending($image, true);
imagecopy(
$image,
$tempImage,
$ellipseXOffset + ($borderWidth + 1) % 2,
$ellipseYOffset + ($borderWidth + 1) % 2,
0,
0,
$tempImageWidth,
$tempImageHeight
);
}

?>
floripondia dot 88 at hotmail dot com
11 年前
如果你想製作一個帶有分離扇形區塊的圓餅圖,並且每個區塊上都顯示數據,而且數據也能正確顯示在右側,你可以使用這段程式碼。
<?php
$values
= array("2010" => 1950, "2011" => 750, "2012" => 2100, "2013" => 580, "2014" => 5000);
$total = count($values);
$data = ($total == 0) ? array(360) : array_values($values);
$keys = ($total == 0) ? array("") : array_keys($values);
$radius = 30;
$imgx = 1800 + $radius;
$imgy = 600 + $radius;
$cx = 400 + $radius;
$cy = 200 + $radius;
$sx = 800;
$sy = 400;
$sz = 150;
$data_sum = array_sum($data);
$angle_sum = array(-1 => 0, 360);
$typo = "./helvetica.ttf";
$im = imagecreate($imgx, $imgy);
imagecolorallocate($im, 255, 255, 255);
$color = array(
array(
220, 20, 60),
array(
77, 33, 114),
array(
249, 141, 53),
array(
158, 37, 59),
array(
1, 128, 128),
array(
28, 94, 160),
//array(206, 16, 118),
array(43, 67, 86),
//array(155, 108, 166),
array(83, 69, 62)
);
shuffle($color);
shuffle($color);
shuffle($color);
$colors = array(imagecolorallocate($im, $color[0][0], $color[0][1], $color[0][2]));
$colord = array(imagecolorallocate($im, ($color[0][0] / 1.5), ($color[0][1] / 1.5), ($color[0][2] / 1.5)));
$factorx = array();
$factory = array();
for(
$i = 0; $i < $total; $i++){
$angle[$i] = (($data[$i] / $data_sum) * 360);
$angle_sum[$i] = array_sum($angle);
$colors[$i] = imagecolorallocate($im, $color[$i][0], $color[$i][1], $color[$i][2]);
$colord[$i] = imagecolorallocate($im, ($color[$i][0] / 1.5), ($color[$i][1] / 1.5), ($color[$i][2] / 1.5));
$factorx[$i] = cos(deg2rad(($angle_sum[$i - 1] + $angle_sum[$i]) / 2));
$factory[$i] = sin(deg2rad(($angle_sum[$i - 1] + $angle_sum[$i]) / 2));
}
for(
$z = 1; $z <= $sz; $z++){
for(
$i = 0; $i < $total; $i++){
imagefilledarc($im, $cx + ($factorx[$i] * $radius), (($cy + $sz) - $z) + ($factory[$i] * $radius), $sx, $sy, $angle_sum[$i - 1], $angle_sum[$i], $colord[$i], IMG_ARC_PIE);
}
}
for(
$i = 0; $i < $total; $i++){
imagefilledarc($im, $cx + ($factorx[$i] * $radius), $cy + ($factory[$i] * $radius), $sx, $sy, $angle_sum[$i - 1], $angle_sum[$i], $colors[$i], IMG_ARC_PIE);
imagefilledrectangle($im, 900, 50 + ($i * 50 * 2), 950, 100 + ($i * 50 * 2), $colors[$i]);
imagettftext($im, 50, 0, 970, 100 + ($i * 50 * 2), imagecolorallocate($im, 0, 0, 0), $typo, $keys[$i]);
imagettftext($im, 40, 0, $cx + ($factorx[$i] * ($sx / 4)) - 40, $cy + ($factory[$i] * ($sy / 4)) + 10, imagecolorallocate($im, 0, 0, 0), $typo, $data[$i]);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Christopher Kramer
15 年前
如果你因為使用舊版 GD 函式庫而無法使用這個函式,以下提供一個用於繪製圓餅圖的解決方案。

<?php

// width and height of the image
$width=200;
$height=200;

$simulate_old_gd=true; // do not use imagefilledarc although available?

// the pieces of the pie (in degree)
$pieces=array(180,90,45,25,15,5);

$diagram=imagecreate($width,$height);

// background color
$white=imagecolorallocate($diagram, 255, 255, 255);
imagefilledrectangle($diagram,0,0,$width,$height,$white);

// the circle is 2px smaller than the image
$width-=2;
$height-=2;

// we need a border color
$black=imagecolorallocate($diagram, 0, 0, 0);

// draw the border of the pie
imagearc($diagram, round($width/2), round($height/2),
$width, $height, 0, 360, $black);

// position (in degrees) where to place the next piece
$position=270;
// we will use calculated gray colors for simple example
$gray=0;

foreach(
$pieces as $deg)
{
// calculate the gray color
$gray+=30;
if(
$gray>255) $gray=0;
$color=imagecolorallocate($diagram,$gray,$gray,$gray);

// position must be kept < 360
if($position>360) $position-=360;

if(!
$simulate_old_gd && is_callable('imagefilledarc'))
{
imagefilledarc($diagram, round($width/2),
round($height/2), $width, $height, $position,
$position+$deg, $color,IMG_ARC_EDGED);
}
else
{
// we use some maths to calculate the pixel on the circle
$pix_x=round(floor(($width-2)/2)*cos($position/180*M_PI)
+
round($width/2));
$pix_y=round(floor(($height-2)/2)*sin($position/180*M_PI)
+
round($height/2));
// now we draw a line from the mid of the circle to the
// calculated pixel on the circle
imageline($diagram, round($width/2), round($height/2),
$pix_x, $pix_y, $black);
// now we need a pixel for flood filling.
//- We could use maths to calculate a pixel inside the
// piece:
//$fill_x=round(floor(($width-10)/2)*
// cos(($position+2)/180*M_PI)+round($width/2));
//$fill_y=round(floor(($height-10)/2)*
// sin(($position+2)/180*M_PI)+round($height/2));
//- or we could use an universal pixel with less maths ;)
// (top mid):
$fill_x=floor($width/2)-2;
$fill_y=3;
// now we flood fill the circle
@imagefilltoborder ($diagram,$fill_x,$fill_y,$black,$color);
/* (it does not matter here that we fill more than we need
because the next pieces will fix this)
IF YOU ONLY WANT ONE PIECE
(simulate imagefilledarc) you'd have to draw
both border lines and flood fill afterwards */
}
// the position of the next piece is $deg degrees further
$position+=$deg;
}

// output the image
header('Content-type: image/png');
imagepng($diagram);
imagedestroy($digram);
?>
mateusz dot charytoniuk at gmail dot com
15 年前
以下程式碼使用了來自 "hans at lintoo dot dk" 的註釋中的顏色。它可以產生帶有標籤的圓餅圖。

<?php
$bright_list
= array(
array(
255, 203, 3),
array(
220, 101, 29),
array(
189, 24, 51),
array(
214, 0, 127),
array(
98, 1, 96),
array(
0, 62, 136),
array(
0, 102, 179),
array(
0, 145, 195),
array(
0, 115, 106),
array(
178, 210, 52),
array(
137, 91, 74),
array(
82, 56, 47)
);
$dark_list = array(
array(
205, 153, 0),
array(
170, 51, 0),
array(
139, 0, 1),
array(
164, 0, 77),
array(
48, 0, 46),
array(
0, 12, 86),
array(
0, 52, 129),
array(
0, 95, 145),
array(
0, 65, 56),
array(
128, 160, 2),
array(
87, 41, 24),
array(
32, 6, 0)
);

$data = array();
$angle = array();
$title = array();
$i = 0;
foreach(
$_GET as $key => $value ) {
$data[$i] = intval($value);
$title[$i++] = str_replace("_"," ",strval($key));
}
$sum = array_sum($data);
if(
$sum == 0 ) {
++
$sum;
}
$count = count($data);
for(
$i = 0; $i < $count; ++ $i ) {
$angle[$i] = floor($data[$i]/$sum*360);
if(
$angle[$i] == 0 ) {
++
$angle[$i];
}
}
$sum_angle = array_sum($angle);
if(
$sum_angle < 360 ) {
$angle[0]+=360-$sum_angle;
}

$height = $count*34;
if(
$height < 180 ) {
$height = 180;
}

$im = imagecreate (350, $height);
$background = imagecolorallocate($im, 226, 226, 226);
$border = imagecolorallocate($im,97,97,97);
$font_color = imagecolorallocate($im,0,0,0);
$font = 'yourfont.ttf';

$bright = array();
foreach(
$bright_list as $c ) {
$bright[] = imagecolorallocate($im,$c[0],$c[1],$c[2]);
}

$dark = array();
foreach(
$dark_list as $c ) {
$dark[] = imagecolorallocate($im,$c[0],$c[1],$c[2]);
}
$tmp = 0;
for(
$i =0; $i < $count; ++ $i ) {
for(
$j = 100; $j > 90; -- $j ) {
imagefilledarc($im, 100, $j, 180, 120, $tmp, $tmp+$angle[$i], $dark[$i], IMG_ARC_PIE);
}
$tmp += $angle[$i];
}

$tmp = 0;
for(
$i =0; $i < $count; ++ $i ) {
imagefilledarc($im, 100, 90, 180, 120, $tmp, $tmp+$angle[$i], $bright[$i], IMG_ARC_PIE);
$tmp += $angle[$i];
}
for(
$i = 0; $i < $count; ++ $i ) {
imagefilledrectangle($im, 209, 19+($i*30), 231, 41+($i*30), $border);
imagefilledrectangle($im, 210, 20+($i*30), 230, 40+($i*30), $bright[$i]);
imagefttext($im, 11, 0, 240, 34+($i*30), $font_color, $font, $title[$i]);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

試試 'pie.php?foo=3&bar=4&baz=6'
imazir at gmail dot com
18 年前
先前的範例無法運作。嘗試以下修改,你將會得到預期的結果。

<?
$Randomized = rand(1,20);
for($i=0;$i<=$Randomized;$i++){$data[$i]=rand(2,20);};//用垃圾數據填滿陣列。
$imgx='200';$imgy='200';//設定圖片大小。ImageX,ImageY
$cx = '100';$cy ='50'; //設定圓餅圖位置。CenterX,CenterY
$sx = '200';$sy='100';$sz ='20';// 設定尺寸。SizeX,SizeY,SizeZ

$data_sum = array_sum($data);
//轉換為角度。
for($i=0;$i<=$Randomized;$i++){
$angle[$i] = (($data[$i] / $data_sum) * 360);
$angle_sum[$i] = array_sum($angle);
};
$im = imagecreate ($imgx,$imgy);
$background = imagecolorallocate($im, 255, 255, 255);
//隨機顏色。
for($i=0;$i<=$Randomized;$i++){
$r=rand(100,255);$g=rand(100,255);$b=rand(100,255);
$colors[$i] = imagecolorallocate($im,$r,$g,$b);
$colord[$i] = imagecolorallocate($im,($r/2),($g/2),($b/2));
}
//3D 效果。
for($z=1;$z<=$sz;$z++){
for($i=1;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,($cy+$sz)-$z,$sx,$sy,$angle_sum[$i-1]
,$angle_sum[$i],$colord[$i],IMG_ARC_PIE);
};
};
//頂部圓餅。
for($i=1;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,$cy,$sx,$sy,$angle_sum[$i-1] ,$angle_sum[$i], $colors[$i], IMG_ARC_PIE);
};
//輸出。
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
paulcharltonthomson at hotmail dot com
19 年前
這裡有一個稍微更好的方法來取得圓餅圖陰影牆的顏色,由 double-zonk at wp dot pl 發表。

<?php

$rgb0
= array (255, 153, 204);
$rgb1 = array (255, 153, 0);
$rgb2 = array (153, 204, 0);
$rgb3 = array (51, 153, 102);
$rgb4 = array (51, 204, 204);
$rgb5 = array (51, 102, 255);
$rgb6 = array (128, 0, 128);
$rgb7 = array (150, 150, 150);

for (
$r = 0; $r < 8; ++$r)
{
if(${
"rgb" . $r}[0] < 50) $shadowr = 0; else $shadowr = ${"rgb" . $r}[0] - 50;
if(${
"rgb" . $r}[1] < 50) $shadowg = 0; else $shadowg = ${"rgb" . $r}[1] - 50;
if(${
"rgb" . $r}[2] < 50) $shadowb = 0; else $shadowb = ${"rgb" . $r}[2] - 50;
${
"wall" . $r} = array ($shadowr, $shadowg, $shadowb);
}

for (
$s = 0; $s < 8; ++$s)
{
$kolor[$s] = imagecolorallocate($image, ${"rgb" . $s}[0], ${"rgb" . $s}[1], ${"rgb" . $s}[2]);
$cien[$s] = imagecolorallocate($image, ${"wall" . $s}[0], ${"wall" . $s}[1], ${"wall" . $s}[2]);
}

?>
rich at dicksonlife dot com
20 年前
更有效率的做法

原始程式碼片段和以下建議效率低下,因為它們依賴上層的 PHP 使用迴圈來垂直填充,而不是利用底層的繪圖程式。此外,這是透過重複繪製填充的部分橢圓來完成的,而圓形計算通常很耗資源(PHP 可能使用表格,我不確定)。原始程式碼可以改寫為:

<?php
// Add the bottom layer.
imagefilledarc($image, 50, 60, 100, 50, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, 60, 100, 50, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, 60, 100, 50, 75, 360 , $darkred, IMG_ARC_PIE);

//Now do the joining pieces.
//Note: Precompute cosines and sines for efficiency
$c1=50*cos(45/180*M_PI);
$s1=25*sin(45/180*M_PI);
$c2=50*cos(75/180*M_PI);
$s2=25*sin(75/180*M_PI);

$area1=array(100,60,100,50,50+$c1,50+$s1,50+$c1,60+$s1);
$area2=array(50+$c1,50+$s1,50+$c1,60+$s1,50+$c2,60+$s2,50+$c2,50+$s2);
//Note that piece 3 goes round the corner. So we are only interested in the leftmost extent. You would need to do this programatically. Also, you do not need to make vertical parts for any segments completely at the back of the pie (in fact, not filledarcs either)
$area3=array(50+$c2,50+$s2,50+$c2,60+$s2,0,60,0,50);

imagefilledpolygon($image, $area1 , 4 , $darknavy);
imagefilledpolygon($image, $area2 , 4 , $darkgray);
imagefilledpolygon($image, $area3 , 4 , $darkred);

imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE);
?>

請注意,多邊形可能效率略低。如果有 imagefilledtriangle 函式,這個程式碼會更簡單。考慮到三角形是多麼基礎的圖形,或許可以在未來的版本中加入?

Rich
hans at lintoo dot dk
20 年前
我發現我的程式碼有一些錯誤.. 因此我發布了修正。
錯誤
<?php
$drakcolor
[2] = imagecolorallocate($im, 139, 0, 1);
//應該是
$darkcolor[2] = imagecolorallocate($im, 139, 0, 1);
?>

然後,如果您稍微修改程式碼,然後製作一個 200x125 的圖像,就不會浪費空間。
變更
<?php
$im
= imagecreate (200, 125);
// 以及
for ($i = 60; $i > 50; $i--) {
imagefilledarc($im, 100, $i, 200, 100, $anglesum[$f], $anglesum[$n], $darkcolor[$f], IMG_ARC_PIE);
}
// 以及
imagefilledarc($im, 100, 50, 200, 100, $anglesum[$n], $anglesum[$i], $randcolor[$n], IMG_ARC_PIE);
?>
您可以在線上查看範例:http://webstatistik.lintoo.dk/
hans at lintoo dot dk
20 年前
我修改了程式碼,以便從其他地方收集的數據製作 3D 餅圖... 在這種情況下,它是用於統計頁面...

測試時,您可以使用

享受

<?php
//Making a image 200 x 200
$im = imagecreate (200, 200);

//Setting background color
$background = imagecolorallocate($im, 226, 226, 226);

//Setting colors of elements
$randcolor[0] = imagecolorallocate($im, 255, 203, 3);
$randcolor[1] = imagecolorallocate($im, 220, 101, 29);
$randcolor[2] = imagecolorallocate($im, 189, 24, 51);
$randcolor[3] = imagecolorallocate($im, 214, 0, 127);
$randcolor[4] = imagecolorallocate($im, 98, 1, 96);
$randcolor[5] = imagecolorallocate($im, 0, 62, 136);
$randcolor[6] = imagecolorallocate($im, 0, 102, 179);
$randcolor[7] = imagecolorallocate($im, 0, 145, 195);
$randcolor[8] = imagecolorallocate($im, 0, 115, 106);
$randcolor[9] = imagecolorallocate($im, 178, 210, 52);
$randcolor[10] = imagecolorallocate($im, 137, 91, 74);
$randcolor[11] = imagecolorallocate($im, 82, 56, 47);

//Setting the darker alt color to the main color
$darkcolor[0] = imagecolorallocate($im, 205, 153, 0);
$darkcolor[1] = imagecolorallocate($im, 170, 51, 0);
$drakcolor[2] = imagecolorallocate($im, 139, 0, 1);
$darkcolor[3] = imagecolorallocate($im, 164, 0, 77);
$darkcolor[4] = imagecolorallocate($im, 48, 0, 46);
$darkcolor[5] = imagecolorallocate($im, 0, 12, 86);
$darkcolor[6] = imagecolorallocate($im, 0, 52, 129);
$darkcolor[7] = imagecolorallocate($im, 0, 95, 145);
$darkcolor[8] = imagecolorallocate($im, 0, 65, 56);
$darkcolor[9] = imagecolorallocate($im, 128, 160, 2);
$darkcolor[10] = imagecolorallocate($im, 87, 41, 24);
$darkcolor[11] = imagecolorallocate($im, 32, 6, 0);

//Getting the data from GET
$i = 0;
while (
$i <= 11) {
$data[$i] = $_GET[++$i];
}

//Getting ready
$datasum = array_sum($data);
$anglesum[0] = 0;
$angle[0] = 0;
$i = 0;

//Calc the start and end angle position of the elements
while ($i <= 11) {
++
$i;
$n = $i - 1;
$part[$i] = $data[$n] / $datasum;
$angle[$i] = floor($part[$i] * 360);
$anglesum[$i] = array_sum($angle);
}

/*
//DEBUGGING - only for testing purposes
echo "<pre>";
print_r($part);
print_r($anglesum);
print_r($angle);
*/

// make the 3D effect
$n = 0;$i=0;
while (
$n <= 11) {
++
$n;
$f = $n - 1;
if (
$angle[$n] != 0) {
for (
$i = 110; $i > 100; $i--) {
imagefilledarc($im, 100, $i, 200, 100, $anglesum[$f], $anglesum[$n], $darkcolor[$f], IMG_ARC_PIE);
}
}
}

//make the 2d data that sits above the 3deffect
$i = 0;
while (
$i <= 11) {
++
$i;
$n = $i - 1;
if (
$angle[$i] != 0) {
imagefilledarc($im, 100, 100, 200, 100, $anglesum[$n], $anglesum[$i], $randcolor[$n], IMG_ARC_PIE);
}
}

// flush image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
micha _a.t_ psytrance _d.o.t_ info
20 年前
為了獲得漂亮的顏色和根據數值調整的陰影,我嘗試

<?php
function _errechne_gradzahlen( $werte ) { /* calc degrees */
foreach( $werte as $wert ) { $sum += $wert; }
foreach(
$werte as $wert ) { $gradzahlen[] = 360 * ( $wert / $sum ); }
return
$gradzahlen;
}

function
_randomcol ( $im ) {
return
imagecolorallocate( $im, rand(100, 224), rand(100, 224), rand(128, 224) );
}

$values = array( 100, 200, 50, 100, 43, 32 ); /* the data to display ( real values ) */
$werte = _errechne_gradzahlen( $values ); /* degrees-array */
$width = 200;
$height = 200;
$half_width = floor( $width / 2 );
$half_height = floor($height / 2);

$im = ImageCreateTrueColor( $width, $height );

foreach(
$werte as $key => $wert ) {
/* get colors and shadows */
$color = _randomcol( $im );
$shadow = $color - 20000; // or brighter shadows take 10000
$colors[] = $color;
$shadows[] = $shadow;
/* 3D effekt */
for ($i = ($half_height + 10); $i > $half_height; $i--) {
imagefilledarc(
$im,
$half_width, $i,
$width, $half_height,
$offset, ($offset + $wert), // from, to (degrees)
$shadows[$key], IMG_ARC_NOFILL);
}
$offset = $offset + $wert;
}
$offset = 0;

foreach(
$werte as $key => $wert ) { /* an now draw the top */
imagefilledarc(
$im,
$half_width, $half_width,
$width, $half_height, // half sized
$offset, ($offset + $wert),
$colors[$key], IMG_ARC_PIE);
$offset = $offset + $wert;
}
header( "Content-type: image/png" );
imagepng ( $im );
imagedestroy( $im );
?>

抱歉我的破英文和雜亂的程式碼,我是從我之前寫的一個類別中剪切並「翻譯」過來的。
poopie
20 年前
目前提供的繪製 3D 外觀餅圖的範例效率極低,並且會對繪製大量餅圖的腳本造成巨大的效能影響,尤其是那些在磁碟上進行離線處理而不是將單個餅圖發送到瀏覽器的腳本(無論哪種方式,此修改都可以節省大量 CPU 週期)。

修改程式碼中建立 3D 效果的部分,使其僅繪製頂部填充餅圖下方餅圖層的輪廓(使用 IMG_ARC_NOFILL)。

// 製作 3D 效果(針對原始範例修改)
for ($i = 60; $i >= 50; $i--) {
imagefilledarc($image, 50, $i, 100, 50, 0, 45, $darknavy, IMG_ARC_NOFILL);
imagefilledarc($image, 50, $i, 100, 50, 45, 75 , $darkgray, IMG_ARC_NOFILL);
imagefilledarc($image, 50, $i, 100, 50, 75, 360 , $darkred, IMG_ARC_NOFILL);
}

請注意 for 迴圈中的 >=,它填補了沒有 = 時產生的間隙。
t_therkelsen at hotmail dot com
19 年前
請注意,imageFilledArc() 和 imageArc() 都使用整數作為角度測量值。如果您*只*使用 imageArc() 和/或 imageFilledArc(),則沒有問題。但是,如果您使用計算出的角度並計劃疊加其他繪圖元素(例如,您想在陰影 3D 效果之間製作垂直線),則需要在將角度轉換為弧度之前使用 floor() 函數,否則會出現精度誤差。

一個說明此「特性」的小範例...

<?php
$img
= imageCreate(400, 400);
$back = imageColorAllocate($img, 0, 0, 0);
$front = imageColorAllocate($img, 255, 255, 255);

$sd = 45.5;
$ed = 130.5;

imageFilledArc($img, 200, 200, 300, 300, $sd, $ed,
$front, IMG_ARC_PIE|IMG_ARC_NOFILL|IMG_ARC_EDGED);
imageArc($img, 200, 230, 300, 300, $sd, $ed, $front);

imageLine($img,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+200,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+230,
$front);
imageLine($img,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+200,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+230,
$front);

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

這才是它應有的樣子...

<?php
$img
= imageCreate(400, 400);
$back = imageColorAllocate($img, 0, 0, 0);
$front = imageColorAllocate($img, 255, 255, 255);

$sd = floor(45.5);
$ed = floor(130.5);

imageFilledArc($img, 200, 200, 300, 300, $sd, $ed,
$front, IMG_ARC_PIE|IMG_ARC_NOFILL|IMG_ARC_EDGED);
imageArc($img, 200, 230, 300, 300, $sd, $ed, $front);

imageLine($img,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+200,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+230,
$front);
imageLine($img,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+200,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+230,
$front);

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