images in php

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
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

images in php

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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=&quote;getimagefromDB.php?img=564&quote;>
I'm sure somebody will say.... err... d11... that's rubbish.. it wont work, but test it until then. :lol:
husniteja
Forum Newbie
Posts: 10
Joined: Fri Feb 25, 2005 8:33 pm
Location: south korea

Post by husniteja »

dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

that sound pluasible. ill give it a try and ill let you know how i get on, thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

we've talked about the pro's and con's of storing binary data (specifically images) in mysql previously.. many times.
brad7451
Forum Newbie
Posts: 4
Joined: Sat Mar 12, 2005 1:03 am

Can be done but why do it in the first place

Post 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

and

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

brad7451, read your private messages.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply