0

The code below:

if($file = $mongoGridFS->findOne(array('_id' => new MongoId($fileId)))) 
{
    $fileName = $file->file['filename'];
    $fileBytes = $file->getBytes();
    header('Content-type: image/jpeg');
    header("Content-Length: " . strlen($fileBytes));
    ob_clean();
    echo $fileBytes;
}

Returns the Response from Mongo as .jpg file.

How to include to this response some html? eg.

<div id='container'>
  <img ... /> <!-- image generated by script and stored in $fileBytes -->
  <br>
  <span class='description'>This is image</span>
</div>

I want not to make <img src='...'> linked to saved file on filesystem.

Is this possible?

123qwe
  • 1,525
  • 1
  • 14
  • 16
  • Do you want to respond with a jpg or html? You can't have both. If you want html, return HTML and link to the image in question in that html. – Dan Smith Apr 01 '15 at 12:53
  • I saw somewhere that somebody made an image from string and then he placed this image to html without linking. But I can not find that way now. – 123qwe Apr 01 '15 at 12:56

1 Answers1

1

You cannot return two resources within the same http response. What you can do is to inline the image data (see Embedding Base64 Images for more details like browser compatibility and other possible issues with inline images). Something like:

<img src="data:image/jpeg;base64,<?=base64_encode($fileBytes)?>" />
Community
  • 1
  • 1
Cristik
  • 30,989
  • 25
  • 91
  • 127