0

好的,我对 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.';
}
   }
 }
4

1 回答 1

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.';
}

也许这会有所帮助,尽管我不了解您的系统的更多信息,但我真的不知道。

于 2013-03-15T16:30:20.013 回答