|
 |
imagefilledellipse (PHP 4 >= 4.0.6, PHP 5) imagefilledellipse -- Draw a filled ellipse Descriptionbool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() draws an ellipse centered at
cx, cy (top left is
0, 0) in the image represented by image.
W and h specifies the
ellipse's width and height respectively. The ellipse is filled using
color. Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.
Пример 1. imagefilledellipse() example
<?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);
?>
|
|
Замечание: Эта функция нуждается в GD версии 2.0.1 или выше.
See also imageellipse() and
imagefilledarc().
imagefilledellipse
jbr at ya-right dot com
22-Feb-2006 07:33
This is a niffty function that you can use to make transparent ellipse/round type cutouts of any PNG or GIF image. The hard part is finding what color to use for the cutout layer and then the transparent layer, because you don't want to set the transparent index to a color being used in the image. After that it's as simple as layering the two images together.
what you need for the example...
a image of the size you want the cutout to be, gif or png (true color /256) can be used!
<?
$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 );
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 );
}
$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 );
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;
}
}
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++;
}
}
imagedestory ( $img );
return ( $colors );
}
?>
You can try a demo here (SGML) capture a web page, then make multi cutouts example!
http://www.ya-right.com/
mark at freegeekchicago dot org
11-Oct-2005 10:12
Here is a simple script using imagefilledellipse to created rounded corners on the fly. It takes color, bg_color, width, height, and placement (i.e. top left, bottom right) as arguments.
<?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;
$image = imagecreatetruecolor($c_width, $c_height);
$col_ellipse = hex2int($image, $color);
$bg = hex2int($image, $bg_color);
imagefill($image, 0, 0, $bg);
if ($placement == "br") {imagefilledellipse($image, 0, 0, $width, $height, $col_ellipse);}
if ($placement == "tl") {imagefilledellipse($image, $c_width, $c_height, $width, $height, $col_ellipse);}
if ($placement == "tr") {imagefilledellipse($image, 0, $c_height, $width, $height, $col_ellipse);}
if ($placement == "bl") {imagefilledellipse($image, $c_width, 0, $width, $height, $col_ellipse);}
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);
}
?>
You would call it like so:
<img src="round_test.php?
width=50&height=50&color=fffefe&bg_color=99e1e0&placement=tr">
richard at mf2fm dot co dot uk
09-Feb-2005 02:25
Here is a piece of code using imagefilledellipse which creates a simulation of the current phase of the moon...
Usage is <img src="moon.php?size=100"> which produces an image 100px by 100px wide. If size is left out, the default is 24px by 24px.
<?php
$mps=2551442.8; $position=time()-mktime(10, 32, 0, 1, 25, 2005); $position=($position-$mps*intval($position/$mps))/$mps; $position=2*(0.5-$position);
$size=$_GET['size'];
if (!is_numeric($size)) $size=24; $moon=imagecreate($size, $size);
$dark=imagecolorallocate($moon, 0, 34, 68); $light=imagecolorallocate($moon, 238, 238, 255); $corona=imagecolorallocatealpha($moon, 153, 153, 153, 64); $background=imagecolorallocatealpha($moon, 0, 0, 0, 127);
imagefill($moon, 0, 0, $background);
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); elseif (abs($position)>1-1/$size) imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $light); 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);
}
}
}
header ("Content-Type: image/png");
imagepng($moon);
imagedestroy($moon);
?>
dave at corecommunications dot us
10-Oct-2002 06:56
I needed to draw translucent circles on an existing image, using imagealphablending($image,true);. Turns out that imagefilledellipse seems to do the ellipse by drawing a series of lines from the centroid out to the circumference. Problem with this is that pixels near the center very visibly get drawn multiple times, producing a kind of moire effect. Also, the 0-degree (center to right) line is drawn twice and so is twice as dark the pixel below it.
My workaround was to draw the ellipse solid in a copy of the source image and do an imagecopymerge back into the original.
ivank at 2xtreme dot net
30-Mar-2002 04:31
Sometimes, you have x1, y1, x2, y2 parameters, but not the center, width, and height. Use this to draw a filled ellipse from your x1, y1, x2, and y2:
ImageFilledEllipse(
$im,
($x1 + round(($x2 - $x1) / 2)),
($y1 + round(($y2 - $y1) / 2)),
($x2 - $x1),
($y2 - $y1),
$color);
harry dot wood at ic dot ac dot uk
19-Jun-2001 04:09
This draws a rotated ellipse. If you don't want filled ellipses, then you don't need the triangle function.
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;
}
}
| |