Link to home
Start Free TrialLog in
Avatar of Lynn Thames
Lynn ThamesFlag for United States of America

asked on

How can I troubleshoot an issue with an image retrieved remotely from a MSSQL image field?

Hi, this is a follow up to a previous question that was already answered.  I have a few images (out of over 1000) that are having this issue.

The images are stored in a MSSQL 2008 R2 database in image field types.

Here is the code I am using to retrieve and create the image file:
	$imageblob = $rows['ProductPicture'];
	$image = imagecreatefromstring($imageblob);
	$filename= 'var/tmp/tempfile.jpg';
	imagejpeg($image, $filename);

Open in new window


On the problem images, the following error is logged to system.log -

ERR (3): Notice: imagecreatefromstring() [<a href='function.imagecreatefromstring'>function.imagecreatefromstring</a>]: gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file

I have tested the image by saving it to the web server and using the following code:
	$fileOut = "MageImages/test2.jpg";
	$imagestring = file_get_contents($fileIn);
	$image = imagecreatefromstring($imagestring);
	imagejpeg($image, $fileOut);

Open in new window


I would love to be fix the problem but at the very least I need to find out a way to trap the error and notify the user.

Thanks for any help you can provide!
Avatar of Gary
Gary
Flag of Ireland image

You can suppress the error with
ini_set('gd.jpeg_ignore_warning', 1);

The way I handled this in code is like so

$image = @imagecreatefromstring($imagestring);
if(!$image) {
// Handle error here
};
Avatar of Lynn Thames

ASKER

Doing this doesn't trigger the error handling.   Any thoughts?

	$fileOut = "MageImages/test2.jpg";
	$imagestring = file_get_contents($fileIn);
	$image = imagecreatefromstring($imagestring);
	if(!$image) die('oops');
	imagejpeg($image, $fileOut);

Open in new window


Also, the error isn't interupting the script.  But I want to know about the error so that I don't continue and insert the broken image into their webstore.
$image = @imagecreatefromstring($imagestring);
if(!$image) die('oops');
It still doesn't trigger.  Here is the literal code from the script at http://www.shopinfotrans.cw/mssql_connect.php

I didn't include the code that connects and queries the database.
$rs = mssql_query($qry);
$RowCount = mssql_num_rows($rs);
if($RowCount>0){
      while($rows = mssql_fetch_assoc($rs)){
		$productimageblob = $rows['ProductPicture'];
		$image = @imagecreatefromstring($productimageblob);
		if(!$image) die('oops'); 
		$filename= 'MageImages/test3.jpg';
		imagejpeg($image, $filename);
		echo $rows['ProductID'] . '<br />';
		echo '<img src="' . $filename . '" alt="some_text">';
	}
}

Open in new window

I have tested the image by saving it to the web server...
And what happens with the test?  Does that work correctly, even when the data base version of the image fails?  Have you checked the length of the string retrieved from the data base versus the length of the file on the web server?
Yes, it works correctly when I open the image with

$fileOut = "MageImages/test2.jpg";
$imagestring = file_get_contents($fileIn);
$image = imagecreatefromstring($imagestring);
imagejpeg($image, $fileOut);

Open in new window


I just checked the lengths and they are NOT the same.  So it looks like we are right at what you suggested in the previous question.  It's losing data . . . .
length from file - 184327
length from db - 64512

If they don't have the data encoded with base64, why do MOST work and only a few not work?

And if that is the case, would saving the image using a different program make any difference at all?

FYI, it's not random . . . . . I have tried this image probably 100 times and it's the exact same result each time.  

Thanks!!!!!!
A quick input...

The IMAGE is a deprecate data type of SQL Server, So change that data type from IMAGE to VARBINARY(MAX), this is completly supported in .NET as well..

Check that out if it solves your issue.
Surendra Ganti:

I can't do this because the database belongs to a 3rd party inventory system that I am pulling the data from.  So they are stuck with the data structure as it is.
If the data gets there from the file, but does not get there from the database, then the transport mechanism is probably not at fault.  But the number of bytes you're getting from the database is so close to 64K that it "smells funny."

See if anything here is applicable
http://technet.microsoft.com/en-us/library/ms143432.aspx

Also, do the images from the data base get truncated at the same byte count every time?
I just tried the 3 examples that I know about and they were all truncated at 64512.  I am checking your article now.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just queried the database for all images that had datalength>64512 and checked several of them that were returned.  They all have the image cut off . .. the larger the original image the more of the image was cut off.

So now we are looking at the server cutting off the data?  

I looked at the article and noticed the "65,536 * Network Packet Size" limitation, but I don't really understand what this means.  I am in NO WAY a sql server expert (or even close).

It is confusing to me, however, because other clients (who are using the same inventory system) have images of over 1mg that are processed fine.  

Could it be the sql server configuration?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That fixed it.  We increased the freetds text buffer and it worked brilliantly!

Something to remember!
Thank you so much for your help!
Thanks for using EE.  Glad we could find a solution!