Jump to content

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*

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.