The example I am going to use stores screen-shots (.jpeg) of websites. For instance the screen-shot of www.facebook.com looks like this:
The database, in my example, will also hold a name and the URL for the screen-shot. The example will neatly show all the images on a page and a clickable link to the URL of the image.
So, here is the to-do list:
- 1
Create the MySQL Database
Create a MySQL database with a table called "tblBlob" that has the following 4 fields:
- blob_id
This needs to be type Integer and set to AUTO_INCREMENT
- blob_name
Type VARCHAR
- blob_url
Type VARCHAR
- blob_binary
You can set the type to either of the following: BLOB, TINYBLOB, MEDIUMBLOB, OR LONGBLOB. The differences between these are the maximum allowed sizes of of the images you are going to store. A BLOB is a binary field that can store the binary representation of images. For this example I am going to use LONGBLOB.
Here is the MySQL syntax for creating this table:
- 2
Create a PHP script for retrieving and re-sizing the image
Call this script "doImage.php". What's going to happen is, from the main page (which we'll create next) where we will show the images and links, we will set the 'src' inside the image tags to the following:
Notice the two request-variables inside the url: blob_id=1 and resize=225,225. The first one, blob_id=1, retrieves the blob in the database whose blob_id is 1. So if you wanted the 15th blob in the database, you would change that 1 to a 15. (We will dynamically set this variable -- I only went into this for explaining purposes) The second variable, resize=225,225, will size the image to 225 x 225 (h x w). It won't change the blob in the database, it is only for viewing purposes. If you want to just display the original image's size, you can leave this out completely which would give you the following url:
The contents of doImage.php need to look like this:
This script takes the first variable (blob_id) and retrieves that blob from the database. If there is a resize variable, the script will re-size the retrieved image.
This is all we need to do with this script. It is as easy as that! Lastly we need to create the main page that will display all the images with their links.
- 3
Create Main Page
Create a php webpage and name it whatever you want.
Inside the body of your php page, insert the following code that grabs the ids, names, and urls from your database, then sets the image sources to the respective url I discussed in the last step:
This will show all the files in your database in a thumbnail-size of (280 x 215) with the Name of the image as a link right above each image.
- 4
Load Sample Data
Put some sample data in your database. I took screen-shots of 6 different websites and placed them into my database. Here is a screen shot of the contents of my database:
- 5
Fix Connection Strings and Place Both Files In Same Directory
Go into both files and fix the MySQL connection strings to connect to your database.
Now place both files in the same directory and navigate to the main page that you created. Here is what my example looks like: