所以我正在制作这个从目录中获取图像的简单画廊。所有图像都有类似 tree_th.jpg 的名称,我想使用子字符串(我认为这是正确的)来切出 _th 并链接到 tree.jpg
<?
$imagetypes = array("image/jpeg", "image/gif");
function getImages($dir) {
global $imagetypes;
$dir = "img/";
$retval = array();
if(substr($dir, -1) != "/") $dir .= "/";
$fulldir = "/$dir";
//echo $fulldir;
$d = @dir($fulldir) or die("");
while(false !== ($entry = $d->read())) {
if($entry[0] == ".") continue;
$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;
}
$thumbs = getImages("img");
foreach($thumbs as $img) {
echo "<img class=\"photo\" src=\"{$img['file']}\" {$img['size'][1]} alt=\"\">\n";
}
?>