Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.14     Updating the drawing script

This is NOT the latest copy of this book; click here for the latest version.

Now you know how to draw individual pixels and lines, we can go back and edit the polygon drawing script from before so that it does something more interesting for the first two clicks. From click three onwards we have enough points to draw a polygon, but by click one we have enough for a point and by click two we have enough for a line. What we're going to do is edit the switch/case block in picture3.php so that in case 1 we draw a point and in case 2 we draw a line, leaving case 3 for polygon drawing. Therefore, the switch/case block needs to be changed to this:

switch($pointcount) {
    case
0:
        break;
    case
1:
        
imagesetpixel($image, $_SESSION['points'][0], $_SESSION['points'][1], $green );
        break;
    case
2:
        
imageline($image, $_SESSION['points'][0], $_SESSION['points'][1], $_SESSION['points'][2], $_SESSION['points'][3], $green );
        break;
    default:
        
imagefilledpolygon($image, $_SESSION['points'], $pointcount, $green );
}

That finishes off the drawing script nicely, although there are other possible changes if you're willing to try them out yourself:

  • Add a dropdown box to select the pen colour and pen thickness

  • Let people choose whether to use a fill image or a plain colour. Even better, let people upload their own pictures for fills!

  • Add an option to let people save their image on the server for later editing. This is best done by serializing the points array and saving it in a database.

  • For extra points, allow people to save their image as a graphics file, then load it back in for re-editing. This has the advantage of letting them draw one polygon, save it out as an image, then reload that image and draw another, different polygon on top.





<< 11.2.13 Points and lines: imagesetpixel(), imageline(), and imagesetthickness()   11.2.15 Special effects using imagefilter() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow