Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 796
  • Last Modified:

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!
0
lthames
Asked:
lthames
  • 8
  • 4
  • 3
  • +1
2 Solutions
 
GaryCommented:
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
};
0
 
lthamesAuthor Commented:
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.
0
 
GaryCommented:
$image = @imagecreatefromstring($imagestring);
if(!$image) die('oops');
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lthamesAuthor Commented:
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

0
 
Ray PaseurCommented:
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?
0
 
lthamesAuthor Commented:
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!!!!!!
0
 
Surendra NathTechnology LeadCommented:
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.
0
 
lthamesAuthor Commented:
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.
0
 
Ray PaseurCommented:
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?
0
 
lthamesAuthor Commented:
I just tried the 3 examples that I know about and they were all truncated at 64512.  I am checking your article now.

Thanks!
0
 
GaryCommented:
Possibly this
http://christianriesen.com/post/39673052683/ms-sql-blob-truncated
Connecting from Linux to MSSQL???
0
 
lthamesAuthor Commented:
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?
0
 
Ray PaseurCommented:
Could it be the sql server configuration?
I don't know.  I think we can say we know what the symptom is now - image files larger than 64,512 bytes get truncated.  Reading the post from Christian Reisen, we get a really enlightening quote:
I'm using freetds to access the database and there is an entry in freetds.conf that reads something like this: text size = 64512.  Upping that number (and restarting the webserver, apache or IIS) did the trick. The images stored in the database were all around that point, so it looked like a random bug, but thankfully it wasn’t random.
So going forward, I think you want to contact the owner of the data base and show them this issue.  I'll bet it can get fixed.
http://christianriesen.com/post/39673052683/ms-sql-blob-truncated
0
 
lthamesAuthor Commented:
That fixed it.  We increased the freetds text buffer and it worked brilliantly!

Something to remember!
0
 
lthamesAuthor Commented:
Thank you so much for your help!
0
 
Ray PaseurCommented:
Thanks for using EE.  Glad we could find a solution!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now