【问题标题】:How to get the pictures from folder one by one and display in the page using PHP如何从文件夹中一张一张获取图片并使用PHP显示在页面中
【发布时间】:2009-08-01 03:59:12
【问题描述】:

如何从文件夹中获取图像并将其显示在页面中,是否可以在 php 中调整它的大小,或者我必须调整它的大小并单独上传以显示为缩略图?

【问题讨论】:

    标签: php image


    【解决方案1】:

    这是遍历目录和处理图像文件的基本结构(假设'images' 是与脚本相同目录中的目录)

    $image_types = array(
        'gif' => 'image/gif',
        'png' => 'image/png',
        'jpg' => 'image/jpeg',
    );
    
    foreach (scandir('images') as $entry) {
        if (!is_dir($entry)) {
            if (in_array(mime_content_type('images/'. $entry), $image_types)) {
                // do something with image
            }
        }
    }
    

    从这里,您可以将图像直接发送到浏览器,为 HTML 页面生成标签或使用GD functions 创建缩略图并存储它们以供显示。

    【讨论】:

    • 请注意,此解决方案使用了已弃用的 mime_content_type 函数,该函数需要 mime_magic php 模块。另外,如果我错了,请纠正我,但 for 循环似乎不是有效的 Php,我认为应该是 foreach( scandir('images') as $entry)
    • @emileb 更正了循环语法。
    【解决方案2】:

    我认为这对你有帮助!

    <?
    $string =array();
    $filePath='directorypath/';  
    $dir = opendir($filePath);
    while ($file = readdir($dir)) { 
       if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) { 
       $string[] = $file;
       }
    }
    while (sizeof($string) != 0){
      $img = array_pop($string);
      echo "<img src='$filePath$img'  width='100px'/>";
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      eregi 现在已弃用,因此您可以改用preg_match

      <?php
      $string =array();
      $filePath='directorypath/';  
      $dir = opendir($filePath);
      while ($file = readdir($dir)) { 
         if (preg_match("/.png/",$file) || preg_match("/.jpg/",$file) || preg_match("/.gif/",$file) ) { 
         $string[] = $file;
         }
      }
      while (sizeof($string) != 0){
        $img = array_pop($string);
        echo "<img src='$filePath$img' >";
      }
      ?>
      

      【讨论】:

        【解决方案4】:
        【解决方案5】:

        这是一个基于another answer 的类似问题的单行代码:

        // this will get you full path to images file.
        $data = glob("path/to/images/*.{jpg,gif,png,bmp}", GLOB_BRACE);
        
        // this will get you only the filenames
        $data= array_map('basename', $data);
        

        最初,我想使用@Imran solution,但mime_content_type 不可用,并且服务器(我对其零控制)使用旧版本的 Apache 和 Php。

        所以我对其进行了一些更改以使用文件扩展名,并在此处提供。

        $imgDir = "images_dir";
        
        // make sure it's a directory
        if (file_exists($imgDir)) {
        
            // select the extensions you want to take into account
            $image_ext = array(
                    'gif',
                    'png',
                    'jpg',
                    'jpeg'
            );
        
            foreach (scandir($imgDir) as $entry) {
                if (! is_dir($entry)) { // no need to weed out '.' and '..'
                    if (in_array(
                            strtolower(pathinfo($entry, PATHINFO_EXTENSION)), 
                            $image_ext)) {
        
                        // do something with the image file.
                    }
                }
            }
        }
        

        代码已经过测试并且正在运行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-10-21
          • 2013-06-11
          • 1970-01-01
          • 2021-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多