Jump to content

Text & Image based test system


jeffrydell

Recommended Posts

I'm putting together an online test application which can incorporate images in both the questions and the answers.  I have to make choice of storing the images in a directory on the server and storing a link in one of the table fields, or storing the images as blobs within an image table in the db.

 

Since the images are small (nothing over 50 - 75k), I figured stashing them in an image table with mime type and a unique name would be the best plan.  Upload & store in the db are no problem ... but I'm having a bit of trouble when it comes to rendering these images on the screen after I retrieve them.

 

Before I get yelled at for posting in the wrong place, please read on - this is NOT a coding question!

 

I have been able to retrieve a single image from the db, sent the header info & it came up no problem. I'm not, however, doing very well with displaying multiple images or a combination of text & images.  Echo commands after the image don't put anything up on the screen.

 

Before I wail on this too much longer, is it really wise to store images as Blobs in a table when dozens of them will need to be retrieved for any one test, or should I just store paths and let html tags do all the work when it comes time to throw a test up on the screen?

Link to comment
Share on other sites

Once you send a header specifying an image MIME type to the browser, the only thing that the browser can display is an image! I'm surprised you weren't getting all sorts of errors by doing that. One thing you can consider is creating a seperate php file which returns only images and using it as the source in an HTML image tag like so:

 

<img src="img.php?index=5" alt="">

 

I generally don't store images in my db tables (although its perfectly acceptable) but using the example above should allow you to do so.

 

Good luck.

Link to comment
Share on other sites

  • 7 months later...

It would be better to just store the images on file system, named after their unique auto-incrementing `id` column in the database.  Then all you need to store in the DB is the file extension so that your PHP code append it when outputting images.

 

images

id, ext

1, jpg

2, jpg

3, jpeg

4, gif

5, png

 

On your file system you'd have:

1.jpg, 2.jpg, 3.jpeg, 4.gif, 5.png in a predetermined directory.

 

When you select the images:

SELECT CONCAT(`id`, '.', `ext`) AS `name` FROM `images` WHERE ...

 

Then it's as simple as construction an img tag:

echo sprintf('<img src="/the/path/%s" alt="An Image" />', $row['name']);

 

Granted there are some pieces missing here, like the fact that images are attached to specific questions and have a placement within that question.  But you can fill those in.

 

As for the why, consider this.  If the images are stored in the DB you have to create a special script that takes the image id, grabs the image from the DB, sets up the header, and outputs the image contents.  So each of your images refers to a PHP script, which requires extra processing on the server just to open the file and feed it into the PHP interpreter.

 

In addition, this script has to create a connection to the database, thus eating up your database connections.  So if your single script that displays a question has 5 images embedded in it, you get 5 more connections to your database.

 

Then you have to query the database for the image.  If the image exists, the DB will return the contents over the MySQL connection; even if the smallest image is only 50kb, that's still 50 thousand characters, which is a substantial amount of data.

 

So you perform all of this when the original script could just pull the file locations out of the DB and have apache handle the delivery of the files for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.