PHP Conference Japan 2024

imagefilledellipse

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

imagefilledellipse繪製實心橢圓

描述

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

在指定的圖片上繪製一個以指定坐標為中心的橢圓。

參數

image

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

center_x

中心的 x 坐標。

center_y

中心的 y 坐標。

width

橢圓的寬度。

height

橢圓的高度。

color

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

返回值

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

更新日誌

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

範例

範例 #1 imagefilledellipse() 範例

<?php

// 建立空白圖片
$image = imagecreatetruecolor(400, 300);

// 填滿背景顏色
$bg = imagecolorallocate($image, 0, 0, 0);

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

// 繪製白色橢圓
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);

// 輸出圖片
header("Content-type: image/png");
imagepng($image);

?>

以上範例將輸出類似以下的結果

Output of example : imagefilledellipse()

注意事項

注意:

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

參見

新增筆記

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

richard at mf2fm dot co dot uk
19 年前
這是一段使用 imagefilledellipse 的程式碼,它可以模擬目前的月相...

使用方法為 <img src="moon.php?size=100">,這會產生一個 100px x 100px 的圖片。如果沒有指定大小,預設值為 24px x 24px。

<?php

$mps
=2551442.8; // moon phase in seconds (29 days, 12 hours, 44 mins, 2.8 secs)
$position=time()-mktime(10, 32, 0, 1, 25, 2005); // seconds since full moon at 10:32GMT on 25 Jan 2005
$position=($position-$mps*intval($position/$mps))/$mps; // phase from 0 to 1

$position=2*(0.5-$position);
## revised to produce easier to work with...
## $position=1 - full moon
## $position=0 - new moon
## $position=-1 - full moon

$size=$_GET['size'];
if (!
is_numeric($size)) $size=24; // width/height in pixels
$moon=imagecreate($size, $size);
$dark=imagecolorallocate($moon, 0, 34, 68); // background colour for moon
$light=imagecolorallocate($moon, 238, 238, 255); // foreground colour for moon
$corona=imagecolorallocatealpha($moon, 153, 153, 153, 64); // edge of moon (semi-transparent)

##
## Make transparent background
##
$background=imagecolorallocatealpha($moon, 0, 0, 0, 127);
imagefill($moon, 0, 0, $background);

##
## Make the moon!
##
imagefilledellipse($moon, round($size/2), round($size/2), $size, $size, $corona);
if (
$position>-1/$size AND $position<1/$size) imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $dark); // new moon
elseif (abs($position)>1-1/$size) imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $light); // full moon
elseif ($position>0) {
imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $light);
for (
$i=0; $i<$size-2; $i++) {
$xpos=($size-2)/2;
$xpos=1-($i/$xpos);
$xpos=sqrt(1-($xpos*$xpos));
$xpos=($size/2)+($position-0.5)*$xpos*($size-2);
imagesetpixel($moon, round($xpos), $i+1, $dark);
}
for (
$i=0; $i<$size; $i++) {
$set=0;
for (
$j=0; $j<$size; $j++) {
if (!
$set AND imagecolorat($moon, $j, $i)==$dark) $set=1;
elseif (
$set AND imagecolorat($moon, $j, $i)==$light) imagesetpixel($moon, $j, $i, $dark);
}
}
}
else {
imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $dark);
for (
$i=0; $i<$size-2; $i++) {
$xpos=($size-2)/2;
$xpos=1-($i/$xpos);
$xpos=sqrt(1-($xpos*$xpos));
$xpos=($size/2)+($position+0.5)*$xpos*($size-2);
imagesetpixel($moon, round($xpos), $i+1, $light);
}
for (
$i=0; $i<$size; $i++) {
$set=0;
for (
$j=0; $j<$size; $j++) {
if (!
$set AND imagecolorat($moon, $j, $i)==$light) $set=1;
elseif (
$set AND imagecolorat($moon, $j, $i)==$dark) imagesetpixel($moon, $j, $i, $light);
}
}
}

