jeffrydell Posted October 12, 2007 Share Posted October 12, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/72992-text-image-based-test-system/ Share on other sites More sharing options...
R_P Posted October 18, 2007 Share Posted October 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72992-text-image-based-test-system/#findComment-372515 Share on other sites More sharing options...
jeffrydell Posted May 20, 2008 Author Share Posted May 20, 2008 Thanks - I ended up using a class that not only retrieves the file (not blob in a table), but it also 'thumbnails' anything wider than 600px down to 600px. Very handy. I appreciate the answer. Jeff Quote Link to comment https://forums.phpfreaks.com/topic/72992-text-image-based-test-system/#findComment-546110 Share on other sites More sharing options...
roopurt18 Posted May 21, 2008 Share Posted May 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/72992-text-image-based-test-system/#findComment-546653 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.