sending image to client

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

nils
Forum Newbie
Posts: 5
Joined: Tue Dec 13, 2005 9:18 pm

sending image to client

Post by nils »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I'm a php novice.
I have a database of images.  How can I write a php script that can be included in the image tag like:

<img src="getImage.php?name='face.gif'">

so far I have managed this ...

Code: Select all

$imgName=$_GET['name'];
$conn = odbc_connect("websale", "root", "");
$query="select img_data from images where img_name='$imgName'";
$rs=odbc_exec($conn,$query);
odbc_binmode($rs, ODBC_BINMODE_RETURN);
$imgContent = odbc_result($rs, 1);
header('Content-type: ' . image_type_to_mime_type(IMAGETYPE_WBMP));
echo $imgContent; // < haha ?

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

That code looks fine, other then the fact you're storing your images in the database. try commenting out hte header line and visiting your script directly (not from an image tag), is there an error?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

<img src="getImage.php?name='face.gif'">
Why do you have single quotes around the image name?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

How to do similar thing with mysql. I tried this , but it didnt work
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

In oracle we can use

Code: Select all

$conn=orcaleconnect function();
$query="Select IMAGE from table"
$stmt = OCIParse($conn, $query);
OCIExecute($stmt, OCI_DEFAULT);	
while (OCIFetchInto($stmt, $row, OCI_ASSOC)) {
	echo $data = $row["IMAGE"]->load();
}	
OCIFreeStatement($stmt);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

dude81 wrote:How to do similar thing with mysql. I tried this , but it didnt work
Simple answer .. don't. A database is a crap place to store images. Store them in a directory and store the filenames in mysql .. that way you can just use something like <img src="$record['filename']">. Storing them in a database means you'll have an unnecessary connection to the database, and you'll have to feed the image through a PHP conduit .. all slowing down the simple operation of sending an image to the user.
nils
Forum Newbie
Posts: 5
Joined: Tue Dec 13, 2005 9:18 pm

Post by nils »

onion2k wrote:
dude81 wrote:How to do similar thing with mysql. I tried this , but it didnt work
Simple answer .. don't. A database is a crap place to store images. Store them in a directory and store the filenames in mysql .. that way you can just use something like <img src="$record['filename']">. Storing them in a database means you'll have an unnecessary connection to the database, and you'll have to feed the image through a PHP conduit .. all slowing down the simple operation of sending an image to the user.
Hmm, I'm really not sure about that, have you timed it. And, the benefit of a database is the isolation and the structuring that a database offers. Why should a persistent database connection be so much slower to dig up a pile of bytes than the filesystem ?. If we are talking split .01 secs here then the there is no reason to rule out the database.
In fact, I will solve the problem of streaming the image to the client, then I will time it and come back with the statistics. - Seeing is beleiving.

I intend to store my entire site in the database. Its going to store a lot of spreadsheets, word, rtf, txt, and application specific blobs, zipped, and I don't like the idea of solving that by a wide and deep directory structure. My old db mentor said to me "a system without a database is a toy" .... 8)

Anyhow, how can a php script be the image source of an img -tag ?, that is my question . I know it is possible. - its a piece of cake in java, but servlet is not an option in my current scenario.

Blob-scared opinions are useless in this respect.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nils wrote: Hmm, I'm really not sure about that, have you timed it. And, the benefit of a database is the isolation and the structuring that a database offers. Why should a persistent database connection be so much slower to dig up a pile of bytes than the filesystem ?. If we are talking split .01 secs here then the there is no reason to rule out the database.
In fact, I will solve the problem of streaming the image to the client, then I will time it and come back with the statistics. - Seeing is beleiving.

I intend to store my entire site in the database. Its going to store a lot of spreadsheets, word, rtf, txt, and application specific blobs, zipped, and I don't like the idea of solving that by a wide and deep directory structure. My old db mentor said to me "a system without a database is a toy" .... 8)

Anyhow, how can a php script be the image source of an img -tag ?, that is my question . I know it is possible. - its a piece of cake in java, but servlet is not an option in my current scenario.

Blob-scared opinions are useless in this respect.
Why don't you try running the benchmarks on normal loads, and again on stressed loads :wink:
nils
Forum Newbie
Posts: 5
Joined: Tue Dec 13, 2005 9:18 pm

Post by nils »

Ok, I'll do that, then I'll implement a persistent dynamic cahce and give it a jolly up and down ride. After that we'll compare code size and mantainability. But,.. I will need a short week to get back on this issue as I am in the middle of the development/delivery/95% -ready process.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

