Solved

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

Posted on 2014-02-05
16
712 Views
Last Modified: 2014-02-06
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
Comment
Question by:lthames
  • 8
  • 4
  • 3
  • +1
16 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39836344
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
 

Author Comment

by:lthames
ID: 39836396
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
 
LVL 58

Expert Comment

by:Gary
ID: 39836413
$image = @imagecreatefromstring($imagestring);
if(!$image) die('oops');
0
 

Author Comment

by:lthames
ID: 39836433
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39836453
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
 

Author Comment

by:lthames
ID: 39836512
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
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39836685
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
 

Author Comment

by:lthames
ID: 39836721
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39836743
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
 

Author Comment

by:lthames
ID: 39836768
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
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 39836793
Possibly this
http://christianriesen.com/post/39673052683/ms-sql-blob-truncated
Connecting from Linux to MSSQL???
0
 

Author Comment

by:lthames
ID: 39836857
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39839017
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
 

Author Comment

by:lthames
ID: 39839414
That fixed it.  We increased the freetds text buffer and it worked brilliantly!

Something to remember!
0
 

Author Comment

by:lthames
ID: 39839416
Thank you so much for your help!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39839668
Thanks for using EE.  Glad we could find a solution!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

709 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now