php在递归模式下对一个目录中的所有图像运行函数
我有一个函数php在递归模式下对一个目录中的所有图像运行函数,php,directory,watermark,recursion,Php,Directory,Watermark,Recursion,我有一个函数 $result = create_watermark( 'input_file_name' ,'output_file_name'); 我有一个名为/images的目录,里面有500个图像。这些文件都被命名为images(一些未知的数字).png(全部png)。现在我想通过循环中的函数运行它们,并想输出/markedimage/images_1.png,images_2.png,images_3.png等等 我想在Ubuntu上运行这个脚本,这样我们也可以使用shell 我怎样
$result = create_watermark( 'input_file_name' ,'output_file_name');
我有一个名为/images
的目录,里面有500个图像。这些文件都被命名为images(一些未知的数字).png
(全部png)。现在我想通过循环中的函数运行它们,并想输出/markedimage/images_1.png
,images_2.png
,images_3.png
等等
我想在Ubuntu上运行这个脚本,这样我们也可以使用shell
我怎样才能做到这一点?我不确定您是否要为输出文件的编号重新编制索引。这个例子应该保留它们:
<?php
$dir = './sourceDir';
$outputDir = './markedimage';
//get files matching pattern. maybe you could use glob() instead
$files = scandir($dir);
$files = preg_grep('~^images_\d+.png$~i', $files);
//process each file
foreach ($files as $file) {
create_watermark( $dir . '/' . $file, $outputDir . '/' . $file);
}
未经测试,但这也应该有效:
// Iterate over all filesystem objects in /images
foreach( new DirectoryIterator('/images') as $file ) {
// check if item is a readable file
if( $file->isFile() && $file->isReadable() ) {
// give debug message so we know what the script is doing
echo "Watermarking $file \n";
// call your function
create_watermark(
// argument 1 is the full path to the image
$file->getPathname(),
// argument 2 is the destination folder plus the filename w/out path
'/markedimage/' . $file->getFilename()
);
// tell us if it is not a readable file
} else {
echo "Skipped $file \n";
}
}
如果文件夹中的文件不是png文件,则可以使用而不是DirectoryIterator,但这需要PHP5.3
看
我用了第二个答案
工作一
<?php
foreach (glob("*.png") as $filename) {
create_watermark($filename, '/watermarked_dir/' . $filename);
}
?>
这个答案没有任何变化:D thankx,但我不能投票赞成,因为我没有注册
@1我没能让它工作,但我想如果花点力气的话,它会工作的
<?php
$dir = './sourceDir';
$outputDir = './markedimage';
//get files matching pattern. maybe you could use glob() instead
$files = scandir($dir);
$files = preg_grep('~^images_\d+.png$~i', $files);
//process each file
foreach ($files as $file) {
create_watermark( $dir . '/' . $file, $outputDir . '/' . $file);
}
?>
感谢guyz,他试着帮我分配了过去5个小时我一直在试着这么做,但没能在5分钟内解决
:D谢谢XXX
史蒂夫在这里读我的回答
<?php
$dir = './sourceDir';
$outputDir = './markedimage';
//get files matching pattern. maybe you could use glob() instead
$files = scandir($dir);
$files = preg_grep('~^images_\d+.png$~i', $files);
//process each file
foreach ($files as $file) {
create_watermark( $dir . '/' . $file, $outputDir . '/' . $file);
}
?>