##
## And output the picture
##
header ("Content-Type: image/png");
imagepng($moon);
imagedestroy($moon);
?>
Mark
15 年前
這會繪製一個具有可選邊框的反鋸齒圓圈。使用 <img src="circle.php?r=50&fg=ff0000&bg=000000&bw=5&bc=ffff00"/> 呼叫它,可以繪製一個半徑為 50、前景顏色為紅色、背景顏色為黑色、邊框寬度為 5 且邊框顏色為黃色的實心圓。假設 register_globals 已開啟,但您可以輕鬆修復它。

<?php
header
('Content-type: image/png');

is_numeric($r) or $r = 8;
is_numeric($bw) or $bw = 0;
strlen($fg)==6 or $fg = 'e8e8e8';
strlen($bg)==6 or $bg = 'ffffff';
strlen($bc)==6 or $bc = '000000';

function
hex2rgb($im,$hex) {
return
imagecolorallocate($im,
hexdec(substr($hex,0,2)),
hexdec(substr($hex,2,2)),
hexdec(substr($hex,4,2))
);
}

$a = $r*2;
$b = $a*4;
$c = $b/2;
$d = $b;
$e = $d-($bw*8);

$im1 = imagecreatetruecolor($b,$b);
$im2 = imagecreatetruecolor($a,$a);
imagefill($im1,0,0,hex2rgb($im1,$bg));
if(
$bw) imagefilledellipse($im1,$c,$c,$d,$d,hex2rgb($im1,$bc));
imagefilledellipse($im1,$c,$c,$e,$e,hex2rgb($im1,$fg));
imagecopyresampled($im2,$im1,0,0,0,0,$a,$a,$b,$b);
imagepng($im2);
?>
sunbox at gmx dot net
18 年前
基於 felixbruns at <ANTI-SPAM>web dot de 的 imagegradientellipse 函式,繪製一個具有 Alpha 色彩支援的漸層填充橢圓。感謝 felixbruns :o)

function imagegradientellipsealpha($image, $cx, $cy, $w, $h, $ic, $oc){
$w = abs($w);
$h = abs($h);
$oc = array(0xFF & ($oc >> 0x10), 0xFF & ($oc >> 0x8), 0xFF & $oc);
$ic = array(0xFF & ($ic >> 0x10), 0xFF & ($ic >> 0x8), 0xFF & $ic);
$c0 = ($oc[0] - $ic[0]) / $w;
$c1 = ($oc[1] - $ic[1]) / $w;
$c2 = ($oc[2] - $ic[2]) / $w;
$ot = $oc >> 24;
$it = $ic >> 24;
$ct = ($ot - $it) / $w;
$i = 0;
$j = 0;
$is = ($w<$h)?($w/$h):1;
$js = ($h<$w)?($h/$w):1;
while(1){
$r = $oc[0] - floor($i * $c0);
$g = $oc[1] - floor($i * $c1);
$b = $oc[2] - floor($i * $c2);
$t = $ot - floor($i * $ct);
$c = imagecolorallocatealpha($image, $r, $g, $b, $t);
imageellipse($image, $cx, $cy, $w-$i, $h-$j, $c);
if($i < $w){
$i += $is;
}
if($j < $h){
$j += $js;
}
if($i >= $w && $j >= $h){
break;
}
}
}
felixbruns at <ANTI-SPAM>web dot de
18 年前
我快速製作了一個漸層橢圓函式(我從其他漸層函式中擷取了一些程式碼)。它的作用類似於 imageellipse 或 imagefilledellipse,但有兩個顏色參數:$ic 是漸層橢圓的內部顏色,$oc 是外部顏色。

<?php

function imagegradientellipse($image, $cx, $cy, $w, $h, $ic, $oc){
$w = abs($w);
$h = abs($h);
$oc = array(0xFF & ($oc >> 0x10), 0xFF & ($oc >> 0x8), 0xFF & $oc);
$ic = array(0xFF & ($ic >> 0x10), 0xFF & ($ic >> 0x8), 0xFF & $ic);
$c0 = ($oc[0] - $ic[0]) / $w;
$c1 = ($oc[1] - $ic[1]) / $w;
$c2 = ($oc[2] - $ic[2]) / $w;
$i = 0;
$j = 0;
$is = ($w<$h)?($w/$h):1;
$js = ($h<$w)?($h/$w):1;
while(
1){
$r = $oc[0] - floor($i * $c0);
$g = $oc[1] - floor($i * $c1);
$b = $oc[2] - floor($i * $c2);
$c = imagecolorallocate($image, $r, $g, $b);
imagefilledellipse($image, $cx, $cy, $w-$i, $h-$j, $c);
if(
$i < $w){
$i += $is;
}
if(
$j < $h){
$j += $js;
}
if(
$i >= $w && $j >= $h){
break;
}
}
}

