PHP PDO - 从MSSQL数据库获取图像

时间:2015-10-29 12:18:06

标签: php sql-server image sql-server-2008 pdo

我试图从MSSQL数据库中获取图像。它保存为“图像”

我已尝试将“image / png”,“image / jpeg”,“image / gif”,“image”作为内容类型。

这是我获取图片的代码($ medi_conn来自一个包含的文件):      

ini_set('display_errors', '1');
error_reporting(E_ALL);

if(isset($_GET['d']) && is_numeric($_GET['d'])) {
    $sth = $medi_conn->prepare("SELECT Symbol FROM Dienstart WHERE DienstartID = :id") or die("Invalid query: " . $sth->errorInfo());
    $sth->bindParam(':id',  $_GET['d']);
    $sth->execute();
    $sth->bindColumn(1, $image, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
    $sth->fetch(PDO::FETCH_ASSOC);

    header("Content-type: image/jpeg;");
    echo $image;

}
else {
    echo 'Please use a real id number';
}
?>

这是图片的示例:

0x89504E470D0A1A0A0000000D49484452000000140000001408060000008D891D0D000000097048597300000B1300000B1301009A9C180000000467414D410000B19E614C41F7000000206348524D00007A25000080830000F9FF000080E8000052080001155800003A970000176FD75A1F90000000EF4944415478DA62FCFFFF3F0335014000B180884F9F3E814DFDF7EF1F5986FCFEFD9BE1F3E7CF0C4A4A4A8C00010436F0C78F1F0C2097FEF9F3872C037FFEFCC970E6CC1990810C0001C402B30164188826073C7FFE9CE1D9B3676036400031315019000410D50D04082016420A9C37E66315DFEB3F11AB384000B1106B332E03D001400051DDCB000144B40B91BD8ECFB5000144752F030410D5BD0C10405437102080A86E204000B1502BEC60002080A8EE428000A2BA81000104F6F2AF5FBF185EBE7CC9F0EAD52BB20C79F7EE1DC3FBF7EFC16C8000021B78EEDC3930FEF6ED1BC52E040820466A5701000106001B39508BCCE3CEDB0000000049454E44AE426082

但这是结果:

    �PNG

���
IHDR�����������
��� pHYs����������gAMA����aLA���� cHRM��z%��������������R�X��:���o�Z�����IDATx�b���?5@����O�>�M���Y���������JJJ��6�Ǐ �����,���p�������d�&<���ٳg`6@�11P��
 B
�7�c��?�8@��k3.�@�Q���D����ϵ�Du/ս@T7 ��n @��P+�`� ���B���������_�^�|����+�y�������l��x��90����. FjW��9P���������IEND�B`�

有人有想法吗? 感谢

1 个答案:

答案 0 :(得分:2)

试试这段代码: -

echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';

使用这样的代码: -

if(isset($_GET['d']) && is_numeric($_GET['d'])) {
    $sth = $medi_conn->prepare("SELECT Symbol FROM Dienstart WHERE DienstartID = :id") or die("Invalid query: " . $sth->errorInfo());
    $sth->bindParam(':id',  $_GET['d']);
    $sth->execute();
    $sth->bindColumn(1, $image, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
    $sth->fetch(PDO::FETCH_ASSOC);
    echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
}