如何使用PHP从文件夹中逐个获取图片并显示在页面中

如何使用PHP从文件夹中逐个获取图片并显示在页面中,php,image,Php,Image,如何从文件夹中获取图像并将其显示在页面中,我是否可以在php中调整它的大小,或者我必须调整它的大小并单独上载以将其显示为缩略图?以下是遍历目录和处理图像文件的基本结构(给定的'images'是脚本的同一目录中的一个目录) 从这里,您可以将图像直接发送到浏览器,为HTML页面生成标记,或使用创建缩略图并存储它们以供显示。请查看: 要直接从PHP调整图像大小,请执行以下操作: 我想这可能会对你有所帮助 <? $string =array(); $filePath='director

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

以下是遍历目录和处理图像文件的基本结构(给定的
'images'
是脚本的同一目录中的一个目录)

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

请查看:

要直接从PHP调整图像大小,请执行以下操作:


    • 我想这可能会对你有所帮助

      <?
      $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'/>";
      }
      ?>
      

      eregi
      现在已被弃用,因此您可以改用
      preg\u 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' >";
      }
      ?>
      

      下面是一个基于类似问题的一行代码:

      // 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);
      
      最初,我想使用,但是
      mime\u content\u 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.
                  }
              }
          }
      }
      

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

      请注意,此解决方案使用了不推荐的
      mime\u content\u type
      函数,它需要mime\u magic php模块。此外,如果我错了,请纠正我,但是
      for
      循环似乎不是有效的php,我认为应该是
      foreach(scandir('images')作为$entry)
      @emileb更正了循环语法。
      $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.
                  }
              }
          }
      }