Jump to content

Display image if I have it as a string?


CharlesCA

Recommended Posts

How do I display an image if I have the jpeg image as a string? 

 

Ideally I just want to use the img src tag so I can have other output on the webpage, without having to write the string of a file or creating a new page for just the image. 

 

I have tried the following (and they work):

• Write the string to a file and use img src tag

• Set the content type to image/jpeg and write the string to a new page with nothing else on it

 

 

Link to comment
https://forums.phpfreaks.com/topic/162835-display-image-if-i-have-it-as-a-string/
Share on other sites

you need to open it in a file with the correct header

ie

<?php
$data = file_get_contents("myImage.jpg"); //get the images data (from database or whatever)
header('Content-Type: image/jpeg'); //Set header
echo $data;
?>

 

Okay when you open that file it will display the image!

 

EDIT: this should be it OWN file..

to display it in a html page just call the php script above as if it was the image

 

heres another example

myimage.php

<?php
$data = file_get_contents($_GET['file']); //get the images data (from database or whatever)
header('Content-Type: image/jpeg'); //Set header
echo $data;
?>

 

in the HTML i would do this

<img src="myimage.php?file=theImage.jpg">

 

*these are just for an example*

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.