-1

I have images stored in a secure location which I want to display in html.

I cannot display them by linking to the source like: <img src="secure_path/image.jpg"/>

What I want to do is copy the image data with PHP and display it like:

<img src="data:image/jpeg;base64,R0lGODdhMAAwAPAAAAAAAP
///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQ
u5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRN
zOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7J
pJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZe
YGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk
0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uN
na3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7" />'`

How can I copy this data from an image file with PHP?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tim Baas
  • 6,035
  • 5
  • 45
  • 72
  • 1
    read_file, base64encode? – kirilloid May 09 '14 at 13:58
  • @PavelŠtěrba It's kind of hard to find an answer if you don't know you need to search for `convert image to base64 encoding`. Therefor I think this question is different from the one you're linking to. Even if the answer may be the same. – Tim Baas May 09 '14 at 14:15

1 Answers1

1

Try:

function getDataURI($image, $mime = '') {
    return 'data: '.(function_exists('mime_content_type') ? mime_content_type($image) : $mime).';base64,'.base64_encode(file_get_contents($image));
}
$s=getDataURI($src);
echo '<img src="'.$s.'">';
laurent
  • 418
  • 3
  • 7