好的...
可能是繪製實心圓的最簡單方法
循環執行 imagearc 函式,將直徑遞增一個像素
<?
// --- 程式碼片段 --- //
for($i=1; $i<$Diameter; $i++){
imagearc($Image, $CenterX, $CenterY, $i, $i, $Start, $End, $Color);
}
// --------------------- //
?>
這對於直徑最大約 60 或 70 像素的圓形非常有效。超過這個大小後,就會開始出現像素間隙。
(PHP 4, PHP 5, PHP 7, PHP 8)
imagearc — 繪製弧形
$image
,$center_x
,$center_y
,$width
,$height
,$start_angle
,$end_angle
,$color
imagearc() 函式會繪製以指定座標為中心的圓弧。
image
一個 GdImage 物件,由影像建立函式之一返回,例如 imagecreatetruecolor()。
center_x
中心的 x 座標。
center_y
中心的 y 座標。
width
圓弧的寬度。
height
圓弧的高度。
start_angle
圓弧的起始角度,以度數表示。
end_angle
圓弧的結束角度,以度數表示。0° 位於三點鐘位置,圓弧順時針繪製。
color
使用 imagecolorallocate() 建立的顏色識別碼。
範例 #1 使用 imagearc() 繪製圓形
<?php
// create a 200*200 image
$img = imagecreatetruecolor(200, 200);
// allocate some colors
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
// draw the head
imagearc($img, 100, 100, 200, 200, 0, 360, $white);
// mouth
imagearc($img, 100, 100, 150, 150, 25, 155, $red);
// left and then the right eye
imagearc($img, 60, 75, 50, 50, 0, 360, $green);
imagearc($img, 140, 75, 50, 50, 0, 360, $blue);
// output image in the browser
header("Content-type: image/png");
imagepng($img);
// free memory
imagedestroy($img);
?>
上述範例將輸出類似以下的內容
好的...
可能是繪製實心圓的最簡單方法
循環執行 imagearc 函式,將直徑遞增一個像素
<?
// --- 程式碼片段 --- //
for($i=1; $i<$Diameter; $i++){
imagearc($Image, $CenterX, $CenterY, $i, $i, $Start, $End, $Color);
}
// --------------------- //
?>
這對於直徑最大約 60 或 70 像素的圓形非常有效。超過這個大小後,就會開始出現像素間隙。
這是我為自己編寫的一個範例腳本,用來幫助我學習如何使用 imagearc 函式。也許它也能幫助其他人。
<?php
//example PHP script of imagearc functions
$image_width=360;$image_height=360;
$img = imagecreatetruecolor($image_width,$image_height); //make image variable
//create a background color by making a filled rectangle
$color = imagecolorallocate($img,255,255,255);
imagefilledrectangle($img,0,0,$image_width,$image_height,$color);
$r=$image_width/2 - $image_width/32 ; //radius
$cx=$image_width/2;
$cy=$image_height/2;
$color = imagecolorallocate($img,0,0,0);
imagearc($img, $cx, $cy, $r*2, $r*2, 0, 360, $color); //regular outlines arc
imagefilledarc($img, $cx, $cy, $r*1, $r*1, 0, 90, $color,IMG_ARC_CHORD); //filled triangle with chord of circle
imagefilledarc($img, $cx, $cy, $r*1, $r*1, 180, 270, $color,IMG_ARC_PIE); //pie slice
$font_number=5; //can use built in fonts numbered 1 to 5
$string="Hello world!";
imagestring($img, $font_number, $cx-(imagefontwidth($font_number)*strlen($string))/2, $cy-120, $string, $color);
header("Content-type: image/png");
imagepng($img);// output image in the browser
$filename="imagearc";
imagepng($img,"./frames/$filename.png",9); //make highly compressed png
imagedestroy($img);
?>
imagesetstyle() 設定所有線條繪製函式在使用特殊顏色繪製時所使用的樣式。
以下是一個繪製虛線圓的範例。好好享受吧!
<?php
header("Content-type: image/jpeg");
$im = imagecreate(100,100);
$b = imagecolorallocate ($im, 0, 0, 0);
$w = imagecolorallocate ($im, 255, 255, 255);
$style = array ($b,$b,$b,$b,$b,$w,$w,$w,$w,$w);
imagesetstyle ($im, $style);
imagearc($im,50,50,100,100,0,360,IMG_COLOR_STYLED);
imagejpeg($im);
imagedestroy($im);
?>
imagearc 範例運作不正常,因為缺少了這一行
"$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img,0,0,$white);
$black = imagecolorallocate($img, 0, 0, 0);
"
動態大小的圓角反鋸齒按鈕。
$w=40;
$h=20;
$im = ImageCreate($w,$h);
$white=ImageColorAllocate($im,255,255,255);
ImageFilledRectangle($im,0,0,$w,$h,$white);
imagecolortransparent ($im, $white);
ImageTTFText ($im, $h+ceil($h/3)+1, 0, -1, $h-1, $col1, "arialbd.ttf", "O");
ImageTTFText ($im, $h+ceil($h/3)+1, 0, $w-$h, $h-1, $col1, "arialbd.ttf", "O");
ImageTTFText ($im, $h+ceil($h/3)+1, 0, 1, $h-1, $col1, "arialbd.ttf", "O");
ImageTTFText ($im, $h+ceil($h/3)+1, 0, $w-$h-2, $h-1, $col1, "arialbd.ttf", "O");
$points=array(
1,round($h/2),
round($h/4),$h-round($h/4),
round($h/2),$h,
$w-(round($h/2)),$h,
$w-(round($h/4)),$h-round($h/4),
$w-2,round($h/2),
$w-round($h/4),round($h/4),
$w-round($h/2),0,
round($h/2),0,
round($h/4),round($h/4)
);
imagefilledpolygon ($im, $points, 10, $col1);
header("content-type: image/gif");
header("Content-Disposition: filename=name.gif");
ImageGif($im);
ImageDestroy($im);
[備註-Apache/1.3.29 (Win32) PHP/4.3.4]
imagearc(以及 imageellipse)函式在繪製 0 到 360 度的圖形時不接受線段粗細設定。
從 0 度繪製到 359 度,然後再從 359 度繪製到 360 度,則會以目前的線段粗細設定建立一個橢圓。
Jerry
繪製橢圓弧形填色 (DiameterX != DiameterY)
<?
function imagefilledarc($Image, $CenterX, $CenterY, $DiameterX, $DiameterY, $Start, $End, $Color) {
// 繪製弧形
imagearc($Image, $CenterX, $CenterY, $DiameterX, $DiameterY, $Start, $End, $Color);
// 使用兩條連接中心點和弧形兩端的線段封閉弧形
$x = $CenterX + (cos(deg2rad($Start))*($DiameterX/2));
$y = $CenterY + (sin(deg2rad($Start))*($DiameterY/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Color);
$x = $CenterX + (cos(deg2rad($End))*($DiameterX/2));
$y = $CenterY + (sin(deg2rad($End))*($DiameterY/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Color);
// 填滿弧形,起始點是封閉空間中間的一個點
$x = $CenterX + (cos(deg2rad(($Start+$End)/2))*($DiameterX/4));
$y = $CenterY + (sin(deg2rad(($Start+$End)/2))*($DiameterY/4));
imagefilltoborder($Image, $x, $y, $Color, $Color);
}
?>
使用兩條線段封閉弧形 (DiameterX != DiameterY)
<?
function imagenofilledarc($Image, $CenterX, $CenterY, $DiameterX, $DiameterY, $Start, $End, $Color) {
// 繪製弧形
imagearc($Image, $CenterX, $CenterY, $DiameterX, $DiameterY, $Start, $End, $Color);
// 使用兩條連接中心點和弧形兩端的線段封閉弧形
$x = $CenterX + (cos(deg2rad($Start))*($DiameterX/2));
$y = $CenterY + (sin(deg2rad($Start))*($DiameterY/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Color);
$x = $CenterX + (cos(deg2rad($End))*($DiameterX/2));
$y = $CenterY + (sin(deg2rad($End))*($DiameterY/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Color);
}
?>
範例
<?
$destImage = imagecreate( 216, 152 );
$c0 = imagecolorallocate( $destImage, 0, 255, 255 );
$c1 = imagecolorallocate( $destImage, 0, 0, 0 );
$c2 = imagecolorallocate( $destImage, 255, 0, 0 );
ImageFilledRectangle ( $destImage, 0, 0, 216, 152, $c0 );
imagefilledarc( $destImage, 108, 76, 180, 80, 0, 130, $c1 );
imagenofilledarc( $destImage, 108, 76, 180, 80, 0, 130, $c2 );
header("content-type: image/PNG");
ImagePNG( $destImage );
ImageDestroy( $destImage );
?>
奇怪的是,前兩個整數表示放置「圓形」的位置。
例如,我首先建立放置圓形的「畫布」。
$image = imagecreate(500, 500);
(這會建立一個很大的 500x500 gif :) )
$colorBody = imagecolorallocate($image, 0, 0, 0);
(將「畫布」的預設顏色設為黑色
$circleColor = imagecolorallocate($image, 255, 0, 255);
(將圓形的顏色設為難看的粉紅色)
imagearc($image, 250, 250, 300, 300, 0, 360, $circleColor);
將圖像放置在中心 (250,250),圓形的直徑為 300 像素。
希望這個說明有幫助。
Travis Kent Beste
我對其他兩個函數不太成功,其中一個函數繪製的圓形看起來像是用點陣式印表機印出來的。這個簡單的函數用圓形構建邊框,看起來效果不錯。
<?php
函式 imagearcunfilled($image,$x,$y,$width,$height,$border_thickness, $color) {
imagesetthickness($image, 1);
$x_radius = $width / 2;
$y_radius = $height / 2;
for ($i = 0; $i < 360; $i++) {
if (TRUE) {
$x2 = $x + cos($i) * $x_radius;
$y2 = $y + sin($i) * $y_radius;
imagefilledarc($image,$x2,$y2,$border_thickness,$border_thickness,0,360,$color,IMG_ARC_PIE);
}
}
}
?>
(nojer2 at yahoo dot com, 2001年4月2日 12:06) 提出的關於旋轉(填滿)橢圓的先前注意事項有錯誤,在第二個弧線處。請使用以下清單替換它們。
if ($filled) {
triangle($im, $cx, $cy, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
triangle($im, $cx, $cy, $cx-$px, $cy-$py, $cx-$x, $cy-$y, $colour);
} else {
imageline($im, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
imageline($im, $cx-$px, $cy-$py, $cx-$x, $cy-$y, $colour);
}
請注意,為了繪製完整的圓形或橢圓形(不使用 imageellipse),您不能將 s 和 e 都使用 0 度。如果您這樣做,您將得到,嗯,什麼也沒有。請將 s 設定為 0 度,e 設定為 360 度以獲得完整的圓形或橢圓形。
這是再次繪製旋轉橢圓的函式。這次我對它進行了一些優化,修復了未填滿的錯誤,並使用了「壓縮比」而不是「半徑修改器」來使曲線完美,所以忽略我之前的版本。
<?php
function rotatedellipse($im, $cx, $cy, $width, $height, $rotateangle, $colour, $filled=true) {
$step=2;
$cosangle=cos(deg2rad($rotateangle));
$sinangle=sin(deg2rad($rotateangle));
$squishratio = $height/$width;
$nopreviouspoint = true;
for ($angle=0; $angle<=(180+$step); $angle+=$step) {
$ox = ($width * cos(deg2rad($angle)));
$oy = ($width * sin(deg2rad($angle))) * $squishratio;
$x = + (($ox * $cosangle) - ($oy * $sinangle));
$y = $centrey + (($ox * $sinangle) + ($oy * $cosangle));
if ($nopreviouspoint) {
$px=$x;
$py=$y;
$nopreviouspoint=false;
}
if ($filled) {
triangle($im, $cx, $cy, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
triangle($im, $cx, $cy, $cx-$px, $cx-$py, $cx-$x, $cy-$y, $colour);
} else {
imageline($im, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
imageline($im, $cx-$px, $cx-$py, $cx-$x, $cy-$y, $colour);
}
$px=$x;
$py=$y;
}
}
function triangle($im, $x1,$y1, $x2,$y2, $x3,$y3, $colour) {
$coords = array($x1,$y1, $x2,$y2, $x3,$y3);
imagefilledpolygon($im, $coords, 3, $colour);
}
?>
要繪製填滿的弧形,請嘗試以下方法
<?php
$diameter = 50;
imagearc($image, 25, 25, $diameter, $diameter, $start, $end, $color);
while($diameter > 0) {
imagearc($image, 25, 25, $diameter, $diameter, $start, $start + 1, $color);
imagearc($image, 25, 25, $diameter, $diameter, $end - 1, $end, $color);
$diameter--;
}
?>
... 你懂的。這是一個權宜之計,而且*非常*慢,但它是免費的。
這是繪製虛線圓的函式
<?php
function dashedcircle($im, $cx, $cy, $radius, $colour, $dashsize=5) {
$dash=false;
for ($angle=0; $angle<=(180+$dashsize); $angle+=$dashsize) {
$x = ($radius * cos(deg2rad($angle)));
$y = ($radius * sin(deg2rad($angle)));
if ($dash) {
imageline($im, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
imageline($im, $cx-$px, $cx-$py, $cx-$x, $cy-$y, $colour);
}
$dash=!$dash;
$px=$x;
$py=$y;
}
}
?>
我需要一個帶有粗邊框的弧形,我不喜歡使用 359.9 作為結束角度,所以我寫了一個運作良好的函式。
<?php
function imagearcthick($image, $x, $y, $w, $h, $s, $e, $color, $thick = 1)
{
if($thick == 1)
{
return imagearc($image, $x, $y, $w, $h, $s, $e, $color);
}
for($i = 1;$i<($thick+1);$i++)
{
imagearc($image, $x, $y, $w-($i/5), $h-($i/5),$s,$e,$color);
imagearc($image, $x, $y, $w+($i/5), $h+($i/5), $s, $e, $color);
}
}
?>
imagearc 函式的精度為一度。該函式會將 $start 和 $end 值截斷至較小的度數。
例如,如果您計算出的起始角度為:-178.62450462172°
而結束角度為:-152.78056427917°
imagearc 會繪製一條從 -178° 到 -152° 的曲線。
如果您需要繪製精確的曲線,則需要使用迴圈逐步繪製小線段。透過建立大量足夠短的線段,您將營造出精確曲線的視覺效果。
我寫了一個可以逆時針繪製弧線的簡單函式。程式碼如下:
<?php
函式 imagearcCC(&$im, $cx, $cy, $w, $h, $s, $e, $c) {
$start = 360 - $e;
$end = 360 - $s;
返回 imagearc($im, $cx, $cy, $w, $h, $start, $end, $c);
}
?>
這個函式的參數與一般的 imagearc 函式完全相同。
這裡有一個函式可以在兩點之間畫一條曲線... 這會是一條向下彎曲的曲線,但要製作一個類似的函式來畫一條向上彎曲的曲線並不難。第一個點必須在第二個點的左邊($x1 < $x2),而高度實際上是相反的。高度越大,曲線的波峰就越小。我想稍加修改,這個函式也可以畫出向上彎曲的曲線。
函式 ImageCurveDown ($image, $x1, $y1, $x2, $y2, $height, $color) {
$presicion = 1;
for ($left = ($x1-$x2); $left < 0; $left++){
if ($y1 < $y2) {
$cy = $y2 + $height;
$cx = $x1 - $left;
} else {
$cy = $y1 + $height;
$cx = $x2 + $left;
}
$nx1 = abs($x1 - $cx);
$ny1 = abs($y1 - $cy);
$nx2 = abs($x2 - $cx);
$ny2 = abs($y2 - $cy);
if ($y1 < $y2) {
if ($nx2 == 0 || $ny1 == 0) continue;
$angle1 = atan($height/$nx2);
$A1 = $nx2/cos ($angle1);
$B1 = $ny2/sin ($angle1);
$angle2 = pi()/2 +atan($left/$ny1);
$A2 = $nx1/cos ($angle2);
$B2 = $ny1/sin ($angle2);
} else {
if ($ny2 == 0 || $nx1 == 0) continue;
$angle1 = atan($ny2/$nx2);
$A1 = abs($nx2/cos ($angle1));
$B1 = abs($ny2/sin ($angle1));
$angle2 = atan($height/$nx1);
$A2 = abs ($nx1/cos ($angle2));
$B2 = abs($ny1/sin ($angle2));
}
if (abs($A1 - $A2) < $presicion && abs ($B1 - $B2) < $presicion) {
ImageArc($image, $cx, $cy, $A1*2, $B1*2, 180+rad2deg($angle2), 360-rad2deg($angle1), $color);
}
}
}
我找到一個更好的方法來繪製圓餅圖
header ("Content-type: image/png");
$diameter = 100;
$radius = $diameter / 2;
$centerX = $radius;
$centerY = $radius;
$im = @ImageCreate ($diameter, $diameter)
or die ("無法初始化新的 GD 影像串流");
$background = ImageColorAllocate ($im, 0, 0, 0);
$red = ImageColorAllocate ($im, 176, 0, 0);
函式 fill_arc($start, $end, $color) {
global $diameter, $centerX, $centerY, $im, $radius;
imagearc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color);
imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start)) * $radius, $centerY + sin(deg2rad($start)) * $radius, $color);
imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end)) * $radius, $centerY + sin(deg2rad($end)) * $radius, $color);
imagefill ($im,$centerX + $radius * 0.5 *cos(deg2rad($start+($end-$start)/2)), $centerY + $radius * 0.5 * sin(deg2rad($start+($end-$start)/2)), $color);
}
fill_arc(0,30,$red);
// 會繪製一個從 0 度開始到 30 度結束的紅色填充弧形
ImagePng ($im); // 輸出 PNG 圖檔
還有另一種填充弧形的方法
<?php
// 繪製弧形
$Color = imagecolorallocate($Image, $Red, $Green, $Blue);
imagearc($Image, $CenterX, $CenterY, $Diameter, $Diameter, $Start, $End, $Color);
// 使用兩條連接圓心和弧形兩端的線段來閉合弧形
$x = $CenterX + (cos(deg2rad($Start))*($Diameter/2));
$y = $CenterY + (sin(deg2rad($Start))*($Diameter/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Color);
$x = $CenterX + (cos(deg2rad($End))*($Diameter/2));
$y = $CenterY + (sin(deg2rad($End))*($Diameter/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Color);
// 填充弧形,起始點是閉合空間中間的一個點
$x = $CenterX + (cos(deg2rad(($Start+$End)/2))*($Diameter/4));
$y = $CenterY + (sin(deg2rad(($Start+$End)/2))*($Diameter/4));
imagefilltoborder($Image, $x, $y, $Color, $Color);
?>