来自MySQL的PHP​​显示图像BLOB [英] PHP display image BLOB from MySQL

查看:71
本文介绍了来自MySQL的PHP​​显示图像BLOB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示存储在数据库的BLOB列中的图像;

I'm attempting to display an image stored in the BLOB column in the database;

我使用SELECT从数据库中获取数据,不对数据进行任何转换,并显示以下内容(从仅输出以下内容的脚本中显示):

I fetch the data from the database with a SELECT perform no transformations on the data and display it with the following (from a script whose only output is the following):

header("Content-Type: image/jpeg");
echo $image;

请注意,chrome正在将内容大小显示为图像的正确大小以及正确的mime类型(image/jpeg).在头文件和ive检查数据库中的blob正确之前,没有任何回应.在<?php ?>标记之前或之后也没有尾随空格.

Please note chrome is displaying the content size as the correct size for the image as well as the correct mime type (image/jpeg). nothing is echoing out before the header and ive checked the blob in the database is correct. There is also no trailing whitespace before or after the <?php ?> tags.

chrome/IE显示图像图标,但不显示图像本身.有什么想法吗?

chrome/IE displays an image icon but not the image itself. any ideas?

图像是从数据库中获取的,例如:

image is got the from the database as such:

$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$row = $sth->fetch();
$image = $row['image'];

var_dump($ image)给出:

var_dump($image) gives:

string 'ÿØÿà�JFIF��x�x��ÿá�ZExif��MM�*�����������J��������Q�������Q������tQ������t�����† ��±ÿÛ�C�       

ÿÛ�CÿÀ�_"�ÿÄ����������� 
ÿÄ�µ���}�!1AQa"q2‘¡#B±ÁRÑð$3br‚ 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’""•–—˜™š¢£¤¥¦§¨©ª²³    ´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ��������'... (length=60766)

推荐答案

像这样尝试.

用于插入数据库

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//you keep your column name setting for insertion. I keep image type Blob.
$query = "INSERT INTO products (id,image) VALUES('','$image')";  
$qry = mysqli_query($db, $query);

用于从Blob访问图像

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

希望它会为您提供帮助.

Hope It will help you.

谢谢.

这篇关于来自MySQL的PHP​​显示图像BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