PHP draws an ellipse

PHPz
Release: 2024-03-21 13:02:02
forward
885 people have browsed it

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);
?>
Copy after login

2. Create image

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

3. Assign colors

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

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);
?>
Copy after login

5. Output image

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

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);
?>
Copy after login

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

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!