?>
ivank at 2xtreme dot net
22 年前
有時,您有 x1、y1、x2、y2 參數,但沒有中心、寬度和高度。使用它可以根據您的 x1、y1、x2 和 y2 繪製一個填充的橢圓。

ImageFilledEllipse(
$im,
($x1 + round(($x2 - $x1) / 2)),
($y1 + round(($y2 - $y1) / 2)),
($x2 - $x1),
($y2 - $y1),
$color);
jbr at ya-right dot com
18 年前
這是一個巧妙的函式,可以用來製作任何 PNG 或 GIF 圖像的透明橢圓/圓形裁切。困難的部分是找到用於裁切圖層和透明圖層的顏色,因為您不希望將透明索引設定為圖像中使用的顏色。之後,只需將兩個圖像疊加在一起即可。

範例所需…

一張與您想要裁切大小相同的圖像,可以使用 gif 或 png(真彩色/256 色)!

<?

$original_image = './image.png';
$output_image = './new.png';
$temp_image = './temp'; // 路徑和名稱(不要包含副檔名)
$is_true_color = true;

$ext = substr ( $original_image, strrpos ( $original_image, '.' ) );
$temp_image .= $ext;
$new = image_get ( $ext, $original_image );
$width = imagesx ( $new );
$height = imagesy ( $new );

// 我們需要建立一個暫時的縮減圖像,以便我們可以取得顏色
// 在高位元真彩色圖像中(僅限 png 16,24 位元)

if ( $is_true_color )
{
imagetruecolortopalette ( $new, false, 256 );
image_make ( $new, $ext, $temp_image );
imagedestroy ( $new );
$colors = get_rgb ( $temp_image, $ext );
@unlink ( $temp_image );
$new = image_get ( $ext, $original_image );
}
else
{
$colors = get_rgb ( $original_image, $ext );
}

// 這會建立裁切圖層(2 種顏色,兩者都將變為透明)

$old = imagecreate ( $width, $height );
imageantialias( $old, true );
imagecolorallocate ( $old, $colors[0]['red'], $colors[0]['green'], $colors[0]['blue'] );
$bg = imagecolorallocate ( $old, $colors[1]['red'], $colors[1]['green'], $colors[1]['blue'] );
imagefilledellipse ( $old, floor ( $width / 2 ), floor ( $height / 2 ), $width, $height, $bg );
imagecolortransparent ( $old, $bg );
imagecopy ( $new, $old, 0, 0, 0, 0, $width, $height );
image_make ( $new, $ext, $output_image );
imagedestroy ( $old );
imagedestroy ( $new );

// 這會將兩個圖像疊加在一起,形成一個漂亮的橢圓/圓形透明圖像裁切

$old = imagecreate ( $width, $height );
$new = image_get ( $ext, $output_image );
$tbg = imagecolorallocate ( $old, $colors[0]['red'], $colors[0]['green'], $colors[0]['blue'] );


imagecopy ( $old, $new, 0, 0, 0, 0, $width, $height );


imagecolortransparent ( $old, $tbg );


image_make ( $old, $ext, $output_image );


imagedestroy ( $old );
imagedestroy ( $new );

/*
* 快捷函式 (1,2)
*/

// 返回被呼叫的圖像資源

function image_get ( $ext, $name )
{
switch ( $ext )
{
case '.gif'
return ( imagecreatefromgif ( $name ) );
break;
case '.png'
return ( imagecreatefrompng ( $name ) );
break;
}
}

// 輸出傳遞給它的已命名圖像

function image_make ( $io, $ext, $name )
{
switch ( $ext )
{
case '.gif'
imagegif ( $io, $name );
break;
case '.png'
imagepng ( $io, $name );
break;
}
}

