mySQL blob images printout?

╄→гoц情女王★ 提交于 2019-12-11 14:02:26

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/8645578/mysql-blob-images-printout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!