Page 1 of 1
images in php
Posted: Fri Mar 11, 2005 6:43 pm
by dannymc1983
does anyone know of a way of displaying images that are stored in a mysql database in a web browser without having to download them to a local folder?
Posted: Fri Mar 11, 2005 6:51 pm
by Chris Corbyn
Never done it, nor researched it or learnt it but at a guess.
Just create a PHP file which get's the data from the DB and outputs it and nothing else (so in a sense, the php file is the image itself). (i.e. echo()'s it along with noting else).
Then to make it display. Use in your page with the image on
Code: Select all
<img src="e;getimagefromDB.php?img=564"e;>
I'm sure somebody will say.... err... d11... that's rubbish.. it wont work, but test it until then.

Posted: Fri Mar 11, 2005 6:52 pm
by husniteja
Posted: Fri Mar 11, 2005 6:53 pm
by dannymc1983
that sound pluasible. ill give it a try and ill let you know how i get on, thanks
Posted: Fri Mar 11, 2005 7:05 pm
by feyd
we've talked about the pro's and con's of storing binary data (specifically images) in mysql previously.. many times.
Can be done but why do it in the first place
Posted: Sun Mar 13, 2005 4:02 pm
by brad7451
I have tried this and succeeded but eventually realised that it was just too much work in the end.
What you need to do is create a script lets call it 'displayimage.php' that sends the binary data to the browser but what you will need to do is first send a header in the form
Header("Content-type: image/pjpeg");
or
Header("Content-type: image/gif");
or
Header("Content-type: image/x-png");
Here you will put the code to extract the binary source from the database and store it into the variable named $img_source. Then
then you need to create the image using the built in gd functions like this
// Create the image from the image stream string
$img = imagecreatefromstring($img_source);
then we output the image to the browser
// Display the image
echo imagejpeg($new_img);
or
// Display the image
echo imagegif($new_img);
or
// Display the image
echo imagepng($new_img);
This script 'displayimage.php' might be called from another script like this <a href="displayimage.php?imageid=23">. Where 'imageid=23' is the id in the database of the image you want to display.
Code: Select all
<?php
Header("Content-type: image/pjpeg");
//or
Header("Content-type: image/gif");
//or
Header("Content-type: image/x-png");
/*
Extract the binary source from the database
and place it into the variable $img_source
*/
// Create the image from the image stream string
$img = imagecreatefromstring($img_source);
// Display the image
echo imagejpeg($new_img); //or
echo imagegif($new_img); //or
echo imagepng($new_img);
?>
Hope this helps.
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Sun Mar 13, 2005 4:12 pm
by feyd
brad7451, read your private messages.
Posted: Sun Mar 13, 2005 4:17 pm
by Ambush Commander
Generally, it's faster to just store files on the server. It's not that difficult to create an export script that dumps the images in a directory. Thumbnails for example, should be created and then saved to prevent overhead.