Home >Backend Development >PHP Tutorial >PHP draws an ellipse

PHP draws an ellipse

PHPz
PHPzforward
2024-03-21 13:00:19925browse

php editor Strawberry introduces you how to draw an ellipse using PHP language. The ellipse is a simple yet elegant geometric shape that is often used in web design and data visualization. PHP language provides GD library and ImageMagick extension, which can be used to draw ellipses to make your web pages or applications more vivid and attractive. Next, let's learn how to draw an ellipse using PHP!

PHP Draw Ellipse

Preface

phpThe language provides a rich function library, among which the GD library is specially used for image processing and can draw various shapes in PHP, including ellipses.

Draw an ellipse

1. Load GD library

<?php
//Load the GD library
imagettftext($im, 12, 0, 50, 50, $color, $font, $text);
?>

2. Create image

<?php
// Create a new image
$im = imagecreatetruecolor(640, 480);
?>

3. Assign colors

<?php
// assign black
$black = imagecolorallocate($im, 0, 0, 0);
?>

4. Draw an ellipse

<?php
//Draw an ellipse with the center coordinates of (200, 200), the major axis radius of 100, the minor axis radius of 50, and fill with black
imageellipse($im, 200, 200, 100, 50, $black);
?>

5. Output image

<?php
//output image
header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);
?>

other options

In addition to basic ellipse drawing, the GD library also provides other options to control the appearance of the ellipse:

1. Filling

Use the imagefilledellipse() function to fill the ellipse.

2. Line width

The line width of the ellipse can be set through the imagelinewidth() function.

3. Starting point and end point

imagearc() The function allows drawing an elliptical arc from the starting angle to the ending angle.

Sample code

<?php
//Load the GD library
imagettftext($im, 12, 0, 50, 50, $color, $font, $text);

// Create a new image
$im = imagecreatetruecolor(640, 480);

// assign color
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);

//Draw a filled ellipse
imagefilledellipse($im, 200, 200, 100, 50, $black);

//Draw an ellipse with a line width of 5
imagelinewidth($im, 5);
imageellipse($im, 350, 200, 100, 50, $red);

//Draw an elliptical arc
imagearc($im, 500, 200, 100, 50, 45, 135, $red);

//output image
header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);
?>

The above is the detailed content of PHP draws an ellipse. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete