How can I get the image file type from an image field in MSSQL 2008 R2?

Hi!

I have an image field in a MSSQL 2008 R2 database that I am trying to import into into another system using php.    My problem is that I need to know the filetype.  

I seem to be having a problem when the data is not jpg.  

I am using imagecreatefromstring to turn the field contents into an image.   And then imagejpeg to save the image to a file.   I am getting errors on images that the client says are png.

Is there a way I can find out the filetype of the image?
lthamesAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
OK, this is how to research the issue.

(1) find an image that causes the failure
(2) prove to yourself that the failure is repeatable
(3) if the failure is not repeatable, the error handling routine is to just fetch the image again
(4) if the failure is repeatable, store the bad image file on your server
(5) have the inventory folks recreate the image file and store it in the data base, making sure that they followed exactly the same process they used when they created the original image (that you now have stored on your server)
(6) fetch the image from the inventory data base and compare it to the copy on your server
(7) if they are the same, indict the image storage process
(8) if they are not the same, and the new image works, have them recreate all of the failing images.
(9) if they are not the same. and the new image fails, it's time to consider a new way of storing and transmitting the images.  This is where base64_encoding may be helpful.
0
 
Ray PaseurCommented:
I'm not sure this is the "best" way, but the imageCreateFromXXX() functions give a return value.  If they return FALSE, it means they failed.  So your script could test this and if it failed on the JPG, you could try imageCreateFromPNG()

See also:
http://www.php.net/manual/en/function.getimagesizefromstring.php

Return values documented here:
http://www.php.net/manual/en/function.getimagesize.php

See: http://www.laprbass.com/RAY_temp_lthames.php

<?php // RAY_temp_lthames.php
error_reporting(E_ALL);
echo '<pre>';

// TEST WITH JPG
$url = 'RAY_orig_600x374.jpg';
$str = file_get_contents($url);
$arr = getImageSizeFromString($str);
var_dump($arr);

// TEST WITH PNG
$url = 'RAY_orig_600x374.png';
$str = file_get_contents($url);
$arr = getImageSizeFromString($str);
var_dump($arr);

Open in new window

HTH, ~Ray
0
 
lthamesAuthor Commented:
Thanks for the help.

Now I realize that the issue is not with the file type but some of the files are damaged.  They show fine in the source program.   This is a remote access to the mssql server so I'm wondering if it is getting corrupt in the query results.  

I am getting the source image from the client so that I can see if the issue is with the mssql data or something else.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Ray PaseurCommented:
That's possible.  If images are stored in a data base, they need to be base64-encoded to make them safe for transport across binary-sensitive boundaries.  If your script is reading them from a file system with cURL or file_get_contents() the encoding is not necessary.
0
 
lthamesAuthor Commented:
How can I tell if they are base64-encoded?
0
 
Ray PaseurCommented:
Ask the originator of the images how they are storing and serving them.  Chances are, if they are sending some of them in a way that works there is some corruption of the data in the images that do not work.
0
 
lthamesAuthor Commented:
I am accessing the MSSQL directly using mssql for php drivers.

I have the same application in hous, so I have all of the data (just not their images).  But in the mssql table structure all I see is image data type.
0
 
Ray PaseurCommented:
Are they really storing the images in the data base?  Why not just store the URL in the data base and store the images in the file system?  "Don't store images in a data base" is kinda CS 101.
0
 
lthamesAuthor Commented:
It is a 3rd party inventory system.  And yes, the data is really stored in the database.  They (or I) don't have any control over that.
0
 
lthamesAuthor Commented:
I am still waiting for the image file from the client,  but this gives me excellent resources for checking the problem.

Thanks!
0
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE!  And best of luck with the project, ~Ray
0
 
lthamesAuthor Commented:
I posted a new question.  The information in this response was not correct.  I had performed the test wrong!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.