Home Backend Development PHP Tutorial PHP draws an ellipse

PHP draws an ellipse

Mar 21, 2024 pm 01:00 PM
gd library php programming php image processing Backend Development Draw an ellipse elliptical arc php draws an ellipse line width

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP format rows to CSV and write file pointer PHP format rows to CSV and write file pointer Mar 22, 2024 am 09:00 AM

PHP format rows to CSV and write file pointer

PHP changes current umask PHP changes current umask Mar 22, 2024 am 08:41 AM

PHP changes current umask

PHP creates a file with a unique file name PHP creates a file with a unique file name Mar 21, 2024 am 11:22 AM

PHP creates a file with a unique file name

PHP calculates MD5 hash of file PHP calculates MD5 hash of file Mar 21, 2024 pm 01:42 PM

PHP calculates MD5 hash of file

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

PHP returns an array with key values ​​flipped

PHP truncate file to given length PHP truncate file to given length Mar 21, 2024 am 11:42 AM

PHP truncate file to given length

PHP returns the numeric encoding of the error message in the previous MySQL operation PHP returns the numeric encoding of the error message in the previous MySQL operation Mar 22, 2024 pm 12:31 PM

PHP returns the numeric encoding of the error message in the previous MySQL operation

PHP get pi PHP get pi Mar 21, 2024 pm 01:52 PM

PHP get pi

See all articles