2024 年日本 PHP 研討會

imageellipse

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

imageellipse繪製橢圓

描述

imageellipse(
    GdImage $image,
    int $center_x,
    int $center_y,
    int $width,
    int $height,
    int $color
): bool

在指定的座標繪製一個橢圓。

參數

image

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

center_x

中心的 x 座標。

center_y

中心的 y 座標。

寬度

橢圓的寬度。

高度

橢圓的高度。

顏色

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

返回值

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

更新日誌

版本 描述
8.0.0 image 現在需要 GdImage 實例;先前需要有效的 gd 資源

範例

範例 #1 imageellipse() 範例

<?php

// 建立空白影像。
$image = imagecreatetruecolor(400, 300);

// 選擇背景顏色。
$bg = imagecolorallocate($image, 0, 0, 0);

// 使用上面選擇的顏色填滿背景。
imagefill($image, 0, 0, $bg);

// 選擇橢圓的顏色。
$col_ellipse = imagecolorallocate($image, 255, 255, 255);

// 繪製橢圓。
imageellipse($image, 200, 150, 300, 200, $col_ellipse);

// 輸出影像。
header("Content-type: image/png");
imagepng($image);

?>

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

Output of example : imageellipse()

注意事項

注意:

imageellipse() 會忽略 imagesetthickness() 的設定。

參見

新增筆記

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

simon_nuttall at hotmail dot com
18 年前
這是 nojer at yahoo dot com 的 rotatedellipse 函式的優化和錯誤修正版本。我已將其更改為與 imageellipse 的參數相容。原始版本請參閱 imagearc 的筆記。

<?php

function rotatedellipse($im, $cx, $cy, $width, $height, $rotateangle, $colour, $filled=false) {
// modified here from nojer's version
// Rotates from the three o-clock position clockwise with increasing angle.
// Arguments are compatible with imageellipse.

$width=$width/2;
$height=$height/2;

// This affects how coarse the ellipse is drawn.
$step=3;

$cosangle=cos(deg2rad($rotateangle));
$sinangle=sin(deg2rad($rotateangle));

// $px and $py are initialised to values corresponding to $angle=0.
$px=$width * $cosangle;
$py=$width * $sinangle;

for (
$angle=$step; $angle<=(180+$step); $angle+=$step) {

$ox = $width * cos(deg2rad($angle));
$oy = $height * sin(deg2rad($angle));

$x = ($ox * $cosangle) - ($oy * $sinangle);
$y = ($ox * $sinangle) + ($oy * $cosangle);

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

?>
julian
19 年前
如果您想在圖像的左上角顯示橢圓,您可以輕鬆計算相應的 cx 和 cy 值。此範例將繪製一個與圖像具有相同寬度和高度的橢圓。

<?php

$ellipse_width
= 100;
$ellipse_height = 200;

$ellipse_cx = ($ellipse_width / 2);
$ellipse_cy = ($ellipse_height / 2);

$img_x = $ellipse_width;
$img_y = $ellipse_height;

$img = imagecreate($img_x, $img_y);
$bg = imagecolorallocate($img, 255,255,255);

$ellipse_color = imagecolorallocate($img, 0, 0, 0);

imageellipse($img, $ellipse_cx, $ellipse_cy, $ellipse_width, $ellipse_height, $ellipse_color);

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

?>
agentyoungsoo at hanmail dot net
22 年前
當您想在 GD 2.0.2 以前的版本中使用「ImageEllipse」函式時,
您可以像下面這樣使用「ImageArc」

----------------------------------------------------
$file_name = "test.png";

$screen_x = 300;
$screen_y = 200;

$x1 = $screen_x / 2;
$y1 = $screen_y / 2;

$radius = 30;

$image = ImageCreate($screen_x, $screen_y);
$black = ImageColorAllocate($image, 0,0,0);

ImageArc($image, $x1, $y1, $radius , $radius , 0, 360, $black);

ImagePng($image, $file_name);
ImageDestroy($image);
----------------------------------------------------
To Top