E su e dai solo per i tuoi occhi
Codice PHP:
<?
$allowed_types 
= array('png','jpg','jpeg','gif');
$dir="img/";
$i=1;
$myImgArray = array();
if (
is_dir($dir)) 
{
    if (
$dh opendir($dir)) 
    {
        while ((
$file readdir($dh)) !== false
        {
            if (
$file != "." && $file != ".."
            {
                if(
in_array(strtolower(substr($file,-3)),$allowed_types))
                {
                    
$size getimagesize($dir.$file);
                    
//file dir width height
                    
$myImgArray[] = $file."|".$dir."|".$size[0]."|".$size[1];
                    
                }
            }
        }
        
closedir($dh);
    }
}
//AD ESEMPIO shuffle Questa funzione mescola un array (rende casuale l'ordine degli elementi).
//Su php.net di queste funzioni ne esistono parecchie
//echo "<pre>";
//print_r($myImgArray);
shuffle($myImgArray);
//print_r($myImgArray);
echo "<table border=\"0\"><tr>\n";
foreach(
$myImgArray as $value)
{
    list(
$file,$dir,$width,$height) = explode("|",$value);
    echo 
"<td><div class=\"thumb_img\"><a href=\"#\"><img src=\"".$dir.$file."\" height=\"".$height."\" width=\"".$width."\" alt=\"".$file."\" onclick=\"javascript:window.open('view.php?path=".$dir.$file."','_blank','tollbar=0')\" border=\"0\" /></a></div>\n</td>";
    if ((
$i 4)==&& $i!=0) {echo "</tr><tr>";}
    
$i++;
}
echo 
"</table>";
?>



Ps.
Non c'è bisogno di dire che lo script è molto migliorabile
sia JS sia PHP