Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-01-28
12
Medium Priority
?
694 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 111

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 111

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

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

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

579 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