mySQL blob 图像打印输出?

发布于 2024-12-23 01:42:32 字数 984 浏览 0 评论 0原文

我知道以前已经有人问过这个问题,并且我知道您可以通过为每个图像制作单独的页面来完成此操作。但这对于我想要的并不理想。

我想做一件古老的事情,在同一页面上显示来自数据库的多个图像:

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_imageuser_thumb 是 blob 图像,有没有办法在上面显示它们全部页面,也许将它们设置为 php 变量,然后转换为 javascript 或类似的东西?而不是:

  header('Content-type: image/jpg');
  echo $thumb;

在单独的文件中?

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 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;

In a seperate file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林散布 2024-12-30 01:42:32

这里基本上有两个问题:

  1. 由于 $thumb 包含图像的二进制数据,浏览器将无法理解它,除非您告诉浏览器它是什么数据(例如 image/ jpg)。

  2. 您需要告诉浏览器数据在哪里。

假设您要创建一个在该页面中显示拇指的图像:

<td><img src="..." alt="thumb"></td>

src 属性告诉浏览器在哪里可以找到该图像的数据。因此它用于解决问题 2。它需要一个统一资源定位符 (URI)。

那么如何将 $thumb 放入 URI 中呢?有多种方法可以做到这一点,包括一个 链接在评论中

但是,如果图像不是很大并且不需要专门缓存它(例如应该缓存 HTML,但不缓存缩略图),则可以使用 data: URI 方案维基百科

$thumbSrc = 'data:image/jpg;base64,'.base64_encode($thumb);

然后可以输出该变量作为 src 属性的值:

<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>";

You have basically two problems here:

  1. 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).

  2. 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>

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).

So how to get the $thumb into an URI? There are multiple ways to do that, including the one linked in a comment.

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 Scheme­Wikipedia:

$thumbSrc = 'data:image/jpg;base64,'.base64_encode($thumb);

You then can output that variable as the src attribute's value:

<td><img src="<?php echo $thumbSrc; ?>" alt="thumb"></td>   

Hope this is helpful.

Complete answer:

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>";
何必那么矫情 2024-12-30 01:42:32

您可以使用数据 URI 方案。但请注意,并非所有浏览器都支持这种类型的 URI。

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><img src='data:image/jpeg;base64," . base64_encode($row['user_thumb']) . "' alt='' /></td>";
    echo "</tr>";
}
echo "</table>";

You may use Data URI Scheme. But note that not all browsers support this type of URI.

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><img src='data:image/jpeg;base64," . base64_encode($row['user_thumb']) . "' alt='' /></td>";
    echo "</tr>";
}
echo "</table>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文