Link to home
Start Free TrialLog in
Avatar of sscotti
sscottiFlag for United States of America

asked on

Creating thumbnails for Image Large Blob data and storing thumb in mySQL database

I have a database that has farily large images stored as large blobs in the database.

The code that stores the image after it is submitted via POST is like this:

if ($_REQUEST['completed'] == 1) {
        // Need to add - check for large upload. Otherwise the code
        // will just duplicate old file ;-)
        // ALSO - note that latest.img must be public write and in a
        // live appliaction should be in another (safe!) directory.
        move_uploaded_file($_FILES['cellframeimageData']['tmp_name'],'latest.img');
        $instr = fopen("latest.img","rb");
        $image = addslashes(fread($instr,filesize("latest.img")));
            $imageSize = strlen($image);
        if (strlen($image) < 5000000) {
                mysql_query("UPDATE images SET cellframeimageData ='".$image."' WHERE cellId ='".$_REQUEST['cellId']."' AND cellframeOrder ='".$_REQUEST['cellframeOrder']."'",getdbHandle());
        } else {
                $errmsg = "Too large!";
        }

For displaying images I am currently using something like this:

<img src='imageGetter.php?cellId=1&cellframeOrder=0' width=200

where the target page grabs the blob data from the database and spits it back.  The base code for that is like this:

$query_image="select cellframeimageData from images where cellId = '".getUrlStringValue('cellId',null)."' and cellframeOrder = '".getUrlStringValue('cellframeOrder',null)."'";
$res=mysql_query($query_image,getdbHandle());
$row = mysql_fetch_assoc($res);
extract($row);
if (isset($cellframeimageData)) {
header("Content-type: image/jpeg");
echo $cellframeimageData;
}

All of that is working as far as storing the images in the database and also for displaying them, although I do have a couple of questions.

1.  It would be nice if it were possible to bypass the call to the imageGetter.php page and somehow diplay the image inline by directly pulling it from the database rather than having it reference the external page.  I guess there isn't a convenient way to do this as you would have to somehow embed the image source in the page rather than in a external file on the server of via the on-the-fly method using the external php page?

2.  I also want to be able to create some thumbnail images from the larger blob images above and store them in the database as well.  Again, if it were possible to pull and display the thumbs from the database without having to use the external php page that would be nice.

3.  I noticed that there is alot of discussion about the merits of storing images or large files in a database rather than in files on the server.  I am kind of playing with that to see how the performance is and to see what I would prefer, although I like the idea of having a self-contained file that stores the whole set of data rather than having the images on the server, but I guess you can take a pretty big performance hit if the db gets large.  In my app, I would probably display the thumbs most of the time and only retrieve the larger images as requested.  I was hoping that that would perform fairly well for small images.
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

hello sscotti, , I have seen in too many database info web pages, that always say something like - "never store images in a database if you can avoid it!". There not only the large size of the data (upto 5000000 in your case, 100 images might be 500 megabytes), but more importantly, for the amount of data through put and access time that the database engine has to do. Every database through put affects all other database operations and through put at that time, as in slowing down some.

as for thumbnails, when the image  files are uploaded, there is a temp file that you can create thumbnails from, as latest.img in your code-
$instr = fopen("latest.img","rb");

for "bypass the call to the imageGetter.php" I have not seen any way to get an image froim a database, only from a php access

Avatar of sscotti

ASKER

Thanks for that.  I probably would limit the size of the images to less than 1 MB, and the thumbnails would be quite small.  Most of the comments that I've seen do suggest storing the images on the file server rather than in the database, although it is nice to have them in one file, the database.  The web app actually would usually display the thumbnails only, and the larger images would only be display one at a time on demand.  Still seems like there should be a way to create an image directly from the database rather than having to use the imageGetter php file.

Do you actually have code for creating the thumbnails.  I am not sure how to do that.  I do have the ?GD library installed.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry
it says "only JPG and GIF images"
this is NOT true, it can also do PNG images as well
also
you must change the
    action="doup1.php"
in the form to your php page