简体   繁体   English

显示存储在数据库中的图像

[英]display image stored in database

I have images that are base64 encoded as blobs in my sql database. 我的sql数据库中有base64编码为Blob的图像。

using a php script I can properly display the picture in my browser using. 使用php脚本,我可以使用浏览器正确显示图片。

echo '<img src="data:image/jpeg;base64,' . $fetch_row['object']. '" />';

however, I would like to create a view that would display the photo directly or by accessing the php file and returning the picture in a column. 但是,我想创建一个视图,该视图将直接显示照片或通过访问php文件并在列中返回图片。 I have tried using something like: in mysql 我试过使用类似的东西:在mysql中

concat('<img src="data:image/jpeg;base64,',`object`,'" />')
or concat('<img src=/image.php? blah blah blah)

any ideas? 有任何想法吗?

<?php

header('Content-type: image/jpeg');
echo base64_decode($fetch_row['object']);

Your question also exactly typifies why storing images in the database is a BAD idea. 您的问题也正好代表了为什么将图像存储在数据库中是一个坏主意。 If the image was just a file on your server's drive, you could just have 如果映像只是服务器驱动器上的文件,则可能只有

<img src="/path/to/file.jpg" />

and not have to involve the DB/PHP layer at all. 完全不必涉及DB / PHP层。 As it stands now, your data-uri version prevents the browser from caching that image at all, and you'll be forcing the user to download that image EVERY time, burning up their (and your) bandwidth. 就目前而言,您的data-uri版本完全阻止浏览器缓存该图像,并且您将迫使用户每次都下载该图像,从而耗尽他们(和您)的带宽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM