Search code examples
phpsql-serverimagejpeg

Image from db is not completed


I am trying get image data from microsoft SQL Server database from column with datatype image, show it and store to file. But some images (10%) are not completed. In browser are they rendered only as half or 1/3 from top. Every time 'problematic' images are the same and broke way is the same too.

Problematic images i was trying show and store on 2 different servers

  • Not working server - PHP Version 5.5.19-1~dotdeb.1 (cca 10% images broken)
  • Working server - PHP Version 5.2.17 (all images ok)

If i use on both servers this:

var_dump(base64_encode($row['data'], "UTF-8"));

It looks like part of image is missing - string is shorter.

Is this some data interpretation/converting issue?

If i use on fetched data iconv_get_encoding ormb_detect_encoding both return false on both servers...

My code:

$conn = mssql_connect ('xxx.xxx.xxx.xxx' , 'xxxxxxxxx', 'xxxxxxxxxxxxxxxxx' );

if (!$conn) {
    echo "<p style='color: red'>error in connection db! id: ".$id."</p>";
} else {
    mssql_select_db('CK', $conn);
    echo "<p style='color: green'>connection ok for id: ".$id."</p>";
}

$result = mssql_query(
    "SELECT k.data FROM KatG k 
     WHERE k.auto='".$id."'
     ORDER BY k.attr DESC");

$row = mssql_fetch_assoc($result);


$imageData = base64_encode($row['data']);
$src = 'data: image/jpeg;base64,'.$imageData;
$img = '<img style="height: 60px; float: right;" src="'. $src. '">';

echo $img."<p>OK i get data!</p> <hr>";

If i save this image like this: (without base64_encode function)

$im = imagecreatefromstring($row['data']);
imagejpeg($im, $path);

Picture is broken same way as displayed image in browser.


Solution

  • Was necessary increase TEXTSIZE before mssql_fetch_assoc and images are ok...

    mssql_query('SET TEXTSIZE 10000000');
    

    So difference was in php.ini there you can set this value pernament

    mssql.textlimit = 10000000
    mssql.textsize = 10000000