In addition to creating images from other images and drawing images on your own, you can also create images based on user input. There's no fundamental difference in how the scripts are created except for the fact that you'll be gathering values from a form instead of hard-coding them into your script.
In Listing 14.7, we create an all-in-one form and script, which asks for user input for a variety of attributes ranging from image size to text and background colors, as well as a message string. You'll be introduced to the imagestring() function, which is used to "write" a string onto an image.
Listing 14.7. Creating an Image from User Input
Let's get into the script, where lines 238 represent the user input form, and the remaining lines handle the image created per user specifications.
In this basic form, you see that several fields are used to obtain image specifications. On lines 1213 there are fields to define the width and the height of the image we want to draw. Next, we set up fields to obtain the RGB values for a background color (lines 1517) and a text color (lines 1921).
By the Way
You could create drop-down list boxes containing values 0 through 255, for the red, green, and blue values. This would ensure that the user input was within the required range.
Line 23 contains a form field for the input string. This string will be drawn onto the background of the image in the text color specified. Lines 2531 represent a drop-down list for the selection of the font size. There are five sizes, 1 through 5, for the default fixed-width font.
By the Way
You can specify fonts using the imageloadfont() and imagettftext() functions. Learn more at http://www.php.net/image.
Finally, lines 33 and 34 allow you to define the text starting position. The upper-left corner of the image area would be X position 0, Y position 0; 10 increments downward would be Y position 10, 10 increments to the right would be X position 10, and so forth.
If we stopped the script here and closed up the if...else statement and PHP block, we would see a form like Figure 14.8 when loaded in our web browser.
Figure 14.8. User input form for image creation.
In only 18 more lines, we can finish this script and generate images with text strings, so take a look at the remainder of Listing 14.7.
The majority of lines 3961 you've already seen before, only this time we use extracted elements from the $_POST superglobal to take the place of hard-coded values. In line 42 we use the width and height values from the form to set up the initial image. Lines 4547 define two colors, $background and $text, using the appropriate RGB values provided by the form.
By the Way
The colors weren't given actual color names in this script because we don't know what the user input would createwe could call the color $red, but if they defined it as 0,255,0 we'd look stupid because that's the RGB value for green! Instead, we simply name our colors after their purpose, not their appearance.
Lines 5152 represent the only new item in this script, the use of the imagestring() function. The six parameters for this function are the image stream ($myImage), the font size ($_POST["font_size"]), the starting X and Y positions ($_POST["x"] and $_POST["y"]), the string to be drawn ($_POST["string"]), and the color in which to draw it ($text). Lines 5556 output the image to the browser, and line 59 destroys and cleans up the image creation process.
If we save this file as imagecreate.php, place it in the document root of the web server, and fill out the form, the output could look something like Figure 14.9. But quite likely your results will differ because there are many variables to play with!
Figure 14.9. Sample output from image creation form.
Try it yourself, using various sizes, colors, and text strings.