在PHP中递归收集图像文件路径 [英] Gather image file paths Recursively in PHP

查看:82
本文介绍了在PHP中递归收集图像文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个非常大的PHP类,它从命令行执行了很多关于图像优化的东西,你基本上将程序传递给图像路径或者文件夹路径里面有多个图片。然后它通过最多5个其他命令行程序运行文件来优化图像。

I am working on a pretty large PHP class that does a lot of stuff with Image Optimization from the Command line, you basically pass the program an Image path or a Folder path that has multiple images inside of it. It then runs the files through up to 5 other command line programs that optimize images.

下面是收集图像路径的循环的一部分,如果路径是文件夹而不是图像路径,它将迭代文件夹中的所有图像并将它们添加到图像数组。

Below is part of a loop that gathers the images paths, if the path is a Folder instead of an image path, it will iterate over all the images in the folder and add them to the image array.

到目前为止,我已经完成了单个图像和图像的所有工作在1个文件夹中。我想修改下面的这一部分,以便递归地再深入1个文件夹以获取图像路径。

So far I have everything working for single images and images in 1 folder. I would like to modify this section below so it could recursively go deeper then 1 folder to get the image paths.

有人可能会告诉我如何将此修改为以下内容完成这个?

Could someone possibly show me how I could modify this below to accomplish this?

// Get files 
if (is_dir($path))
{
    echo 'the path is a directory, grab images in this directory';
    $handle = opendir($path);
    // FIXME : need to run recursively
    while(FALSE !== ($file = readdir($handle))) 
    {
        if(is_dir($path.self::DS.$file))
        {
            continue;
        }
        if( ! self::is_image($path.self::DS.$file))
        {
            continue;
        }
        $files[] = $path.self::DS.$file;
    }
    closedir($handle);



}else{
    echo 'the path is an Image and NOT a directory';
    if(self::is_image($path))
    {   
        echo 'assign image Paths to our image array to process = '. $path. '<br><br>';
        $files[] = $path;
    }
}

if (!count($files))
{
    throw new NoImageFoundException("Image not found : $path");
}






UPDATE



@Chris的回答让我看了Docs,我发现了一个我修改过的例子似乎有效


UPDATE

@Chris's answer got me looking at the Docs and I found an example that I modified to this that seems to work

public static function find_recursive_images($path) {
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),
                                              RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($iterator as $path) {
      if ($path->isDir()) {
         //skip directories
         continue;
      } else {
         $files[] = $path->__toString();
      }
    }
    return $files;
}

...

$files = self::find_recursive_images($path);
echo '<pre>';
print_r($files);
echo '</pre>';
exit();

输出只是文件名,这样的路径是我的最终目标,到目前为止这是有效的完美,但总是如果有更好的方式,我全部改善

The output is JUST the filenames and there path like this which is my ultimate goal, so far this works perfect but as always if there is a better way I am all for improving

(
    [0] => E:\Server\_ImageOptimize\img\testfiles\css3-generator.png
    [1] => E:\Server\_ImageOptimize\img\testfiles\css3-please.png
    [2] => E:\Server\_ImageOptimize\img\testfiles\css3-tools-10.png
    [3] => E:\Server\_ImageOptimize\img\testfiles\fb.jpg
    [4] => E:\Server\_ImageOptimize\img\testfiles\mysql.gif
    [5] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\css3-generator.png
    [6] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\css3-please.png
    [7] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\css3-tools-10.png
    [8] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\fb.jpg
    [9] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\mysql.gif
    [10] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\support-browsers.png
    [11] => E:\Server\_ImageOptimize\img\testfiles\support-browsers.png
)


推荐答案

虽然andreas的答案可能有效,但你也可以让PHP 5的 RecursiveDirectoryIterator 为您工作并使用更多OOP方法。

While andreas' answer probably works, you can also let PHP 5's RecursiveDirectoryIterator do that work for you and use a more OOP approach.

这是一个简单的例子:

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));

while ($it->valid())
{
  if ($it->isDot())
    continue;

  $file = $it->current();
  if (self::is_image($file->pathName))
  {
    $files[] = $file->pathName;
  }

  $it->next();
}

编辑:

或者,可以尝试这(从Zend_Translate_Adapter复制):

Alternatively, you could try this (copied from Zend_Translate_Adapter):

$it = new RecursiveIteratorIterator(
                new RecursiveRegexIterator(
                    new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
                    '/^(?!.*(\.svn|\.cvs)).*$/', RecursiveRegexIterator::MATCH
                ),
                RecursiveIteratorIterator::SELF_FIRST
            );

foreach ($it as $dir => $info)
{
  var_dump($dir); 
}

干杯

克里斯

这篇关于在PHP中递归收集图像文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