略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: imagearc

2024-11-22

imagearc

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

imagearc画椭圆弧

说明

imagearc(
    resource $image,
    int $cx,
    int $cy,
    int $w,
    int $h,
    int $s,
    int $e,
    int $color
): bool

imagearc()cxcy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。wh 分别指定了椭圆的宽度和高度,起始和结束点以 se 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。

示例 #1 用 imagearc() 画一个圆

<?php
// 创建一个 200X200 的图像
$img imagecreatetruecolor(200200);
// 分配颜色
$white imagecolorallocate($img255255255);
$black imagecolorallocate($img000);
// 画一个黑色的圆
imagearc($img1001001501500360$black);
// 将图像输出到浏览器
header("Content-type: image/png");
imagepng($img);
// 释放内存
imagedestroy($img);
?>

参见 imageellipse()imagefilledellipse()imagefilledarc()

add a noteadd a note

User Contributed Notes 20 notes

up
3
chandlerklebs at gmail dot com
10 years ago
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.

<?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*20, 360, $color); //regular outlines arc

imagefilledarc($img, $cx, $cy, $r*1, $r*10, 90, $color,IMG_ARC_CHORD); //filled triangle with chord of circle
imagefilledarc($img, $cx, $cy, $r*1, $r*1180, 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);
?>
up
2
anton dot vandeghinste at telenet dot be
12 years ago
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:

<?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);
    }
}
?>
up
2
eamon at hostelworld dot com
18 years ago
Right...
possibly the easiest way of drawing a filled circle:
Loop through the imagearc function incrementing the diameter by one pixel:
<?
// --- code fragment --- //

for($i=1; $i<$Diameter; $i++){
  imagearc($Image, $CenterX, $CenterY, $i, $i, $Start, $End, $Color);
}

// --------------------- //

?>

This works great for circles with diameters up to about 60 or 70 pixels wide. After that, you start to get pixle gaps.
up
2
jinny at 263 dot net
20 years ago
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!

<?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);
?>
up
1
lucas dot delmas at live dot fr
8 years ago
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.
up
1
marc at resiteit dot com
20 years ago
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);
up
0
cleverphp
4 years ago
imagearc example works not well,as it lacks of this line
"$white = imagecolorallocate($img, 255, 255, 255);
    imagefill($img,0,0,$white);
    $black = imagecolorallocate($img, 0, 0, 0);
"
up
0
joe dot tym at gmail dot com
9 years ago
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.

<?php
function 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);
                }
        }
}
?>
up
0
mojiro at awmn dot net
16 years ago
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);
}
up
0
jerryscript at aol dot com
18 years ago
[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