PHP Graphics Drawing Lines, Rectangles, and Polygons - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Graphics Drawing Lines, Rectangles, and Polygons - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, June 22, 2019

PHP Graphics Drawing Lines, Rectangles, and Polygons

PHP Graphics


Drawing Lines, Rectangles, and Polygons

Problem

You want to draw a line, rectangle, or polygon. You also want to be able to control if the rectangle or polygon is open or filled in. For example, you want to be able to draw bar charts or create graphs of stock quotes.

Solution

To draw a line, use ImageLine():

       $width = 200;
       $height = 50;
       $image = ImageCreateTrueColor($width, $height);

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

       $x1 = $y1 = 0 ; // 0
       $x2 = $y2 = $height - 1; // 49
       $color = 0xCCCCCC; // gray

       ImageLine($image, $x1, $y1, $x2, $y2, $color);

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

To draw an open rectangle, use ImageRectangle():

       ImageRectangle($image, $x1, $y1, $x2, $y2, $color);

To draw a solid rectangle, use ImageFilledRectangle():

       ImageFilledRectangle($image, $x1, $y1, $x2, $y2, $color);

To draw an open polygon, use ImagePolygon():

       $points = array($x1, $y1, $x2, $y2, $x3, $y3);
       ImagePolygon($image, $points, count($points)/2, $color);

To draw a filled polygon, use ImageFilledPolygon():

       $points = array($x1, $y1, $x2, $y2, $x3, $y3);
       ImageFilledPolygon($image, $points, count($points)/2, $color);

Discussion

The prototypes for all five functions in the Solution are similar. The first parameter is the canvas to draw on. The next set of parameters are the x and y coordinates to specify where GD should draw the shape. In ImageLine(), the four coordinates are the end‐points of the line, and in ImageRectangle(), they’re the opposite corners of the rectangle. For example, ImageLine($image, 0, 0, 100, 100, $color) produces a diagonal line. Passing the same parameters to ImageRectangle() produces a rectangle with corners at (0,0), (100,0), (0,100), and (100,100).

Figure  A diagonal line and a square


The ImagePolygon() function is slightly different because it can accept a variable number of vertices. Therefore, the second parameter is an array of x and y coordinates. The function starts at the first set of points and draws lines from vertex to vertex before finally completing the figure by connecting back to the original point. You must have a minimum of three vertices in your polygon (for a total of six elements in the array). The third parameter is the number of vertices in the shape; since that’s always half of the number of elements in the array of points, a flexible value for this is count($points) / 2 because it allows you to update the array of vertices without breaking the call to ImageLine().

For example, to draw a right triangle, this generates the image in Figure :

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

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

       // three points for right triangle
       $x1 = $y1 = 0 ;                   // ( 0, 0)
       $x2 = $y2 = $size - 1;      // (49,49)
       $x3 = 0; $y3 = $size - 1; // ( 0,49)

       $gray = 0xCCCCCC; // gray

       $points = array($x1, $y1, $x2, $y2, $x3, $y3);
       ImagePolygon($image, $points, count($points)/2, $gray);

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

Figure  A right triangle


Last, all the functions take a final parameter that specifies the drawing color. This is usually a color value (e.g., 0xCCCCCC), but can also be the constants IMG_COLOR_STYLED or IMG_COLOR_STYLEDBRUSHED, if you want to draw nonsolid lines.

These functions all draw open shapes. To get GD to fill the region with the drawing color, use ImageFilledRectangle() and ImageFilledPolygon() with the identical set of arguments as their unfilled cousins.



No comments:

Post a Comment

Post Top Ad