nils wrote:Hmm, I'm really not sure about that, have you timed it. And, the benefit of a database is the isolation and the structuring that a database offers. Why should a persistent database connection be so much slower to dig up a pile of bytes than the filesystem ?
Why should it be slower?

Retrieving an image stored in a database:

1. Http request - A request comes in for an image
2. Apache - Process the request
3. PHP - Parse script, process script, request data from database
4. Database - Process request, send data back to PHP
5. PHP - Send data to output buffer
6. Apache - Send output to user

Retrieving an image stored in the filesytem:

1. Http request - A request comes in for an image
2. Apache - Process the request
3. Apache - Send file data to user

Don't get me wrong .. I definitely see the benefit of using a script to process an image (that's sort of my speciality within PHP) .. but if you're not changing the data at all then I see no point in pushing it through a script. It just slows it down. You might think 0.01s is unimportant .. firstly I think that's unrealistically low, and secondly, why spend 0.01s on something unnecessarily?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

nils wrote:Hmm, I'm really not sure about that, have you timed it. And, the benefit of a database is the isolation and the structuring that a database offers.
Every test I have run on various databases storing binary image data and storing images in a directory indicates using a database to store images is many times slower than pulling the information from a directory. You place a much larger load on the database server and php server if you store binary data in the database compared to just storing the image name and directory path to the image in the database.

Also, persistent database connections are usually the worst thing a person can use if the site is very popular because they tend to use vastly more memory than non-persistent connection. And with most servers you will NOT notice any speed increase or load decrease in the database server if you are using persistent connections. In many cases the server load goes up with persistent connections as the server tends to start using swap space unless you have gigabytes of memory on an active database server.

And before anyone starts saying otherwise I have performed hundreds of tests on the most popular databases and in every case the only time persistent database connections were a benifit is on very low traffic sites connecting to a database server running on a 400mhz machine.

But back to storing images in a database...

Retrieving binary data from a database is about the slowest method you could use and far more CPU intensive compared to retrieving the same data from a stored file. There is far less overhead involved.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nils wrote:Ok, I'll do that, then I'll implement a persistent dynamic cahce and give it a jolly up and down ride. After that we'll compare code size and mantainability. But,.. I will need a short week to get back on this issue as I am in the middle of the development/delivery/95% -ready process.
Challenge Accepted. :)
nils
Forum Newbie
Posts: 5
Joined: Tue Dec 13, 2005 9:18 pm

Post by nils »

..the cache will be the entire database of course, and, I have not said anything about a database with zillions of images, a few hundred at the most. And documents and spredsheets will all be zipped. I bet nobody can deny the fact that we, in a few years, will think of physical disc directories as we think of vinyl today. I bet even indexing will be obsolete as we have data storage that finds anything in a flash. :o

But, it would be nice if anyone could explain to me how I do that special trick, like, the initial question I asked :? ?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

nils wrote:..the cache will be the entire database of course, and, I have not said anything about a database with zillions of images, a few hundred at the most. And documents and spredsheets will all be zipped. I bet nobody can deny the fact that we, in a few years, will think of physical disc directories as we think of vinyl today. I bet even indexing will be obsolete as we have data storage that finds anything in a flash. :o
The number of files, and their size is irrelevant. You're sending the same data to the user whatever the storage medium.

As for the the way data is stored in "physical disc directories" .. what exactly do you think the difference between a database and a filesystem is? The both store the data as a stream of bytes on a disc. The way they differ is how you access that information. If you're storing files in a database the retrieval method (php -> db -> php) is not optimised to return large amounts of binary information, hence it's relatively slow. Just passing the data from the database to PHP will be a bottleneck. Using the filesystem eliminates that bottleneck because Apache is very good at getting data from the filesystem and sending it to the user.

I think the problem here is that you've designed your system and you're unwilling to accept that it's less than optimal because it'd be some work to change it. Fair enough. Just stop suggesting that it's better to store images in a database. It isn't.

Getting back on topic .. as the second poster in the thread pointed out: your code looks fine. There's no trick to sending images to the user .. so long as you've sent the right content-type header and the image data is ok then it should work fine.
nils
Forum Newbie
Posts: 5
Joined: Tue Dec 13, 2005 9:18 pm

Post by nils »

With sybase you can specify that the entire database resides in memory. That makes a difference. And, what I said was "I'm really not sure about that", and that I am going to give it a go, just to get the facts. You have not yet succeeded in convincing me, "it isn't" isn't enough. So Ill go ahead and convince myself, and I will be back with the statistics. Ok ?

Ah, one more thing. I will use java servlets. What kind of language this method is implemeted in hasn't been a topic in this discussion.
Post Reply