PHP:从MySQL Blob直接将图像检索到< img>标签
我已将我的图像存储到(中)BLOB字段中,并希望检索嵌入在PHP生成的网页中的图像。
I've stored my Images into (Medium) BLOB fields and want to retrieve them embedded within my PHP-generated web pages.
当我测试使用
header('Content-type: ' . $image['mime_type']);
echo $image['file_data'];
一切看起来都很好。
但是,我还没有找到一种方法将图像干净地检索到我的文档中间。例如,使用
However, I have not yet found a way to retrieve the image(s) cleanly into the middle of my documents. For example, using
$image = $row['file_data'];
echo '<img src="data:image/jpeg;base64,'.$image['file_data'].'" alt="photo"><br>';
...或...
$im = imageCreateFromString($image);
我只是在屏幕上看到一堆十六进制垃圾。
I just wind up with a bunch of hexadecimal garbage on screen.
我使用以下方式直接存储图像:
I intitially stored the Images using:
ob_start();
imagejpeg($resizedImage, null, 100);
$content = ob_get_contents();
ob_end_clean();
$sql = sprintf(
"insert into images (filename, mime_type, file_size, file_data, event_id)
values ('%s', '%s', %d, '%s',%d)",
mysql_real_escape_string($fileName),
mysql_real_escape_string($mimeType),
$imageSize,
mysql_real_escape_string($content),
$eventID
);
$result = $cn->query($sql);
有没有人请有一个有效的代码片段来成功显示PHP中存储的.jpg中间文件输出?
Does anyone PLEASE have a working code snippet to successfully display the stored .jpg mid-file in the PHP output?
echo '<img src="data:image/jpeg;base64,'.base64_encode($image['file_data']).'" alt="photo"><br>';
但请记住,旧的IE版本不支持这种内嵌图像!除此之外,浏览器不能缓存这样的图像,除了它包含的HTML页面。
However, remember that old IE versions do not support this kind of inline images! Besides that, the browser cannot cache such an image except together with its containing HTML page.