如何显示数据库中的图像


How can i display image from database?

图像无法从php中的数据库中显示,它引发了这样的错误。当我执行这个代码时,会发生这样的错误。图像无法显示,因为它包含错误。图像存储在mysql数据库中,字段类型为BLOB。IT没有找到任何解决方案来避免此错误。

<?php
require_once('database/db.class.php');
$objDatabase=new db_class();
$objDatabase->connect();
$sql="SELECT id,firstname,lastname,sex,email,userimage,ext,city,state,username FROM t_users WHERE username='".$_SESSION['username']."'";
$totalRow=$objDatabase->select($sql);
if($row=$objDatabase->get_row($totalRow, 'MYSQL_BOTH'))
    {
        $userArray=array('id'=>$row['id'],'firstname'=>$row['firstname'],'lastname'=>$row['lastname'],'sex'=>$row['sex'],'email'=>$row['email'],'userimage'=>$row['userimage'],'ext'=>$row['ext'],'city'=>$row['city'],'state'=>$row['state'],'username'=>$row['username']);
    }
?>

  <table cellspacing="10px" width="100%">
  <tr>
  <td width="23%" style="vertical-align:top">
  <?php
    header("Content-type: image/{$userArray['ext']}");
    header("Content-Length: " . strlen($userArray['userimage']));
    echo  $userArray['userimage'];
  ?>
 </td>
</tr>
</table>

上传图像代码

@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);
    // Get image type.
    // We use @ to omit errors
if ($imtype == 3) // cheking image type
$ext="png";   // to use it later in HTTP headers
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
    $ext="gif";
else
$msg = 'Error: unknown file format';
   $img = file_get_contents($_FILES['photo']['tmp_name']);
$img = mysql_real_escape_string($img);
        // Preparing data to be used in MySQL query
$data=array('firstname'=>$_REQUEST['firstname'],'lastname'=>$_REQUEST['lastname'],'sex'=>$_REQUEST['gender'],'email'=>$_REQUEST['email'],'userimage'=>$img,'ext'=>$ext,'city'=>$_REQUEST['city'],'state'=>$_REQUEST['state'],'zipcode'=>$_REQUEST['zipcode'],'username'=>$_REQUEST['usernameText'],'password'=>$_REQUEST['passwordText']);
$result=$objDatabase->insert_array('t_users',$data);    

 <input name="photo" type="file" id="photo">

edit-我忘了包括:base64文件将比BLOB大得多。


尝试将其保存为base64而不是BLOB。

这是我在上找到的一个函数http://www.php.net/manual/en/function.base64-encode.php

function base64_encode_image ($filename=string,$filetype=string) {
    if ($filename) {
        $imgbinary = fread(fopen($filename, "r"), filesize($filename));
        return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
    }
}

当您从数据库中检索图像时,它将是一个base64字符串,您可以将该字符串直接放入HTML中的src="属性中(无需解码)。浏览器可以解析base64并显示图像。

好吧,这不是方法,只需像往常一样存储图像,然后将路径存储在数据库中。

如果你真的想这样做,你需要制作一个新文件,称为image.php,然后放入

<?php
require_once('database/db.class.php');
$objDatabase=new db_class();
$objDatabase->connect();
$sql="SELECT id,firstname,lastname,sex,email,userimage,ext,city,state,username FROM t_users WHERE username='".$_SESSION['username']."'";
$totalRow=$objDatabase->select($sql);
if($row=$objDatabase->get_row($totalRow, 'MYSQL_BOTH'))
{
    $userArray=array('id'=>$row['id'],'firstname'=>$row['firstname'],'lastname'=>$row['lastname'],'sex'=>$row['sex'],'email'=>$row['email'],'userimage'=>$row['userimage'],'ext'=>$row['ext'],'city'=>$row['city'],'state'=>$row['state'],'username'=>$row['username']);
}
header("Content-type: image/{$userArray['ext']}");
header("Content-Length: " . strlen($userArray['userimage']));
echo  $userArray['userimage'];
?>

在该文件中。

然后在表格中使用

<img src='/image.php ?>' alt='' />

但不要这样做,只需将路径存储在数据库中即可,很少会将二进制数据存储在非文件系统的数据库中,原因我不在此赘述。