// 取得圖像中不存在的(2 個)顏色

function get_rgb ( $image, $ext )
{
$x = 0;
$colors = array ();
$img = image_get ( $ext, $image );

for ( $color = 10; $color <= 250; $color++ )
{
if ( imagecolorexact ( $img, $color, $color, $color ) == -1 )
{
$colors[] = array ( 'red' => $color, 'green' => $color, 'blue' => $color );

if ( $x == 1 )
{
imagedestroy ( $img );
return ( $colors );
}

$x++;


}
}

imagedestroy ( $img );
return ( $colors );
}

?>

您可以在這裡嘗試一個演示(SGML)擷取網頁,然後製作多個裁切範例!

http://www.ya-right.com/
mark at freegeekchicago dot org
19 年前
這裡有一個使用 imagefilledellipse 即時創建圓角的簡單腳本。它接受顏色、背景顏色、寬度、高度和位置(例如左上、右下)作為參數。

<?php
$color
= $_REQUEST['color'];
$bg_color = $_REQUEST['bg_color'];

$c_width = $_REQUEST['width'];
$c_height = $_REQUEST['height'];

$placement = $_REQUEST['placement'];

$width = 2 * $c_width;
$height = 2 * $c_height;

// create a blank image
$image = imagecreatetruecolor($c_width, $c_height);

// fill the corner color
$col_ellipse = hex2int($image, $color);

// fill the background color

$bg = hex2int($image, $bg_color);

// fill the background color
imagefill($image, 0, 0, $bg);

// draw the ellipse
//takes (resource image, int cx, int cy, int w, int h, int color)

// bottom right corner
if ($placement == "br") {imagefilledellipse($image, 0, 0, $width, $height, $col_ellipse);}

// top right corner
if ($placement == "tl") {imagefilledellipse($image, $c_width, $c_height, $width, $height, $col_ellipse);}

// top left corner
if ($placement == "tr") {imagefilledellipse($image, 0, $c_height, $width, $height, $col_ellipse);}

// bottom left corner
if ($placement == "bl") {imagefilledellipse($image, $c_width, 0, $width, $height, $col_ellipse);}

// output the picture
header("Content-type: image/png");
imagepng($image);

function
hex2int($image, $color) {
$string = str_replace("#","",$color);
$red = hexdec(substr($string,0,2));
$green = hexdec(substr($string,2,2));
$blue = hexdec(substr($string,4,2));

$color_int = imagecolorallocate($image, $red, $green, $blue);
return(
$color_int);
}
?>

您可以這樣呼叫它
<img src="round_test.php?
width=50&height=50&color=fffefe&bg_color=99e1e0&placement=tr">
dave at corecommunications dot us
22 年前
我需要使用 imagealphablending($image,true) 在現有圖像上繪製半透明的圓形。結果發現,imagefilledellipse 似乎是通過從中心點到圓周繪製一系列線條來繪製橢圓的。這個方法的問題是,靠近中心的像素會明顯地被多次繪製,產生一種莫爾紋效果。此外,0 度(中心到右側)的線條會被繪製兩次,因此比它下面的像素顏色深兩倍。

我的解決方法是在源圖像的副本中繪製實心橢圓,然後使用 imagecopymerge 將其複製回原始圖像。
harry dot wood at ic dot ac dot uk
23 年前
這會繪製一個旋轉的橢圓。如果您不需要填滿的橢圓,則不需要 triangle 函式。

function triangle($x1,$y1, $x2,$y2, $x3,$y3, $colour) {
global $im;
$coords = array($x1,$y1, $x2,$y2, $x3,$y3);
imagefilledpolygon($im, $coords, 3, $colour);
}

function rotatedellipse($cx, $cy, $width, $height, $rotateangle, $colour, $filled=true) {
global $im;
$step=15;
$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 = ($ox * $sinangle) + ($oy * $cosangle);

if ($nopreviouspoint) {
$px=$x;
$py=$y;
$nopreviouspoint=false;
}

if ($filled) {
triangle($cx, $cy, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
triangle($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;
}
}
To Top