显示在 MySql DB 中保存为 Blob 的图像

发布于 2024-10-28 16:27:40 字数 1110 浏览 3 评论 0原文

我正在创建一个博客,下面的一小段代码是博客打印出来的地方。 我的 mysql 数据库中保存了一个 blob,我正在尝试将其转回图像。 当我运行代码时,imageName、imageType、imageSize、imageContect 都会收到值。问题在于 imageContent 变量显示大量随机字符而不是图像。似乎这是标题的原因,但我不知道该怎么做。谁能帮我重新编码图像。谢谢

while($row = mysql_fetch_array($result)) 
{

    echo "name ".$row['imageName'].'<BR>';
    echo "type ".$row['imageType'].'<BR>';
    echo "size ".$row['imageSize'].'<BR>';

    echo '<B>'.$row['blogTitle'].'</B><br />';

    echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=edit&blogID='.$row['blogID'].'>Edit</A>&nbsp;&nbsp;&nbsp;&nbsp;';       
    echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=delete&blogID='.$row['blogID'].'>Delete</A><BR />';


    echo $row['blog'].'<br />';

    header("Content-length:".$row['imageSize']);
    header("Content-type:.".$row['imageType']);
    header("Content-Disposition: attachment; filename=".$row['imageName']);
    echo $row['imageContent'].'---------<br /><br /><br />';

}

Im creating a blog and the little bit of code below is where the blog geets printed out.
I've got a blob saved in my mysql database and im trying to turn it back into an image.
the imageName, imageType, imageSize, imageContect all receive values when i run my code. The problem is that the imageContent variable displays a load of random characters rather then an image. it seems that the reason for this is the headers but i've no idea what to do. can anyone help me to recode the image. thanks

while($row = mysql_fetch_array($result)) 
{

    echo "name ".$row['imageName'].'<BR>';
    echo "type ".$row['imageType'].'<BR>';
    echo "size ".$row['imageSize'].'<BR>';

    echo '<B>'.$row['blogTitle'].'</B><br />';

    echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=edit&blogID='.$row['blogID'].'>Edit</A>    ';       
    echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=delete&blogID='.$row['blogID'].'>Delete</A><BR />';


    echo $row['blog'].'<br />';

    header("Content-length:".$row['imageSize']);
    header("Content-type:.".$row['imageType']);
    header("Content-Disposition: attachment; filename=".$row['imageName']);
    echo $row['imageContent'].'---------<br /><br /><br />';

}

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

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

发布评论

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

评论(2

小霸王臭丫头 2024-11-04 16:27:40

这是一个相当简单的示例,显示存储为 blob 的图像。

<?php

require_once ('./includes/db.inc.php'); // Connect to the db.

//let the browser know its an image
header("Content-type: image/jpeg");

// Make the query.
$query = "SELECT thumbnail FROM items where item_id=" . $_GET['item_id'];
$result = @mysql_query ($query);

if ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  echo $row['thumbnail'];
  }

mysql_close(); // Close the database connection.
?>

Here is a fairly simple example of displaying an image stored as a blob.

<?php

require_once ('./includes/db.inc.php'); // Connect to the db.

//let the browser know its an image
header("Content-type: image/jpeg");

// Make the query.
$query = "SELECT thumbnail FROM items where item_id=" . $_GET['item_id'];
$result = @mysql_query ($query);

if ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  echo $row['thumbnail'];
  }

mysql_close(); // Close the database connection.
?>
遗失的美好 2024-11-04 16:27:40

跳过 Content-length 和 Content-disposition,只需使用 Content-type 并将其设置为有效的 MIME 类型。

假设它们是 JPEG,您可以这样做:

header( 'Content-type: image/jpeg' );

也查看此链接

Skip the Content-length and Content-disposition, just use Content-type and set it to a valid MIME type.

Assuming they're JPEGs, you'd do this:

header( 'Content-type: image/jpeg' );

Check out this link too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文