Dealing with Images
Another PHP function lets you link to images as easily as pages, without having to care much about the exact file location. This is particularly helpful with templates, so you can easily inherit the look and feel of a parent directory, or override logos and graphics without changing the actual page layout. And as an added bonus, you get automatic width and height attributes, and even an automatic alt attribute if you forget to provide one.
So, rather than measuring the image size manually and writing a long HTML tag such as:
width="400" height="100" alt="Logo" border="0" />
You can write this shorter version:
<? image("logo.jpg"); ?>
And in many cases, you won't need to write any code at all. Simply drop a file into the gfx/
subdirectory, and the image should show up on your page automatically. For example, if your site uses /gfx/logo.jpg
for its logo, you could apply a different logo to the bears section by creating /bears/gfx/logo.jpg
. Doing that would override the regular logo, and thus show up automatically without writing any special code.
It's also very common to use "thumbnail" imagessmall images that link to larger or full-sized images. Normally, using thumbnails is a fairly tedious task; you have to load an image in a photo-editing program, resize it, save it, put both copies onto your site, and write HTML code such as the following:
<img src="/path/to/small.jpg" width="200"
alt="click for bigger image" border="0" />
A small PHP function similar to image()
makes this process much easier. Simply put the original image on your site, and call a function:
<? thumbnail("big.jpg"); ?>
Doing this has the same effect as the longer method above, except that the Web server will create the thumbnail image automatically the first time it's requested. You can optionally pass numeric values to specify a maximum width or height for the thumbnail image.
The files inherit.inc
in the sample code
implement the image()
functions, respectively. To use thumbnail()
, make sure to give your Web server permission to write to the gfx/
directory; the server won't create thumbnail images without write permission. The thumbnail()
function also requires the free "convert" utility from ImageMagick