Points and lines

int imagesetpixel ( resource image, int x, int y, int color)

int imageline ( resource image, int x1, int y1, int x2, int y2, int color)

int imagesetthickness ( resource image, int thickness)

Although drawing points and lines is quite easy to learn, I have left the topic until now because they are generally less useful than shapes and fills. However, a few chapters ago we put together a script that allows users to draw their own polygons by clicking on the screen. If you recall, you had to click three times before anything happened because polygons need at least three points to be drawn.

The script that handled the drawing used a switch/case block to do different actions based upon the number of points that were set, which means we can easily extend that script so that we draw a point if only one co-ordinate is set, or a line if two are set. First things first, though: how to draw points and lines in a standalone script.

Drawing points is accomplished with the function imagesetpixel(), which takes four parameters: the image to draw on, the X and Y co-ordinates, and the colour to use. Thus, you can use it like this:

<?php
    $width = 255;
    $height = 255;
    $image = imagecreatetruecolor($width, $height);

    for ($i = 0; $i <= $width; ++$i) {
        for ($j = 0; $j <= $height; ++$j) {
            $col = imagecolorallocate($image, 255, $i, $j);
            imagesetpixel($image, $i, $j, $col);
        }
    }

    header("Content-type: image/png");
    imagepng($image);
    imagedestroy($image);
?>

In that example, we have two loops to handle setting the green and blue parameters to imagecolorallocate(), with red always being set to 255. This colour is then used to set the relevant pixel to the newly allocated colour, which should give you a smooth gradient.

Moving on, drawing lines is only a little more difficult than individual pixels, and is handled by the imageline() function. This time the parameters are the image to draw on, the X and Y co-ordinates of the start of the line, the X and Y co-ordinates of the end of the line, and the colour to use for drawing. We can extend our pixel script to draw a grid over the gradient by looping from 0 to $width and $height, incrementing by 15 each time, and drawing a line at the appropriate place. The reason $width and $height were both set to 241 in the previous script is that it is 255 - 15 + 1, which means it is the largest grid we can draw using the stock 0-255 colour range. The reason the +1 is necessary is because drawing a line on the 255th row of the picture would be invisible - it would be outside!

So, add these lines before the header() call:

for ($i = 0; $i <= $width; $i += 15) {
    imageline($image, $i, 0, $i, 255, $black);
}

for ($i = 0; $i <= $height; $i += 15) {
    imageline($image, 0, $i, 255, $i, $black);
}

The first loop draws the vertical lines, so the X co-ordinate increments by 15 with each loop, whereas the Y co-ordinates are always 0 and 255, or from the very top to the very bottom. The second loop does the same for the horizontal lines, so this time it is the Y co-ordinates that change.

To get the script to work, you will also need to add this line after the call to imagecreatetruecolor():

$black = imagecolorallocate($image, 0, 0, 0);

Run the script and see what you get.

The final function that is good to keep in mind when working with lines is imagesetthickness(), which allows you to specify the width in pixels of all lines drawn. When I say "all lines drawn", that is what I mean - lines drawn using imageline() are affect, but it also affects rectangles, arcs, etc. To use the function, pass in the image to alter as parameter one, and the width in pixels as parameter two, then simply draw lines. The new thickness remains in place until you change it again or destroy the image.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Updating the drawing script >>

Previous chapter: Scaling and rotating

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.