【问题标题】:BLOB data as SRC attribute of image tagBLOB 数据作为图像标签的 SRC 属性
【发布时间】:2013-06-03 19:44:23
【问题描述】:

我尝试将 MySQL DB 中存储的图像(blob 数据)链接到 PHP,但我实现的唯一目标是整页字符。这就是我所做的:

$query = "SELECT image FROM uploads WHERE id = {$id}";
$image_array = mysql_query($query, $connection);
$row = mysql_fetch_array($image_array);
$image = $row['image'];

echo $image;
// echo base64_decode($content);

这会显示原始数据的存储方式。我想将它链接到 HTML 标签或至少在页面上显示它,在那里我显示了很多其他的东西 header('内容类型:图片/png'); 对我来说不是解决方案。

有什么建议吗?

【问题讨论】:

  • 你可以找到你的确切答案here

标签: php html mysql blob


【解决方案1】:

要正确显示图像,您只需在echo 之前放置一个带有Content-type: image/png 的标题

header("Content-type: image/png");
echo (base64_decode($row['image']));

如果您想放置在图像标签中,您只需要使用此代码

echo '<img src="data:image/png;base64,' . $row['image'] . '" />'; 

【讨论】:

    【解决方案2】:

    您需要提供正确的标题,否则页面不会知道您发送的内容。

    // define results into variables 
    $name=mysql_result($result,0,"file_name"); 
    $size=mysql_result($result,0,"file_size"); 
    $type=mysql_result($result,0,"file_type"); 
    $content=mysql_result($result,0,"file_stream"); 
    
    // give our picture the proper headers...otherwise our page will be confused 
    header("Content-Disposition: attachment; filename=$name"); 
    header("Content-length: $size"); 
    header("Content-type: $type"); 
    echo $content; 
    

    【讨论】:

      【解决方案3】:

      您应该使用称为“内联图像”的东西。使用此代码在页面上显示图像,假设 $image 变量包含 base64 编码数据:

      <img src="data:image/png;base64,<?php echo $image; ?>" alt="Larry" />
      

      来源:https://en.wikipedia.org/wiki/Data_URI_scheme

      【讨论】:

        猜你喜欢
        • 2011-01-02
        • 2014-02-15
        • 2016-09-11
        • 1970-01-01
        • 2017-07-05
        • 2016-04-20
        • 2014-06-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多