Gossamer Forum
Home : General : Internet Technologies :

Display Image From Variable

Quote Reply
Display Image From Variable
I've done this before in the past, but have lost my notes and can't figure out what I am doing wrong.

I upload an image from a form and read that image into a variable:

Code:

$_handle = fopen($_FILES[tmp_name]));
fclose($_handle);

[/code]


To display the image, I am using:

[code]

if ($data[display_image]) {
$_form .= '<img src="' . $_file_content . '" width="200" alt="" border="1"><br />';
}

[/code]


Instead of displaying the image, it displays the character string. Can anyone help me out a little. NOTE: I must use an <img> tag instead of simply an echo or print command because I am building the page from a sub-routine. Any assistance would be GREATLY appreciated.
Quote Reply
Re: [Lee] Display Image From Variable In reply to
The image needs to be processed seperately from the HTML page you're displaying. set the value for the SRC attribute in the image tag to point a script that will in turn return an appropriate header and the image content. Also need to read the file in binary, not ASCII.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [Lee] Display Image From Variable In reply to
Is that PHP? It certainly looks like it ($_FILES, fopen, fclose). I know that code wouldn't work in PHP. You don't have enough parameters for fopen. In your open mode, just include b to indicate you are opening for binary reading. Also, you aren't reading the file (try using the file_get_contents function instead). Then there's the fact that <img src="some random binary data"> won't work. You have to base64 encode it or have "src" point to the image itself.

AFAIK base64 images are only supported by Mozilla browsers (Mozilla, Firefox, Netscape, etc.) and Internet Explorer 7, including recent betas/RCs (I don't know about Opera or khtml/webcore). This can be done using
Code:
<img src="data:MIME;base64,...">



where "MIME" is your image's MIME type and "..." is the base64 encoded image. For instance (this is from a quick search),
Code:
<img src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////
wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4ML
wWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="
alt="Base64 encoded image" width="150" height="150">

Last edited by:

mkp: Oct 13, 2006, 2:37 PM