9.6. Dynamically Generated ButtonsA popular use for dynamically generated images is to create images for buttons on the fly (this was introduced to you in Chapter 1 as well). Normally, a blank button background image is used and text is overlaid on top of it, as shown in Example 9-8. Example 9-8. Creating a dynamic button
In this case, the blank button (button.png) looks as shown in Figure 9-7. Figure 9-7. Blank ButtonThe script in Example 9-8 can be called from a page like this:
This HTML generates the button shown in Figure 9-8. Figure 9-8. Button with generated text labelThe + character in the URL is the encoded form of a space. Spaces are illegal in URLs and must be encoded. Use PHP's urlencode( ) function to encode your button strings. For example:
9.6.1. Caching the Dynamically Generated ButtonsIt is somewhat slower to generate an image than to send a static image. For buttons that will always look the same when called with the same text argument, a simple cache mechanism can be implemented. Example 9-9 generates the button only when no cache file for that button is found. The $path variable holds a directory, writable by the web server user, where buttons can be cached. The filesize( ) function returns the size of a file, and readfile( ) sends the contents of a file to the browser. Because this script uses the text form parameter as the filename, it is very insecure (Chapter 12 explains why and how to fix it). Example 9-9. Caching dynamic buttons
9.6.2. A Faster CacheExample 9-9 is still not quite as quick as it could be. There is a more advanced caching First, create a buttons directory somewhere under your web server's DocumentRoot and make sure that your web server user has permissions to write to this directory. For example, if the DocumentRoot directory is /var/www/html, create /var/www/html/buttons. Second, edit your Apache httpd.conf file and add the following block:
This tells Apache that requests for nonexistent files in the buttons directory should be sent to your button.php script. Third, save Example 9-10 as button.php. This script creates new buttons, saving them to the cache and sending them to the browser. There are several differences from Example 9-9, though. We don't have form parameters in $_GET, because Apache handles error pages as redirections. Instead, we have to pull apart values in $_SERVER to find out which button we're generating. While we're at it, we delete the '..' in the filename to fix the security hole from Example 9-9. Once button.php is installed, when a request comes in for something like http://your.site/buttons/php.png, the web server checks whether the buttons/php.png file exists. If it does not, the request is redirected to our button.php script, which creates the image (with the text "php") and saves it to buttons/php.png. Any subsequent requests for this file are served up directly without a line of PHP being run. Example 9-10. More efficient caching of dynamic buttons
The only drawback to the mechanism in Example 9-10 is that the button text cannot contain any characters that are illegal in a filename. Nonetheless, this is the most efficient way to cache dynamically generated images. If you change the look of your buttons and you need to regenerate the cached images, simply delete all the images in your buttons directory, and they will be recreated as they are requested. You can also take this a step further and get your button.php script to support multiple image types. Simply check $ext and call the appropriate ImagePNG( ), ImageJPEG( ), or ImageGIF( ) function at the end of the script. You can also parse the filename and add modifiers such as color, size, and font, or pass them right in the URL. Because of the parse_str( ) call in the example, a URL such as http://your.site/buttons/php.png?size=16 displays "php" in a font size of 16. |
Tuesday, November 3, 2009
Section 9.6. Dynamically Generated Buttons
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment