1

I'm using the following to detect an EXTERNAL image's extension:

        $url = (POST_IMAGESELECTOR);
        $path_parts = pathinfo($url);
        $extension = $path_parts['extension'];

All images come from various external sources. The sample above works 80% of the time, however, it fails when I run into an image without a traditional extension. An example would be the image located here:

http://media.kohls.com.edgesuite.net/is/image/kohls/227522?wid=1000&hei=1000&op_sharpen=1

What would the extension be on an image like this? How can I return the true extension?

Thanks!

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • I think you will have to check the mime-type because there is no file extension (it's a resource not a file), see: [getting image type of remote image in php](http://stackoverflow.com/questions/3931839/getting-image-type-of-remote-image-in-php) – Wesley Murch Jan 05 '12 at 07:53

3 Answers3

1

If you're sure that the file in question actually is an image then you could use getimagesize() or exif_imagetype() to determine the type of image you're working with. You can then map that onto the appropriate filename extension.

Be careful though, you can't be sure what will happen if you call these functions on files that aren't images.

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • Good idea. I'm already running getimagesize() at a different point in the script, and I can pass the file type from there. Thanks. – Paul Dessert Jan 05 '12 at 08:05
0

only for benefit.. another approach:

$newFileName = $_FILES['userfile']['name'];
$fileExt =substr(strrchr($newFileName,'.'),1);
Ahmed khaled
  • 51
  • 1
  • 7
0

Please try this code.

$url = (POST_IMAGESELECTOR);
list($width, $height, $type, $attr) = getimagesize($url);
$imageType = image_type_to_mime_type($type);
witch($imageType) {
    case "image/jpeg":
        // your actions go here...
}
Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52