好吧,我对php很新,我的问题可能非常愚蠢,但我已经尝试了我能想到的每一个组合,并且不能让它正常工作。
它根本不在空库中显示消息,或者在每个库中显示三次,无论它是否包含图像。
我知道缩略图周边不是最令人向往的,但我试图在自动图像裁剪周边工作以创建缩略图,并且无法让它在我的生活中起作用。
我主要担心的是空库消息,但是如果你可以帮我合并一个可靠的图片裁剪片段来创建更好的缩略图..请执行:)
function lightbox_display($dir_to_search, $rel){
$image_dir = $dir_to_search;
$dir_to_search = scandir($dir_to_search);
$image_exts = array('gif', 'jpg', 'jpeg', 'png');
$excluded_filename = '_t';
foreach ($dir_to_search as $image_file){
$dot = strrpos($image_file, '.');
$filename = substr($image_file, 0, $dot);
$filetype = substr($image_file, $dot+1);
$thumbnail_file = strrpos($filename, $excluded_filename);
if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){
echo "<a href='".$image_dir.$image_file."' rel='".$rel."'>
<img src='".$image_dir.$image_file."' alt='".$filename."' width='100' height='80' title='' border='none'/>
</a>"."\n";
} else {
echo 'Currently there are no machines available for sale, please check back with us soon.';
}
}
}
更新了php编码:
我尝试在$ imagesFound中添加一个数组,以排除可能包含文件夹服务器端。
function lightbox_display($dir_to_search, $rel){
$image_dir = $dir_to_search;
$dir_to_search = scandir($dir_to_search);
$image_exts = array('gif', 'jpg', 'jpeg', 'png');
$excluded_filename = '_t';
$imagesFound = array('gif', 'jpg', 'jpeg', 'png') && 0;
foreach ($dir_to_search as $image_file){
$dot = strrpos($image_file, '.');
$filename = substr($image_file, 0, $dot);
$filetype = substr($image_file, $dot+1);
$thumbnail_file = strrpos($filename, $excluded_filename);
if ((!$thumbnail_file) && array_search($filetype, $image_exts) !== false) {
$imagesFound++;
echo "<a href='$image_dir$image_file' rel='$rel'>
<img src='$image_dir$image_file' alt='$filename' width='100' height='80' title='' border='none'/>
</a>\n";
}
if ((0 === $imagesFound) !== true){
echo 'Currently there are no machines available for sale, please check back with us soon.';
}
}
}
答案 0 :(得分:1)
如果它显示三次相同的消息,我假设$dir_to_search
中有三个文件,但没有一个是图像。它们可能是.
,..
等等。每当你发现一个不是有效图像的文件时,你就输出了这个消息,所以你可以做的是保持你找到的图像数量的计数,并且仅在你找不到。例如
$imagesFound = 0;
foreach ($dir_to_search as $image_file){
$dot = strrpos($image_file, '.');
$filename = substr($image_file, 0, $dot);
$filetype = substr($image_file, $dot+1);
$thumbnail_file = strrpos($filename, $excluded_filename);
if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false) {
$imagesFound++;
echo "<a href='$image_dir$image_file' rel='$rel'>
<img src='$image_dir$image_file' alt='$filename' width='100' height='80' title='' border='none'/>
</a>\n";
}
}
if (0 === $imagesFound) {
echo 'Currently there are no machines available for sale, please check back with us soon.';
}
根据以下评论进行更新:
如果您多次调用lightbox_display(),并且仍然只希望显示一次消息,则可以返回找到的图像数量并使用它。像这样:
function lightbox_display($dir_to_search, $rel) {
$imagesFound = 0;
foreach ($dir_to_search as $image_file){
if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false) {
$imagesFound++;
echo "<img...";
}
}
return $imagesFound;
}
$totalImagesFound = 0;
foreach ($galleries as $gallery) {
$totalImagesFound += lightbox_display($gallery['dir'], $rel);
}
if (0 === $totalImagesFound ) {
echo 'Currently there are no machines available for sale, please check back with us soon.';
}
也许这会有所帮助,虽然我不太了解你的系统,但我真的不知道。