method to use to Display JPEG image from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

method to use to Display JPEG image from database

Post by batcloud »

I am having difficulty in understanding the method I need to use to display an image that resides in a database.
The image file was inserted into MySQL database as a BLOB, and is about 5kb in size.
I am able to connect, select and fetch the file, but then how do I display the image? There are so many functions like:

imagecreatefromjpeg();
imagejpeg();
imagecreatefromstring();

I am not sure in what format the file comes out of the database (it was jpeg going in), and whether it needs to be reconstructed.
Also, will the file be stored in a variable (not the filename, but the contents of the file) until used.
Is there a temporary place on the server (or my website) where it is stored?
And finally, how do I display the image on the screen without first storing it in some folder as a file with filename.

Would really appreciate any help on this one. I have been reading the documentation and sample code on php net and other places. The more I read, the more confusing it gets
:cry:
Thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If it was a JPEG going in then it's a JPEG coming out. You need to write a script that simply outputs the content type header and then the database content. Sort of like:

Code: Select all

require_once "databaseConnectionStuff.php";

$sql = "SELECT `myImage`.`imageBlob` FROM `myIMage` WHERE `myImage`.`image_id` = 1";
$result = mysql_query($sql,$connection);
if (mysql_num_rows($result)==1) {
  $record = mysql_fetch_object($result);
  header("Content-type: image/jpeg");
  echo $record->imageBlob;
} else {
  header("Content-type: image/jpeg");
  echo file_get_contents("missingImage.jpg");
}
Then you'd save that as myImage.php, and in your HTML code you'd put <img src="myImage.php"> to display image 1 from the database.

(That code is untested by the way, it's just to give you an idea)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Something to be aware of, under normal circumstances it is recommended to store files in the file system, not a database. Memory and time are the major concerns.
suyeic
Forum Newbie
Posts: 6
Joined: Wed Jun 13, 2007 9:22 pm
Location: Bejing China

Post by suyeic »

Both of the methods are ok. If we save it in database directly, for the different size of file, it should waste most of resource of database. But we needn't do other work, because it save there, anybody without auth cannot destory it. For file, maybe we save this file's path in the database, we should maintain both path and database, it need more time on it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Maybe you misunderstood.

The memory we are trying to save is the server's active requirements. Many concurrent users requesting images from the database will require the database loading all those images into its memory, then php loading them in its memory, possibly even the web server loading them in the output buffer.

The time we are trying to save is execution time for the server to return data. Each image stored in the database requires a separate request and separate query.
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

Post by batcloud »

Thanks everyone for your answers.
I am sorry to reply so late (DSL down, busy with other projects etc). I will be following the suggestions and try it out tomorrow.
Will post results.
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

solved

Post by batcloud »

Onion2k:
Thanks very much. Your directions worked. Your code outline is right. One thing that I got stuck at was the line:
echo $record->imageBlob;
It took me a while to realize that what we are echoing belongs to the class imageBlob. I used the name of the column of the database as the class and it worked. Please correct me if this is not the appropriate way to do it.
Thanks everyone!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: solved

Post by onion2k »

batcloud wrote:Onion2k:
Thanks very much. Your directions worked. Your code outline is right. One thing that I got stuck at was the line:
echo $record->imageBlob;
It took me a while to realize that what we are echoing belongs to the class imageBlob. I used the name of the column of the database as the class and it worked. Please correct me if this is not the appropriate way to do it.
Thanks everyone!
The class is actually a MySQL row class that's internal to PHP. But yeah, imageBlob refers to the database column name... so you just had to change that. Glad it works.
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

Additional requirement: store image in client-side temp file

Post by batcloud »

Well, my needs never end!

I now want display this image in numerous pages that the user visits. I would like to avoid the user-specific image to be downloaded from the database for every page load or refresh. After this, for every page refresh, I would prefer the image to be loaded from the cache. My questions are:

1) Is the image stored as a temp file somewhere on the User's system (I mean client-side)?
2) If (1) above not true, Can I force the file to be stored somewhere on the User's system?
3) Can I access this image file on every page refresh (or loading a new page within the site)?
4) Can I use cookies to control the above behavior, for ex. by setting a user-image specific cookie?

Suggestions would be appreciated...BTW, I am a php newb, but am willing to learn...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You'll want to have the image generated only when necessary and stored in your file system (not client-side). The database would store the filename.
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

Post by batcloud »

Thanks superdezign...
but then I went the database route just to avoid having to store the file on the server, where (please correct me on this if I am wrong) it is accesable to everyone.
BTW, file size is around 5k or less (100 by 80 pixels), so hopefully not a great load on the server. We recently discussed the other issues regarding filename in database versus image itself into database, thanks to feyd, suyeic, onion2k. I then decided to go the database way, mainly because of the reduced filesize.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

One of the things that comes with doing it through the database is that you'll have that extra load. That's why it's debatable which one to use. I think you'll just have to deal with the extra database interaction. I don't think you can cache the database. :P
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

batcloud wrote:Thanks superdezign...but then I went the database route just to avoid having to store the file on the server, where (please correct me on this if I am wrong) it is accesable to everyone.
BTW, file size is around 5k or less (100 by 80 pixels), so hopefully not a great load on the server. We recently discussed the other issues regarding filename in database versus image itself into database, thanks to feyd, suyeic, onion2k. I then decided to go the database way, mainly because of the reduced filesize.
There are 3 points raised here:

1. Storing a file on the server is only accessible to everyone if you make it accessible to everyone. There are many routes to blocking people hotlinking to it.

2. Getting 5k from a database is a lot more than most scripts do, and a lot more work than fetching a static file. The database has to load it into memory, then send it to PHP, then PHP has it in memory, then PHP sends it to the web server which may also buffer it in memory prior to sending it to the user. That means you'll have 3 copies of it in memory at once. If you fetched it as a static file (eg <img src="file.jpg">) then the web server could stream it straight from the disk to the browser ... it would never be held in memory.

3. You're sending a 5k file.. it's always going to be a 5k file regardless of where you put it.
batcloud
Forum Newbie
Posts: 15
Joined: Sun Jun 10, 2007 3:04 pm

Post by batcloud »

onion2k wrote: 1. Storing a file on the server is only accessible to everyone if you make it accessible to everyone. There are many routes to blocking people hotlinking to it.
I didn't have hotlinking in mind, just privacy issues (this is a family oriented site). One of the ways it (hotlinking) can be prevented in through the c-panel- not sure though of other methods.
What I meant was more along the lines of someone browsing the folder, and if that is blocked, guessing filenames and loading it into their browser. Can the latter be prevented, i.e. only the owners of the file can have access to the file? This would make things easy.
Any thoughts?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Files on the server can be protected so that only their owners (and any root level user) can view them. Don't ask me how though, I'm no sysadmin.
Post Reply