【发布时间】:2018-04-28 17:37:10
【问题描述】:
我的 PHP 脚本,用于将编码的图像数据作为“blob”类型存储在数据库表中:
include './libraries/LIB_mysql.php'; # library for using the 'insert()' function at the end
$imageFilePath = "./pythonProgrammingProgram.PNG";
$imageFileRead = file_get_contents($imageFilePath); # == �PNG IHDR�A ... uY�IEND�B`� (... and so on)
$imageEncode = base64_encode($imageFileRead); # iVBORw0KGgoAAAA (... and so on)
$data_array['image_id'] = 3;
$data_array['image_file'] = $imageEncode;
$table = 'images';
insert(DATABASE, $table, $data_array);
图像数据似乎已正确存储在数据库中:
mysql> select * from images;
| image_id | image_file
| 3 | iVBORw0KGgoAAAA # (... and so on)
然后我想使用“img”标签在 HTML 页面中显示图像,该标签调用 PHP 脚本:<img src="./imageDisplay.php?pictureID=3">。
PHP脚本imageDisplay.php从数据库中检索图像数据并回显解码后的图像:
header("Content-type: image/png");
include('./libraries/LIB_mysql.php'); # for using exe_sql() function
$image_id = $_GET['pictureID']; # == 3 -> corresponds to the value in the database !!
$sql = "SELECT image_file FROM images WHERE image_id = '" . $image_id . "'"; # $image_id";
list($img) = exe_sql(DATABASE, $sql); # $img contains the decoded data: == �PNG IHDR�A ... uY�IEND�B`� (... and so on)
echo base64_decode($img);
exit;
在本地主机上加载 HTML 文件时,我看到损坏的图像。在 Firefox 下的 Network -> Response 我看到:
Name: imageDisplay.php
Dimensions: 0 × 0
MIME Type: image/png
我猜图像数据的传输不起作用,但我真的不明白为什么。 base64_decode($img)中的解码图像数据与编码前$imageFileRead中的相同,所以我猜数据库操作不是问题,而是echo base64_decode($img);或<img src="./imageDisplay.php?pictureID=3">的某个地方。找到了这个(SO question)[PHP - call a function from a img src和(this other)[The image cannot be displayed because it contains errors,但仍然看不到如何解决它:-(
【问题讨论】: