2

我有一个功能

 $result = create_watermark( 'input_file_name' ,'output_file_name');

我有一个目录/images,里面有 500 个图像。这些文件都被命名images_(some_unknown_numbers).png(都是png)。现在我想通过循环中的函数运行它们,并希望输出/markedimage/images_1.png,等。images_2.pngimages_3.png

我想在 Ubuntu 上运行这个脚本,这样我们也可以使用 shell

我怎样才能做到这一点?

4

4 回答 4

3
<?php

foreach (glob("*.png") as $filename) {
    create_watermark($filename, '/watermarked_dir/' . $filename);
}
于 2010-04-23T13:08:16.873 回答
0

我用了第二个答案

工作一

<?php

foreach (glob("*.png") as $filename) {
    create_watermark($filename, '/watermarked_dir/' . $filename);
}
?>

这个答案没有任何改变:D谢谢,但我不能投票,因为我没有注册

@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 小时开始尝试这样做但失败了,它在五分钟内解决了

:D 谢谢xxx

史蒂夫

于 2010-04-23T13:27:08.233 回答
0

未经测试,但这也应该有效:

// 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 文件的文件,您可以使用GlobIterator代替 DirectoryIterator,但这需要 PHP5.3。

于 2010-04-23T13:08:33.723 回答
0

我不确定您是否要重新索引输出文件的数字。这个例子应该保留它们:

<?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);
}
于 2010-04-23T12:59:45.670 回答