Lynn Thames
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:
On the problem images, the following error is logged to system.log -
ERR (3): Notice: imagecreatefromstring() [<a href='function.imagecreate fromstring '>function .imagecrea tefromstri ng</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:
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!
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);
On the problem images, the following error is logged to system.log -
ERR (3): Notice: imagecreatefromstring() [<a href='function.imagecreate
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);
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!
ASKER
Doing this doesn't trigger the error handling. Any thoughts?
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.
$fileOut = "MageImages/test2.jpg";
$imagestring = file_get_contents($fileIn);
$image = imagecreatefromstring($imagestring);
if(!$image) die('oops');
imagejpeg($image, $fileOut);
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($im agestring) ;
if(!$image) die('oops');
if(!$image) die('oops');
ASKER
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.
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">';
}
}
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?
ASKER
Yes, it works correctly when I open the image with
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!!!!!!
$fileOut = "MageImages/test2.jpg";
$imagestring = file_get_contents($fileIn);
$image = imagecreatefromstring($imagestring);
imagejpeg($image, $fileOut);
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.
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.
ASKER
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.
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?
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?
ASKER
I just tried the 3 examples that I know about and they were all truncated at 64512. I am checking your article now.
Thanks!
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That fixed it. We increased the freetds text buffer and it worked brilliantly!
Something to remember!
Something to remember!
ASKER
Thank you so much for your help!
Thanks for using EE. Glad we could find a solution!
ini_set('gd.jpeg_ignore_wa
The way I handled this in code is like so
$image = @imagecreatefromstring($im
if(!$image) {
// Handle error here
};