php 将Mysql数据库中的BLOB图像显示为html中的动态div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34111390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:21  来源:igfitidea点击:

Displaying BLOB image from Mysql database into dynamic div in html

phphtmlmysql

提问by Kamanda

I have a BLOB image that is stored when the user submits an advert form, they have the choice of uploading one image. The image is stored in the database with the other information.

我有一个 BLOB 图像,在用户提交广告表单时存储,他们可以选择上传一张图像。该图像与其他信息一起存储在数据库中。

Every time my page loads it dynamically creates the advert divs and also fills in the matching information from my database into the div.

每次我的页面加载时,它都会动态创建广告 div,并将我的数据库中的匹配信息填充到 div 中。

The problem I have is displaying all the information including the image together at the same time so when the user clicks to view the page they see each div with a different picture and information. I've seen the other posts on how to display the image but I doubt its the same method when you're displaying several images from the same database.

我遇到的问题是同时显示包括图像在内的所有信息,因此当用户单击查看页面时,他们会看到每个 div 具有不同的图片和信息。我已经看过有关如何显示图像的其他帖子,但是当您显示来自同一数据库的多个图像时,我怀疑它的方法是否相同。

My database is set up as follows:

我的数据库设置如下:

ID ADTITLE EMAIL PRICE DESCRIPTION CATEGORY name type size contentDATE

ID ADTITLE EMAIL 价格描述类别 名称类型尺寸内容日期

The bold variables are for the image/blob

粗体变量用于图像/blob

Here is my code where I retrieve the information :

这是我检索信息的代码:

$Category = 'Specials'; 
$query = $pdo->prepare("SELECT * FROM adsubm WHERE CATEGORY LIKE '%$Category%' ORDER BY DATE DESC" );
$query->execute();

while($row = $query->fetch()) {
    $Adtitle=$row['ADTITLE'];
    $Desc=$row['DESCRIPTION'];
    $Price=$row['PRICE'];
    $Date=$row['DATE'];
    $timestamp=strtotime($Date);
    $Day= date("d",$timestamp);
    $Month=date("F",$timestamp);
    $Newmonth=date('M', strtotime($Month . '01'));
    $Year=date("Y",$timestamp);
    header('Content-type: image/jpeg');
    $Image=$row['content'];


echo  " 
           <div class='[ col-xs-12 col-sm-offset-2 col-sm-8 ]' style='margin-top: 10px'>
                <ul class='event-list'>
                    <li>
                        <time datetime='$Date'>
                            <span class='day'>$Day</span>
                            <span class='month'>$Newmonth</span>
                            <span class='year'>$Year</span>
                            <span class='time'>ALL DAY</span>
                        </time>
                        <img alt='#' src='$Image/>
                        <div class='info'>
                            <h2 class='title'>$Adtitle</h2>
                            <p class='desc'>$Desc</p>
                                                        <ul>
                                <li style='width:50%;'><span class='fa fa-money'></span>  $Price</li>
                            </ul>
                        </div>

                    </li>
                              </ul>
                    </div>
        ";

My php skills are still beginner levels as well. I'm just trying to keep things as plain and simple as possible, i'll look into other methods abit later.

我的 php 技能仍然是初学者水平。我只是想让事情尽可能简单明了,稍后我会研究其他方法。

All help is appreciated. Thanks

感谢所有帮助。谢谢

回答by Blag

1) Base64 option

1) Base64 选项

Work with a single line, image/pngfor a png image and image/jpegfor a jpg one :

使用单行,image/png用于 png 图像和image/jpegjpg图像:

echo '<img src="data:image/png;base64,'.base64_encode($blob).'"/>';

example :

例子 :

<div style="background-color:black; text-align:center; padding: 5px;">
  <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAwBAMAAACh2TSJAAAALVBMVEUAAADtNTX////3n5/+9fX719f7zMz5tLTzfHzuQED//f31jY3ybGzxXV3wVFRaxp+rAAAAAXRSTlMAQObYZgAAALVJREFUOMut0rENAjEQRNHdC4kY0QBaAQUQX0QAFSAKIKQEKiAA6VqgIkriApuV1x7pQPz0aWwHljLMpZ0CRDBGoXmeghGYKFJsUo90giAImCgV5OJF+oOgKE48MlGgs2VLBIunWesw0a1ZHqF82c7GmmIfUSpgotOly29DFPFJFDEhkgIT/V5mZuvj6XofKrHU6vyI4u37IYi36aN4h5tL7PJyif1dvCgEpapzISbCTEj5R78BZq5A5Ldh2XYAAAAASUVORK5CYII">
</div>



2) Dedicated page

2) 专用页面

With many big picture on the same page, the base64 may be not the good choice

同一个页面有很多大图,base64可能不是很好的选择

Base64 is cool, but a bit heavy(usually around twice as the binaryvalue encoded) and can't be cachedby the browser as it's a part of the page, and not a page by itself (like a picture).

Base64 很酷,但有点重(通常是编码的二进制值的两倍左右)并且不能被浏览器缓存,因为它是页面的一部分,而不是页面本身(如图片)。

In this case, the best is to use a specific php page to display your picture :

在这种情况下,最好使用特定的 php 页面来显示您的图片:

On the main page use instead of base 64 : echo '<img src="image.php?id='.$id.'"/>';with the id of the line you want the image.

在主页上使用而不是 base 64 :echo '<img src="image.php?id='.$id.'"/>';带有您想要图像的行的 id。

On your image.php, for the basic you should use this :

在你的image.php,对于基本的你应该使用这个:

// << include the $pdo here
$query = $pdo->prepare("SELECT `content` FROM `adsubm` WHERE `id` = :id" );
$query->execute(array(':id'=>$_GET['id']));
$data = $query->fetch();

if(empty($data)))
    header("HTTP/1.0 404 Not Found");
else {
    header('Content-type: image/jpeg');
    echo $data['content'];
}