3

I've tried to get an image from an MSSQL Database. It's saved as "image"

I've tried "image/png", "image/jpeg", "image/gif", "image" as Content Type.

This is my code to get the Image(The $medi_conn comes from an included File):

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';
}
?>

This is an example of an Image:

0x89504E470D0A1A0A0000000D49484452000000140000001408060000008D891D0D000000097048597300000B1300000B1301009A9C180000000467414D410000B19E614C41F7000000206348524D00007A25000080830000F9FF000080E8000052080001155800003A970000176FD75A1F90000000EF4944415478DA62FCFFFF3F0335014000B180884F9F3E814DFDF7EF1F5986FCFEFD9BE1F3E7CF0C4A4A4A8C00010436F0C78F1F0C2097FEF9F3872C037FFEFCC970E6CC1990810C0001C402B30164188826073C7FFE9CE1D9B3676036400031315019000410D50D04082016420A9C37E66315DFEB3F11AB384000B1106B332E03D001400051DDCB000144B40B91BD8ECFB5000144752F030410D5BD0C10405437102080A86E204000B1502BEC60002080A8EE428000A2BA81000104F6F2AF5FBF185EBE7CC9F0EAD52BB20C79F7EE1DC3FBF7EFC16C8000021B78EEDC3930FEF6ED1BC52E040820466A5701000106001B39508BCCE3CEDB0000000049454E44AE426082

But this is the Result:

    �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`�

Has anyone an idea? Thanks

2
  • You should store the filename in the database and not the image itself.. Commented Oct 29, 2015 at 12:19
  • I can just read the Database. And the images are stored there Commented Oct 29, 2015 at 12:20

1 Answer 1

3

Try this code :-

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

Use code like that:-

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).'"/>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! That's it
it's my pleasure :) @BastianE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.