images in php
Moderator: General Moderators
-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
images in php
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?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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
I'm sure somebody will say.... err... d11... that's rubbish.. it wont work, but test it until then. 
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;>-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
Can be done but why do it in the first place
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.
Hope this helps.
feyd | Please use
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);
?>feyd | Please use
Code: Select all
andCode: 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]- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US