Solved

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

Posted on 2014-01-28
12
657 Views
Last Modified: 2014-02-05
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?
0
Comment
Question by:lthames
  • 6
  • 6
12 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:lthames
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:lthames
Comment Utility
How can I tell if they are base64-encoded?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:lthames
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:lthames
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:lthames
Comment Utility
I am still waiting for the image file from the client,  but this gives me excellent resources for checking the problem.

Thanks!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE!  And best of luck with the project, ~Ray
0
 

Author Comment

by:lthames
Comment Utility
I posted a new question.  The information in this response was not correct.  I had performed the test wrong!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert a GMT TimeStamp to Local Time using PHP 3 18
encyps queries mssql 15 24
Mssql SQL query 14 24
Sql query 34 16
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 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

10 Experts available now in Live!

Get 1:1 Help Now