我对 PHP 很陌生,所以我仍在学习非常基础的知识,但是我正在尝试创建一个图片库。
后来经过无数次谷歌搜索,我找到了一个 PHP 脚本,它可以做我想做的事情,在查看代码并稍微操作它之后,它与我的网站完美配合;除了图像不是按字母顺序排列的。
这是代码
$max_width = 100;
$max_height = 100;
$imagedir = 'gifs/animals/'; //Remember trailing slash
function getPictureType($ext) {
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function getPictures() {
global $max_width, $max_height, $imagedir;
if ( $handle = opendir($imagedir) ) {
$lightbox = rand();
echo '<ul id="pictures">';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) ) {
$split = explode($imagedir, $file);
$ext = $split[count($split) - 1];
if ( ($type = getPictureType($ext)) == '' ) {
continue;
}
$name = substr($file, 0, -4);
$title = str_replace("_"," ",$name);
echo '<li><a href="'.$name.'">';
echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
echo '</a>';
echo ''.$title.'';
echo '</li>';
}
}
echo '</ul>';
}
}
我使用了 scandir() 函数,它可以按字母顺序对它们进行排序,但是我留下了一个数组。然后我使用 implode 函数将数组连接在一起,但是在那之后我被困在做什么。
任何帮助将不胜感激!
干杯。