draw a circle in php

To draw a circle in PHP, we can use the GD library which allows us to create and manipulate images on the fly. Here's an example code block that demonstrates drawing of a circle using PHP:

main.php
// Create a new image with given width and height
$img = imagecreatetruecolor(200, 200);

// Allocate colors
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);

// Draw a circle at the center with given radius
$centerX = imagesx($img) / 2;
$centerY = imagesy($img) / 2;
$radius = 50;
imagefilledellipse($img, $centerX, $centerY, $radius * 2, $radius * 2, $white);
imageellipse($img, $centerX, $centerY, $radius * 2, $radius * 2, $black);

// Output image
header('Content-type: image/png');
imagepng($img);

// Clean up memory
imagedestroy($img);
589 chars
21 lines

In this code, we first create an empty image using imagecreatetruecolor() function with width and height specified as 200 pixels each. Then we allocate two colors - white for filling the circle and black for drawing the circle's outline.

We then compute the coordinates of the center of the image and set the radius of the circle to be 50 pixels. We use imagefilledellipse() function to fill the circle with white color and imageellipse() function to draw the outline in black color.

Finally, we output the image using header() function and clean up memory using imagedestroy() function.

gistlibby LogSnag