php imagearc,imagearc - [ php中文手册 ] - 在线原生手册 - php中文网

用户评论:

[#1]

lucas dot delmas at live dot fr [2013-07-07 19:15:23]

The imagearc function has a precision of one degree. The function truncates the $start and $end values to the inferior degree.

For example if the starting angle you calculated is : -178.62450462172??

and the ending angle is : -152.78056427917??

imagearc will draw a curve from -178?? to -152??.

If you need accurate curves drawing, you need to use a loop to draw little step-by-step lines. By creating a large number of short enough lines, you will create the impression of a curve with accuracy.

[#2]

joe dot tym at gmail dot com [2012-10-02 17:08:33]

I didn't have much luck with the other two functions, one of them makes circles that look like they've been printed on a dot-matrix printer. This simple function builds a border out of circles, seems to work nicely.

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);

}

}

}?>

[#3]

chandlerklebs at gmail dot com [2012-01-07 22:24:42]

This is an example script I wrote for myself to help me learn how to used the imagearc functions. Maybe if will also help others.

//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 arcimagefilledarc($img,$cx,$cy,$r*1,$r*1,0,90,$color,IMG_ARC_CHORD);//filled triangle with chord of circleimagefilledarc($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 pngimagedestroy($img);?>

[#4]

Michael [2011-03-04 02:56:57]

Hi, this is a function that replaces "imagearc" to solve the thickness-problem. it does not use the global value set by imagesetthickness, so you have to pass it along.

"connecting" the arc to lines is still a problem, it sometimes shifts by 1px, but so does the original function.

Theres still alot to improve ...

global$cols;$rad+=$thickness/2;// to calculate outer edge$th_fact=1-((($thickness-1)/$rad));$pts=36*$rad;// adjust density$fact=$pts/360;$w1=$w1*$fact;$w2=$w2*$fact;

if($thickness>2)// to make sure we have neither gaps nor ugly looking artefactsimagesetthickness($handle,2);

elseimagesetthickness($handle,1);$winkel= (2*pi())/$pts;

for($i=$w1+1;$i

}imagesetthickness($handle,1);// just to reset}?>

[#5]

ajim1417 at gmail dot com [2010-01-28 19:42:59]

I wrote a simple function that can draws an arc counter-clockwisekly. Here it is :

returnimagearc($im,$cx,$cy,$w,$h,$start,$end,$c);

}?>

The params of this function is exactly the same as the usual imagearc function.

[#6]

anton dot vandeghinste at telenet dot be [2009-12-13 07:27:17]

I needed an arc with a thick border and i didn't like to use 359.9 as end angle so i made a function that works pretty well:

{

if($thick==1)

{

returnimagearc($image,$x,$y,$w,$h,$s,$e,$color);

}

for($i=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);

}

}?>

[#7]

mojiro at awmn dot net [2005-12-13 12:28:45]

A previous for the Rotated (Filled)Ellipse note from(nojer2 at yahoo dot com, 02-Apr-2001 12:06) has a mistake, at the second arc. Replace them with the following listing.

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);

}

[#8]

ruturaj_v at yahoo dot com [2004-05-17 06:32:11]

this is another piechart eg. very simple ...

functionget_polar($xrel,$yrel,$ang,$radius) {$i=$ang;$ang= ($ang*pi())/180;$ix=abs($radius*cos($ang));$iy=abs($radius*sin($ang));

if ($i>=0&&$i<=90) {$ix=$xrel+$ix;$iy=$yrel-$iy;

}

if ($i>90&&$i<=180) {$ix=$xrel-$ix;$iy=$yrel-$iy;

}

if ($i>180&&$i<=270) {$ix=$xrel-$ix;$iy=$yrel+$iy;

}

if ($i>270&&$i<=360) {$ix=$xrel+$ix;$iy=$yrel+$iy;

}$ix=floor($ix);$iy=floor($iy);//echo ($ix . " $iy
");$returnvals= array ('x1'=>$xrel,'y1'=>$yrel,'x2'=>$ix,'y2'=>$iy);

return$returnvals;

}

functionget_degtotal($degindex)

{

global$deg;

if ($degindex==0) {

return ($deg[$degindex] );

}

else {

return ($deg[$degindex] +get_degtotal($degindex-1) );

}

}$im=imagecreate(400,400);$w=imagecolorallocate($im,255,255,255);$black=imagecolorallocate($im,0,0,0);$red=imagecolorallocate($im,255,0,0);$green=imagecolorallocate($im,0,180,0);$randcolor[0] =imagecolorallocate($im,243,54,163);$randcolor[1] =imagecolorallocate($im,179,51,247);$randcolor[2] =imagecolorallocate($im,103,48,250);$randcolor[3] =imagecolorallocate($im,53,145,244);$randcolor[4] =imagecolorallocate($im,54,243,243);$randcolor[5] =imagecolorallocate($im,107,245,180);$randcolor[6] =imagecolorallocate($im,203,242,111);$randcolor[7] =imagecolorallocate($im,248,201,105);$data[0] =30;$data[1] =20;$data[2] =15;$data[3] =10;$data[4] =8;$data[5] =7;$data[6] =5;$data[7] =5;$datasum=array_sum($data);$deg[0] =number_format((30/$datasum*360),2,".","");$deg[1] =number_format((20/$datasum*360),2,".","");$deg[2] =number_format((15/$datasum*360),2,".","");$deg[3] =number_format((10/$datasum*360),2,".","");$deg[4] =number_format((8/$datasum*360),2,".","");$deg[5] =number_format((7/$datasum*360),2,".","");$deg[6] =number_format((5/$datasum*360),2,".","");$deg[7] =number_format((5/$datasum*360),2,".","");

echo ('

');//print_r($deg);$datadeg= array();$datapol= array();$degbetween= array();$databetweenpol= array();

for ($i=0;$i

}

for ($i=0;$i

}print_r($datadeg);print_r($degbetween);print_r($databetweenpol);//exit;for ($i=0;$i

}imagearc($im,200,200,200,200,0,360,$black);

for ($i=0;$i

}//header ("Content-type: image/png");imagepng($im,'piechart.png');?>

[#9]

jerryscript at aol dot com [2003-12-26 02:05:41]

[note-Apache/1.3.29 (Win32) PHP/4.3.4]

The imagearc (and imageellipse) functions do not accept line thicknesses when drawn from 0 to 360 degrees.

Drawing from 0 to 359 and again from 359 to 360 does create an ellipse with the current line thickness.

Jerry

[#10]

eamon at hostelworld dot com [2003-12-17 07:24:34]

Right...

possibly the easiest way of drawing a filled circle:

Loop through the imagearc function incrementing the diameter by one pixel:

}// --------------------- //?>

This works great for circles with diameters up to about 60 or 70 pixels wide. After that, you start to get pixle gaps.

[#11]

logang at deltatee dot com [2003-08-04 20:19:40]

Heres a function to make a curve between two points... This will be a downward curve but it wouldn't be hard to make a similar function to make an upward curve. The first point has to be to the left of the second point ($x1 

function ImageCurveDown ($image, $x1, $y1, $x2, $y2, $height, $color) {

$presicion = 1;

for ($left = ($x1-$x2); $left 

if ($y1 

$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 

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) 

ImageArc($image, $cx, $cy, $A1*2, $B1*2, 180+rad2deg($angle2), 360-rad2deg($angle1), $color);

}

}

}

[#12]

[2003-01-23 08:55:38]

[#13]

jinny at 263 dot net [2002-05-13 22:27:19]

imagesetstyle() sets the style to be used by all line drawing functions when drawing with the special color .

Here goes a example of drawing a dashed-line circle.enjoy!

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);?>

[#14]

arve at skogvold dot as [2001-11-30 13:04:31]

I found a better way for drawing a pie chart:

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

$diameter = 100;

$radius = $diameter / 2;

$centerX = $radius;

$centerY = $radius;

$im = @ImageCreate ($diameter, $diameter)

or die ("Cannot Initialize new GD image stream");

$background = ImageColorAllocate ($im, 0, 0, 0);

$red = ImageColorAllocate ($im, 176, 0, 0);

function 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);

// Will make a red filled arc, starting at 0 degrees, ending at 30 degrees

ImagePng ($im);

[#15]

marc at resiteit dot com [2001-11-08 13:04:58]

Round cornered anti-aliased dynamically sized button.

$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);

[#16]

foripepe at yahoo dot com [2001-09-02 09:56:57]

To fill an arc (DiameterX != DiameterY):

}?>

To close the arc with 2 lines (DiameterX != DiameterY):

}?>

An example:

$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);?>

[#17]

[2001-04-08 22:37:11]

The following site contains heaps of different functions to draw graphs with easy to follow code for newbies and heaps of examples with OVER 60 different predefined graphs

http://www.aditus.nu/jpgraph/index.php

[#18]

nojer2 at yahoo dot com [2001-04-01 14:18:28]

Here's a dashed circle function:

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;

}

}?>

[#19]

nojer2 at yahoo dot com [2001-04-01 14:06:37]

Here's the function to draw rotated ellipses again. This time I've optimised it a bit, fixed the no-fill bug, and used a 'squishratio' rather than a 'radiusmodifier', to make the curves perfect, so ignore my previous version.

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;

}

}

functiontriangle($im,$x1,$y1,$x2,$y2,$x3,$y3,$colour) {$coords= array($x1,$y1,$x2,$y2,$x3,$y3);imagefilledpolygon($im,$coords,3,$colour);

}?>

[#20]

ericquil at yahoo dot com [2000-09-29 04:30:14]

If circles overlap, a temporary border is needed when filling:

ImageArc($im,$x,$y,$w,$h,0,360,$temp_color);ImageFillToBorder($im,$x,$y,$temp_color,$fill_color);ImageArc($im,$x,$y,$w,$h,0,360,$fill_color);?>

[#21]

cbriou at orange-art dot fr [2000-09-17 19:03:46]

There is another way to fill an arc :

[#22]

timothyhouck at yahoo dot com [2000-08-16 15:58:36]

To do filled arcs, try something like this:

$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--;

}?>

...well you get the point.  It's a kludge, and *very* slow, but it's free.

[#23]

travis at duluth dot com [1999-12-22 18:36:03]

The wierd thing is that the first two integers tell where to place the "circle".

So for example I first create the "pallet" to place the circle on.

$image = imagecreate(500, 500);

(this makes a huge 500x500 gif :) )

$colorBody = imagecolorallocate($image, 0, 0, 0);

(make the default color of the "pallet" black

$circleColor = imagecolorallocate($image, 255, 0, 255);

(going to make the circle an ugly pink color)

imagearc($image, 250, 250, 300, 300, 0, 360, $circleColor);

Places the image in the center (250,250) and the circle is 300 pixels in diameter.

Hope this helps.

Travis Kent Beste

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值