Display BMP image on web page from BLOB field in Dynamics NAV Database (SQL Server) through PHP

14-02-2015
In some scenario we need to display some image in web page which is stored in Dynamics NAV SQL Server Database in BLOB data type as a BMP format. 

If you are using PHP 5+ as server side script then the following script you may use for displaying employee photo on the web page.


<?
header("Content-type: image/bmp;");
$serverName = "<server>"; // Server name or IP
$connectionInfo = array("UID"=>"<username>","PWD"=>"<password>","Database"=>"<database>");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.";
die( print_r( sqlsrv_errors(), true));
}
$iType = SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY);
$sql = 'SELECT Picture FROM [<Company Name>$Employee] where [No_]=\'' . <Employee No.> . '\'';
$result = sqlsrv_query($conn,$sql);
sqlsrv_fetch($result);
$image = sqlsrv_get_field($result, 0, $iType);
fpassthru($image);
sqlsrv_close($conn);
?>

Note : Compressed property of the BLOB field should be false.