普通网友 2009-10-17 12:56
浏览 40
已采纳

搜索所有文件和目录以在php中创建缩略图

I want to search through files and create images from jpg files. There's the code:

$dir2 = opendir($direction2);
$dir = opendir($direction);

function create_fcat($direction, $width, $direction2, $dir) {
  while(false !== ($fn = readdir($dir))) {
    $n_dir = $direction.'/'.$fn;
    if (is_dir($n_dir) && $fn != '.' && $fn != '..') {
      if ($handle = opendir($n_dir)) {
        create_fcat($fn, $width, $direction2, $handle);
      }
    } elseif ($fn != '.' && $fn != '..') {
      $ext = strtolower(substr($fn,strlen($fn)-3));
      if ($ext == 'jpg') {
        if ($img = imagecreatefromjpeg( $direction.'/'.$fn )) {
          $width_original = imagesx( $img );
          $height_original = imagesy( $img );
          if ($width_original > $height_original) {
            $new_width = $width;
            $new_height = floor( $height_original * ( $width / $width_original ) );
          } else {
            $new_height = $width;
            $new_width = floor( $width_original * ( $width / $height_original ) );
          }
          $tmp_img = imagecreatetruecolor( $new_width, $new_height );
          imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width_original, $height_original );
          imagejpeg( $tmp_img, $direction2.'/large/'.$fn );
        } else {
          echo '<p>The file cannot be loaded.</p>';
        }
      }
    }
  }
  closedir($dir);
}

The problem is: when for example we have these files:

  • MAIN
    • image1.jpg
    • FOLDER1
      • image2.jpg
      • FOLDER 1.1
        • image3.jpg

Files image1.jpg and image2.jpg are loaded, but image3.jpg is not - it looks like FOLDER 1.1 is treated as file. How can I fix it?

  • 写回答

2条回答 默认 最新

  • douwen1213 2009-10-17 13:06
    关注
    $path = '/your/top/level/dir';
    
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
     foreach ($objects as $fileinfo) {
        if ($fileinfo->isFile()) {
            echo $fileinfo->getFilename() . "
    ";
            $fullpath = $fileinfo->getPathname(); // full path to image for resizing
        }
    }
    

    This would give you all the files in a directory and all sub-directories, you can then resize each image as you wish. I always use imagmagick for this myself as i think the results are generally better than with GD but obviously GD is more commonly installed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题