使用PHP在目录中列出和预览图像

时间:2012-07-07 11:43:29

标签: php image

我正在尝试改进我网站的管理员面板。我需要在缩略图文件夹中预览图像,以便当我使用缩略图获取新闻时,我不必再次上传图像。我找到了一个很棒的脚本,但是我无法读取目录错误。这是脚本:

<?php
  // filetypes to display
  $imagetypes = array("image/jpeg", "image/gif", "image/png");

  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  function getImages($dir)
  {
    global $imagetypes;

    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // full server path to directory
    $fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir";

    $d = @dir($fulldir) or die("getImages: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;

      // check for image files
      $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;
  }

  // fetch image details
  $images = getImages("../images/thumbnails");

  // display on page
  foreach($images as $img) {
    echo "<div class=\"photo\">";
    echo "<img src=\"{$img['file']}\" {$img['size'][3]} alt=\"\"><br>\n";
    // display image file name as link
    echo "<a href=\"{$img['file']}\">",basename($img['file']),"</a><br>\n";
    // display image dimenstions
    echo "({$img['size'][0]} x {$img['size'][1]} pixels)<br>\n";
    // display mime_type
    echo $img['size']['mime'];
    echo "</div>\n";
  }
?>

如果有人可以提供帮助,我真的很感激。

修改

<div style=" height: 200px; width: 600px; overflow: auto;">
<?PHP
 foreach(glob("../thumbnail/".'*') as $filename){
    echo "<div style=\"display:inline-table; font-size:10px; font-family:'Tahoma'; margin:5px;\">";
    echo "<img width=\"100px\" height=\"100px\" src=\"../thumbnail/$filename\"/>";
    echo "<br>".basename($filename) . "<br>";
    echo "</div>";
}
?>
</div>

这种方法很完美。无需使用复杂的脚本。 无论如何,有人可以告诉我如何检查显示小于100px x 100px的图像吗?

1 个答案:

答案 0 :(得分:1)

<?PHP
 foreach(glob("../thumbnail/".'*') as $filename){
    list($width, $height, $type, $attr) = getimagesize("../thumbnail/".$filename);
    if($width>=100 || $height >=100) continue;
    $rest = substr($filename, 3);
?>

应该这样做..