views:

1133

answers:

5

I am looking for a way to take a user uploaded image that is currently put in a temporary location ex: /tmp/jkhjkh78 and create a php image from it, autodetecting the format.

Is there a more clever way to do this than a bunch of try/catching with imagefromjpeg, imagefrompng, etc?

A: 

You can call a system command (if you're under linux/unix), file if you like:

kender@eira:~$ file a
a: JPEG image data, EXIF standard 2.2
kender
+9  A: 

This is one of the functions of getimagesize. They probably should have called it "getimageinfo", but that's PHP for you.

Neall
+1  A: 

You could try finfo_file(), apparently an improved version of mime_content_type().

Edit: OK, getimagesize() is better..

aib
+1  A: 

Check out this question/answer for more options: http://stackoverflow.com/questions/134833/how-do-i-find-the-mime-type-of-a-file-with-php#134893

leek
+1  A: 

Use exif_imagetype() if it's available ..:

http://www.php.net/manual/en/function.exif-imagetype.php

I'm pretty sure exif functions are available by default (i.e. you have to specifically exclude them rather than specifically include them) when you install php

searbe