如何从MSSQL导出图像并将其显示在网站上?

时间:2013-04-25 11:12:53

标签: php sql-server

我在使用它时遇到了一些困难。

以下是我正在使用的代码:

require_once('db_connection.php');
$Query = mssql_query("SELECT Photo FROM Persons WHERE IDPerson = '3'");
$Person = mssql_fetch_array($Query);
$Photo = $Person["Photo"];
header('Content-type: image/jpeg');
echo $Photo;

这是我得到的结果:

enter image description here

  

(图像底部出现了一些东西,但那是   只是图像的一部分)

这是我的数据库中图像值的样子:

  

0x424D3684030000000000360000002800000040010000F000000001001800000000000084030000000000000000000000000000000000FDFEF5FCFDF4FFFFFAE3F2DA   .......

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您必须使用use base64编码来显示数据库中的图像

喜欢

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

使用,base64编码

<?php
$Query = mssql_query("SELECT Photo FROM Persons WHERE IDPerson = '3'");
$Person = mssql_fetch_array($Query);
$Photo = $Person["Photo"];

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

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

例如

header("Content-type: image/png");

echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHhSURBVDjLjZPLSxtRFIfVZRdWi0oFBf+BrhRx5dKVYKG4tLhRqlgXPmIVJQiC60JCCZYqFHQh7rrQlUK7aVUUfCBRG5RkJpNkkswrM5NEf73n6gxpHujAB/fOvefjnHM5VQCqCPa1MNoZnU/Qxqhx4woE7ZZlpXO53F0+n0c52Dl8Pt/nQkmhoJOCdUWBsvQJ2u4ODMOAwvapVAqSJHGJKIrw+/2uxAmuJgFdMDUVincSxvEBTNOEpmlIp9OIxWJckMlkoOs6AoHAg6RYYNs2kp4RqOvfuIACVFVFPB4vKYn3pFjAykDSOwVta52vqW6nlEQiwTMRBKGygIh9GEDCMwZH6EgoE+qHLMuVBdbfKwjv3yE6Ogjz/PQ/CZVDPSFRRYE4/RHy1y8wry8RGWGSqyC/nM1meX9IQpQV2JKIUH8vrEgYmeAFwuPDCHa9QehtD26HBhCZnYC8ucGzKSsIL8wgsjiH1PYPxL+vQvm5B/3sBMLyIm7GhhCe90BaWykV/Gp+VR9oqPVe9vfBTsruM1HtBKVPmFIUNusBrV3B4ev6bsbyXlPdkbr/u+StHUkxruBPY+0KY8f38oWX/byvNAdluHNLeOxDB+uyQQfPCWZ3NT69BYJWkjxjnB1o9Fv/ASQ5s+ABz8i2AAAAAElFTkSuQmCC');

如果您的字符串是这样的,它将打印pdf徽标