mySQL Blob图像打印输出? [英] mySQL blob images printout?
问题描述
我知道这已经被问过了,我知道您可以通过为每个图像制作一个单独的页面来做到这一点.但这不是我想要的理想选择.
I know this has been asked before, and I know you can do it via making a seprate page for each image. But thats not ideal for what I want.
我想做一个古老的事情,即在同一页面上显示数据库中的多个图像:
I want to do that age old thing of displaying multiple images from a db on the same page:
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo "<td>" . $row['user_image'] . "</td>";
echo "<td>" . $row['user_thumb'] . "</td>";
echo "</tr>";
}
echo "</table>";
user_image
和user_thumb
是斑点图像,是否有某种方式在页面上显示它们,也许将它们设置为php变量,然后转换为javascript或类似内容?而不是:
user_image
and user_thumb
are blob images, is there someway of showing them all on that page, perhaps setting them to a php variable and then converting to javascript or something along those lines? Rather than:
header('Content-type: image/jpg');
echo $thumb;
在单独的文件中?
推荐答案
这里基本上有两个问题:
You have basically two problems here:
-
由于
$thumb
包含图像的二进制数据,除非您告诉浏览器它是什么数据(例如image/jpg
),否则浏览器将无法理解它.
As
$thumb
contains the binary data of the image, the browser will not understand it unless you tell the browser what data it is (e.g.image/jpg
).
您需要告诉浏览器数据在哪里.
You need to tell the browser where the data is.
假设您要创建一个在该页面上显示拇指的图像:
Let's say you want to create an image displaying the thumb in that page:
<td><img src="..." alt="thumb"></td>
src
属性告诉浏览器可以在哪里找到图像数据.因此,它用于解决问题2.它需要一个统一资源定位符(URI).
The src
attribute tells the browser where it can find the data of the image. So it is used to solve problem 2. It expects an Uniform Resource Locator (URI).
那么如何将$thumb
转换为URI?有多种方法可以做到这一点,包括一种链接的方法在评论中.
So how to get the $thumb
into an URI? There are multiple ways to do that, including the one linked in a comment.
但是,如果图像不是很大,并且您不需要专门对其进行缓存(例如,应该缓存HTML,而不是拇指图像),则可以使用
However, if the image is not very large and you don't need to have it cached specifically (e.g. the HTML should be cached, but not the thumb image), you can make use of a data:
URI SchemeWikipedia:
$thumbSrc = 'data:image/jpg;base64,'.base64_encode($thumb);
然后您可以将该变量输出为src
属性的值:
You then can output that variable as the src
attribute's value:
<td><img src="<?php echo $thumbSrc; ?>" alt="thumb"></td>
希望这会有所帮助.
完整答案:
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo '<td><img src="data:image/jpg;base64,', base64_encode($row['user_thumb']), '" alt='thumb'></td>';
echo '<td><img src="data:image/jpg;base64,', base64_encode($row['user_image']), '" alt='image'></td>';
echo "</tr>";
}
echo "</table>";
这篇关于mySQL Blob图像打印输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!