Search code examples
phpimagevarbinarymax

Varbinary(max) to picture


I am fetching data from MSSQL database and trying to convert it to picture and show on website. I have tried a few different tricks but none seem to work. So I am hoping some one would be kind enough to help me out.

The database image is too large to be shown here, but here is a clip (it was stored as varbinary(MAX) in DB) ):

$kuva = 0xFFD8FFE000104A46494600010101004800480000FFE138724578696600004D4D002A00000008000C010E0002000000200000

I assumed that it was base64 and have tried the following

> echo '<img src="data:image/jpg;base64,='.base64_encode("0xFFD8FFE000104A46494600010101004800480000FFE1365E4578696600004D4D002A00000008000C010E000200000020000008AA010F000200000005000008CA0110000200000009000008D00112000300000001000100000");

<img src="data:image/jpeg;base64,<?php echo base64_encode($kuva);?>" />

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

echo base64_encode($kuva);

echo base64_encode($kuva);
echo base64_decode($kuva);

However I can not seem to find either the right format (png, gif, jpg, jpeg) or I would have to decode/encode it with something else than base64.

I know that this site is full of anwers on this topic, but so far nothing has worked. If anyone knows the answer it would be greatly apreciated or can point out some website/program that can actually identify the code and allow it to be type juggled to a format that can be saved as image.

Thanks in advance!


Solution

  • The actual problem was that the PHP array run out of room before the whole picture data was read. This resulted in partial code being read to the array (which of course can not be shown). The solution that I used was to use a script on database server side which used t-sql to convert the picture to proper format and write it to another table.

    However this did not solve the issue with PHP query result running out of room before the whole picture was shown. I tried cutting the picture code to several array cells (reading about 50K chars in each), but the parsed result on PHP could not be shown as an image.

    If anyone knows how to avoid that problem, feel free to write the answer