Consistent way to identify image files

I'm trying to figure out a way to consistently and efficiently identify if a file is an image.

Ultimately, I need the URL (not the markup) of the fullsize image for the file.

In this scenario it's not helpful to test whether the file has a thumbnail/derivative (e.g. $file->hasThumbnail) since videos - and even audio files sometimes - claim to have a thumbnail, but of course don't actually have a fullsize image.

Ideas?

Here's what I have right now, which is giving me false positives.

/*
** Get URL of the first image for the item
** Used for responsive image areas
** Usage: get_img_for_item($item,$size='fullsize')
*/
function get_img_for_item($item,$size){
		$i=0;
		foreach (loop('files', $item->Files) as $file){
			if( ($file->hasThumbnail()) && ($i==0) ){
				$url = WEB_ROOT.'/files/'.$size.'/'.$file->filename;
				$i++;
				return $url;
			}
		}
}

You might try $file->getProperty('fullsize_uri') to check. Another approach might be to use metadata($file, 'mime_type') and check it against a list of image mime types.

Hi Patrick.

$file->getProperty('fullsize_uri') isn't helpful here. I'm getting the same kind of results for audio and video files as I was getting with hasThumbnail. Running getProperty('fullsize_uri') against an audio file returns a broken link in my tests.

I was hoping to avoid using mime types but it looks like that may be my only option unless anyone has other ideas.

FYI, just using this...

function isImg($mime){
	$img = array('image/jpeg','image/jpg','image/png','image/jpeg','image/gif');
	$test=in_array($mime,$img);
		if($test){
			return true;
		}else{
			return false;
		}
}

Might also just use preg_match to see if 'image' is in the Mime type string:

function isImg($mime) {
    return preg_match('image', $mime);
}

Haven't tested this, but should return true if 'image' is in the mime string, and false if not. preg_match returns boolean.

This is an effect of Omeka agressively trying to make derivative images. Anything ImageMagick can understand, Omeka will make a thumbnail for. For some file types these thumbnails aren't particularly nice, though. (But, if we're returning true on hasThumbnail, then those files should absolutely exist. Still, that doesn't tell you if the original was an image.)

Jeremy's example is broken, but a pattern like '#^image/#' would work.

Jeremy's example is broken, but a pattern like '#^image/#' would work.

Ah yeah, sorry, you have to use a regex pattern.