|
Drawing Rectangles Now that we have read a bit about polygons, you
will whizz through this section. The GD function for drawing rectangles is,
funnily enough, called imagerectangle(). It accepts 6 arguments: the image
identifier, the top left x coordinate, and the y coordinate, and the bottom
right x coordinate, and the y coordinate, and the colour identifier.
imagerectangle() simply draws a rectangle from point [x1],[y1] to [x2],[y2]
using the specified colour and image identifier. Yep, that’s it! Let’s try:
<?php
$im = imagecreate(400, 400);
$bg = imagecolorallocate($im, 0, 0, 0);
# simple black background
$red = imagecolorallocate($im, 255, 0, 0);
imagerectangle($im, 50,50, 350,200, $red);
# rectangle drawn
# output image
?>
|
This script should output a black square, 400px wide and
high, with the outline of a rectangle from point 50,50 to point 350,200.
This only goes half way down the image simply to demonstrate that we don't
HAVE to draw squares.
Once completed, it should look something like this:

Drawing Circles & Ellipses We can use the same methodology to draw
ellipses or circles. The function imageellipse() will do either, depending on
how we input arguments into it.
imageellipse() accepts 6 arguments: the image identifier, the centre point for
the circle [x], and [y], the width, the height, and the colour identifier.
When making circles, just pass the same variable in for the height as you do for
the width, so both will be the same. For ellipses, specify each separately, to
create the effect of a squashed circle.
Yep, GD functions are quite similar a lot of the time! Let’s try it:
<?php
$im = imagecreate(400, 400);
$bg = imagecolorallocate($im, 0, 0, 0);
# let’s just use a black background.
$red = imagecolorallocate($im, 255, 0, 0);
# our red colour defined for the circle
$white = imagecolorallocate($im, 255, 255, 255);
# white for the ellipse
$circle = 200;
$ellipse["w"] = 300;
$ellipse["h"] = 100;
imageellipse($im, 200, 200, $circle, $circle, $red);
# our circle is drawn in the middle
imageellipse($im, 200, 200, $ellipse["w"], $ellipse["h"], $white);
# our ellipse is drawn over the top
# output here, we will cover this later
?>
|
You don’t have to define the ellipse width and height in an
array, but I thought I may as well because I am exampling a circle and an
ellipse in the same script. This should draw the outline of a red circle in
the middle, and the outline of a white “squashed circle” – ellipse over the
top. It should look like this:

- Tutorial written by Scrowler
|