Solved

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

Posted on 2014-01-28
12
663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39815255
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
ID: 39815666
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 110

Expert Comment

by:Ray Paseur
ID: 39815793
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:lthames
ID: 39815806
How can I tell if they are base64-encoded?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39815823
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
ID: 39815843
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39816217
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
ID: 39816236
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 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39816268
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
ID: 39828375
I am still waiting for the image file from the client,  but this gives me excellent resources for checking the problem.

Thanks!
0
 
LVL 110

Expert Comment

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

Author Comment

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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP substring 3 54
CMD R or Page Refresh Resending Update Query Data from Form 8 48
Format Date 7 28
PHP MYSQLI Connection in Function in a class 4 29
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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to count occurrences of each item in an array.

710 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