<?php

function getImages($dir,$imagetypes=null)
{
	// array to hold return value
	$retval = array();
	
	if($imagetypes==null)
		$imagetypes = array("image/jpeg", "image/gif");
		
	// add trailing slash if missing
	if(substr($dir, -1) != "/") $dir .= "/";

	// full server path to directory
	$fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir";

	$d = @dir($fulldir) or die("getImages: Failed opening directory $dir for reading");
	while(false !== ($entry = $d->read())) {
		// skip hidden files
		if($entry[0] == ".") continue;

		// check for image files
		$f = escapeshellarg("$fulldir$entry");
		$mimetype = trim(`file -bi $f`);
		foreach($imagetypes as $valid_type) {
			if(preg_match("@^{$valid_type}@", $mimetype)) {
				$retval[] = array(
					'file' => "/$dir$entry",
					'size' => getimagesize("$fulldir$entry")
				);
				break;
			}
		}
	}
	$d->close();

	return $retval;
}

function preprint($str){

	print("<pre style='float:left;text-align:left;color:white;background:black;padding:10px;'>".print_r($str,true)."</pre>");
}

?>