3

I have some data stored in a MySQL database .. i would like to show the stored image data along with other data in a .php page..

if i fetch data from the database and use header("Content-type: image/jpeg"); its not possible to show the image with other php data.. is there a a other way ?

Sudantha
  • 15,684
  • 43
  • 105
  • 161

2 Answers2

4

If you set the header to image/jpeg that treats your entire page as an image file.. You want the data to be insert into the image holder only.

Try something like this

<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

Where you will next echo the blob data into the image src

 <img alt="Embedded Image" src="data:image/png;base64,<?php echo $image->blob_data; ?> "/>
Atticus
  • 6,585
  • 10
  • 35
  • 57
  • Interesting approach. I'll have to remember this one. – Matthew Scharley May 09 '11 at 03:45
  • Matt -- how else would you? Not being condescending, rather I didn't know there was another approach? What else can be done here if he's streaming data into an image dynamically? – Atticus May 09 '11 at 03:47
  • 1
    @Atticus The way I would have approached the problem is as recommended in the comments in the question, create a different script that returns the image, then use a `src="get_picture.php?id=blah"` to fetch the image into the page. Wasn't aware of data URI's before just now. – Matthew Scharley May 09 '11 at 03:58
  • Oh that'd be a good approach too -- so get_picture.php?id=blah would echo out the data and set its header to an image file.. so that'd work, probably much cleaner that way too.. +1 :) – Atticus May 09 '11 at 04:01
  • @Atticus: No. As long as he's only returning the image data and not a HTML page as well, PHP can return a correct `Content-Type` header for the image data and the browser won't care one bit whether it's generated by PHP or retrieved from the file system. The same sort of tricks are used to generate dynamic images via eg. GD. – Matthew Scharley May 09 '11 at 04:01
  • @Atticus a simple test is the following: `header('Content-Type: image/png'); echo file_get_contents('some.png');` – Matthew Scharley May 09 '11 at 04:03
  • Yeah no i get that, i use that for a thumbnail generator – Atticus May 09 '11 at 04:03
  • @Atticus Sorry, I misread your comment. As a sidenote, if all you have is data that's already base64'd or similar, you can get away with some weird stuff like `Content-Transfer-Encoding: base64`, but I'd recommend you just unencode it on the serverside. – Matthew Scharley May 09 '11 at 04:08
  • Yeah I'd normally go with this approach too, i wasn't thinking outside the box when i was watching OP directly put his data into the image -- I actually never have done it this way, just know it's possible. Even my latest project I've done it the way you're describing. I might be a little high right now... ;) – Atticus May 09 '11 at 04:10
  • @Atticus for pages that aren't reloading (ie, AJAX heavy pages), this actually does kind of make some sort of minor sense. Would totally get around the issue in this question: http://stackoverflow.com/questions/5877568/how-to-go-about-writing-a-javascript-pre-loader (because the page wouldn't load till all the images had). Of course, that doesn't give the user any feedback other than a very slow page though... – Matthew Scharley May 09 '11 at 04:15
  • I didn't realize you could do this. Interesting – Joe Phillips May 09 '11 at 04:17
2

It depends how you stored you data, but sometimes you have to convert the data to base64. Try this

echo '<img src="data:image/png;base64,' . base64_encode($blob_data) . '"/>
Murolack
  • 1,897
  • 1
  • 14
  • 17