0

I could of course check mime types through the exif_imagetype or getimagesize and check mime types one by one... But I just want ANY image - I dont care what type... So I was wondering - can I do something like this: ?

// PHP manual says: Determine the type of an image
// and that Imagetype Constants are 1-17 so :
$tmp_imagetype = exif_imagetype('image.gif'); 
if ( ($tmp_imagetype>=1) && ($tmp_imagetype<=17) ) {
    echo "It is an image!";
} else{
    echo "It isn't an image.";
}

Can I rely on that?

What happens when the file is not an image? Will it just return non-image constant value or will it throw a warning or error

Thanks

jave.web
  • 13,880
  • 12
  • 91
  • 125
  • This looks like a duplicate of http://stackoverflow.com/questions/15408125/php-check-if-file-is-an-image. – Ashley Feb 12 '14 at 15:29
  • @Ashley But it is not as I stated in the begining of the question (but the question name could be confusing, I repaired it) – jave.web Feb 12 '14 at 15:30
  • How is the dupe different? – PeeHaa Feb 12 '14 at 15:33
  • Also what is wrong with what you currently have? – PeeHaa Feb 12 '14 at 15:34
  • @PeeHaa I am looking for an automatic solution and asking about the particular exif_imagetype - possible "duplicates" are all solving how to detect image type or how to check it one by one, or search for it in an array... And I dont know if I can rely on this my current solution – jave.web Feb 12 '14 at 15:35

3 Answers3

1

You really don't need all that you just need the following as exif_imagetype returns false if its not an image

if($imagetype = exif_imagetype('image.gif')){
    // its an image
} else {
    // its not an image
}
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • Ah you are right I missread the manual it really says "When a correct signature is found, the appropriate constant value will be returned otherwise the return value is **FALSE**. ". Do you think I should also add `@` before the exif_imagetype() call? I dont want any warnings when checking for the too small or no-a-file... – jave.web Feb 12 '14 at 15:38
  • @jave.web you can put a `@` if you want but i usually avoid them for debugging purposes – cmorrissey Feb 12 '14 at 15:40
  • Yea me too, I just wanted to know if it is a completely stupid idea or not :) – jave.web Feb 12 '14 at 15:44
1

You could check if the function returns (exactly) FALSE:

$file="SOMEFILE";
if(exif_imagetype($file)===FALSE){
    print("NOT IMAGE");
}else{
    print("IMAGE");
}
zaf
  • 22,776
  • 12
  • 65
  • 95
0

Why don't you just put all types of images in an array and then check it with this code:

if(in_array($value, $array)) {
   //it's an image! Hooray!
}else {
  //it's not.
}
annascott
  • 50
  • 1
  • 5