Creating the img tag
First, we need a function that, when provided with an image name, does this:
- looks up the image information in the database
- calculates the height and width of the image after resizing
- builds the URL to the image retrieval script
- combines these three parts in a nice img tag for use in our html.
The function
In imgtag.php, we start with defining the name of the table containing the image info and connecting to the database. Then, we open a function called img_tag, that will return the img tag.
// define database table containing image info
$img_table = "images";
// connect with database
mysql_connect("yourhost", "youruser", "password");
mysql_select_db("yourdb");
// generates and returns an img-tag
function img_tag($img_file, $attrib) {
global $img_table;
To create an img tag, our function needs the following arguments passed:
- $img_file: the 13-character filename of the image, as saved in the database and in the filesystem
- $attrib: an array describing how we want the image to be. This associative array consists of the same one-letter keys we used in the resize script.