sscotti
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 ['cellfram eimageData ']['tmp_na me'],'late st.img');
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,fi lesize("la test.img") ));
$imageSize = strlen($image);
if (strlen($image) < 5000000) {
mysql_query("UPDATE images SET cellframeimageData ='".$image."' WHERE cellId ='".$_REQUEST['cellId']."' AND cellframeOrder ='".$_REQUEST['cellframeOr der']."'", getdbHandl e());
} else {
$errmsg = "Too large!";
}
For displaying images I am currently using something like this:
<img src='imageGetter.php?cellI d=1&cellfr ameOrder=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('cell Id',null). "' and cellframeOrder = '".getUrlStringValue('cell frameOrder ',null)."' ";
$res=mysql_query($query_im age,getdbH andle());
$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.
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
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,fi
$imageSize = strlen($image);
if (strlen($image) < 5000000) {
mysql_query("UPDATE images SET cellframeimageData ='".$image."' WHERE cellId ='".$_REQUEST['cellId']."'
} else {
$errmsg = "Too large!";
}
For displaying images I am currently using something like this:
<img src='imageGetter.php?cellI
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('cell
$res=mysql_query($query_im
$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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry
it says "only JPG and GIF images"
this is NOT true, it can also do PNG images as well
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
you must change the
action="doup1.php"
in the form to your php page
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