Gather image file paths Recursively in PHP(在 PHP 中递归地收集图像文件路径)
问题描述
我正在研究一个相当大的 PHP 类,它通过命令行执行大量图像优化工作,您基本上向程序传递 Image path
或 Folder path
里面有多个图像.然后,它通过最多 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");
}
<小时>
更新
@Chris 的回答让我查看了文档,我发现了一个我修改为似乎可行的示例
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\_ImageOptimizeimg estfilescss3-generator.png
[1] => E:Server\_ImageOptimizeimg estfilescss3-please.png
[2] => E:Server\_ImageOptimizeimg estfilescss3-tools-10.png
[3] => E:Server\_ImageOptimizeimg estfilesfb.jpg
[4] => E:Server\_ImageOptimizeimg estfilesmysql.gif
[5] => E:Server\_ImageOptimizeimg estfilesOriginalImagescss3-generator.png
[6] => E:Server\_ImageOptimizeimg estfilesOriginalImagescss3-please.png
[7] => E:Server\_ImageOptimizeimg estfilesOriginalImagescss3-tools-10.png
[8] => E:Server\_ImageOptimizeimg estfilesOriginalImagesfb.jpg
[9] => E:Server\_ImageOptimizeimg estfilesOriginalImagesmysql.gif
[10] => E:Server\_ImageOptimizeimg estfilesOriginalImagessupport-browsers.png
[11] => E:Server\_ImageOptimizeimg estfilessupport-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 中递归地收集图像文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中递归地收集图像文件路径
基础教程推荐
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01