php 如何在php中显示数据库中的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13602259/
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 05:46:11  来源:igfitidea点击:

how to display images from database in php?

php

提问by Jaydipsinh

i write bellow code to display images from database

我编写波纹管代码来显示数据库中的图像

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$qry = "select id from image";
$res = mysql_query($qry) or die(mysql_error());
while($row = mysql_fetch_array($res))
{
  echo "<img src=image.php?id='$row[0]'>";
}
?>

Image.php

图片.php

<?php
$query = mysql_query("SELECT img FROM images WHERE id = ".$_GET['id']);
$row = mysql_fetch_array($query);
if($type=="pjpeg")
$type="jpeg";
header("Content-type:$type");
echo $row['img'];
?>

but this will not work. it display blank image icon.

但这行不通。它显示空白图像图标。

回答by nkamm

You also might use base64 encodingto build in the image. Like

您也可以使用 base64 编码来构建图像。喜欢

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAWgBaAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..." />

UPDATE, base64 encoding example

更新,base64 编码示例

You can do that easily:

你可以很容易地做到这一点:

<?php
$imageId = intval($_GET["id"]);

$query = mysql_query("SELECT img FROM images WHERE id = ". $imageId);
$row = mysql_fetch_array($query);

$mime = null;
// place $type init. here
if ($type=="pjpeg") // <<< where do you get $type btw?
    $mime = "image/jpeg";

$b64Src = "data:".$mime.";base64," . base64_encode($row["img"]);
echo '<img src="'.$b64Src.'" alt="" />';
?>

回答by Jeroen

jpegis not a valid Content-Type, it should be image/jpeg

jpeg不是有效的内容类型,它应该是 image/jpeg

回答by Tim

As above + be sure to pass the replace = trueparameter to the header function.

如上 + 确保将replace = true参数传递给标头函数。

header( 'Content-Type: image/jpeg', true );

header( 'Content-Type: image/jpeg', true );