PHP Graphics Drawing Arcs, Ellipses, and Circles - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Graphics Drawing Arcs, Ellipses, and Circles - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, June 23, 2019

PHP Graphics Drawing Arcs, Ellipses, and Circles

PHP Graphics


Drawing Arcs, Ellipses, and Circles


Problem

You want to draw open or filled curves. For example, you want to draw a pie chart showing the results of a user poll.

Solution

To draw an arc, use ImageArc():

       ImageArc($image, $x, $y, $width, $height, $start, $end, $color);

To draw an ellipse, use ImageEllipse():

       ImageEllipse($image, $x, $y, $width, $height, $color);

To draw a circle, use ImageEllipse(), and use the same value for both $width and $height:

       ImageEllipse($image, $x, $y, $diameter, $diameter, $color);

Discussion

Because the ImageArc() function is highly flexible, you can create many types of curves.Like many GD functions, the first parameter is the canvas. The next two parameters are the x and y coordinates for the center position of the arc. After that comes the arc width and height.

The sixth and seventh parameters are the starting and ending angles, in degrees. A value of 0 is at three o’clock. The arc then moves clockwise, so 90 is at six o’clock, 180 is at nine o’clock, and 270 is at the top of the hour. (Be careful—this behavior is not consistent among all GD functions. For example, when you rotate text, you turn in a counter‐clockwise direction.) Because the arc’s center is located at ($x,$y), if you draw a semicircle from 0 to 180, it doesn’t start at ($x,$y); instead, it begins at ($x+ ($diameter/2),$y).

As usual, the last parameter is the arc color.

The ImageEllipse() function is similar ImageArc(), except that the starting and ending angles are omitted because they’re hardcoded to 0 and 360. Because a circle is an ellipse with the same width and height, to draw a circle, set both numbers to the diameter of the circle.

For example, this draws an open black circle with a diameter of 100 pixels centered on the canvas, as shown in the left half of figure:

       $size = 100;
       $image = ImageCreateTrueColor($size, $size);

       $background_color = 0xFFFFFF; // white
       ImageFilledRectangle($image, 0, 0, $size - 1, $size - 1, $background_color);

       $black = 0x000000;
       ImageEllipse($image, $size / 2, $size / 2, $size - 1, $size - 1, $black);

To produce a solid ellipse or circle, call ImageFilledEllipse():

       ImageFilledEllipse($image, $size / 2, $size / 2, $size - 1, $size - 1, $black);

The output is shown in the right half of Figure.

Figure  An open black circle and a filled black circle


There is also an ImageFilledArc() function. It takes an additional final parameter that describes the fill style. Use IMG_ARC_CHORD to connect the start and end points of the arc with a straight line or IMG_ARC_PIE to only draw the arc itself. To avoid filling the arc, use IMG_ARC_NOFILL. These can be bitwise ored together in a variety of combinations to create pie wedges and other interesting shapes. For example:

       $styles = [IMG_ARC_PIE,
                          IMG_ARC_CHORD,
                          IMG_ARC_PIE | IMG_ARC_NOFILL,
                          IMG_ARC_PIE | IMG_ARC_NOFILL | IMG_ARC_EDGED];

       $size = 100;
       $image = ImageCreateTrueColor($size * count($styles), $size);

       $background_color = 0xFFFFFF; // white
       ImageFilledRectangle($image, 0, 0,
              $size * count($styles) - 1, $size * count($styles) - 1, $background_color);
       $black = 0x000000; // aka 0

       for ($i = 0; $i < count($styles); $i++) {
              ImageFilledArc($image, $size / 2 + $i * $size, $size / 2,
                     $size - 1, $size - 1, 0, 135, $black, $styles[$i]);
       }

       header('Content-type: image/png');
       ImagePNG($image);
       ImageDestroy($image);

This generates the shapes in Figure.
Figure  An assortment of pie wedges




No comments:

Post a Comment

Post Top Ad