787Chapter 42GraphicsEmbedded images from filesOf course, HTML has had the
tag for a long time. This enables you to embed an imageby specifying its file path or URL, like this:
This works with static image files, but there is no reason why the image can t have beenrecently created. So you can have a script that 1) creates an image, 2) writes the image datato a local file, and then 3) produces HTML with an appropriate
tag referring to the filethat you just made. The only drawbacks to this approach are 1) you re introducing file writes, which may be time- consuming, into the page-generation process, and 2) you need to figure out what to do withthe files after you are done with them. There is one situation this approach is perfect for, however, which is creating and caching images that represent a finite set of possibilities. In this case, you have some way to map from a situation to an image filename. Whenever adisplay situation arises, you check to see if you already have the appropriate file if you do, you simply refer to it by using an
tag, and if not, you create the image, write it out to afile, and then refer to it. Eventually, you should need to do no more creation. You can see a page created this way at a just-for-fun site that we made a few years ago. Inwww.sciencebookguide.com/sizescales.html, there is a bar graph in the top part of thepage, and then a scale legend at the bottom, which was a gd-generated GIF. The text and tickmarks of the scale legend depend on the exact bar graph data that is being displayed, but thereare only a limited number of cases. So, long ago, we auto-cached all the possible images, andever since the displays have been static. (This is a good thing, of course, since gdquiterightly no longer supports the GIF format. Sometime soon, we may get around to replacingthe GIFs with PNGs.) Embedded images from scriptsFinally, there is no reason why you cannot have a standalone generated image, as in the sec- tion Full-page images, but, in turn, embed that URL in a different dynamic page via an
tag. The only difficulty lies in how to communicate necessary data to the dependent page. You may, for example, have an embedded image tag like this:
where ballpage.phphappened to return PNG images of colored balls in various positions inthe image. There is a gotcha lurking here because both Web servers and browsers sometimes pay atten- tion to the suffix of the served file, and in different ways. You may need the suffix of ballpageto be .phpto let Apache (for example) know that the server-side code should be interpretedas PHP (although this behavior can be controlled with configuration files). Some brokenbrowsers, however, may insist that a file that ends in .phpcannot be an image despite theheaders we are sending. This technique requires some cross-browser testing to make surethat your intended users are seeing the same thing you are. Now it s high time to move on to an example of using gdto create images.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.