php以递归模式对一个目录中的所有图像运行函数

发布于 2024-08-29 15:04:37 字数 400 浏览 2 评论 0原文

我有一个函数,

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

我有一个名为 /images 的目录,其中包含 500 张图像。这些文件均名为 images_(some_unknown_numbers).png(均为 png)。现在我想通过循环中的函数运行它们,并希望输出 /markedimage/images_1.pngimages_2.pngimages_3.png等等。

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

我该怎么做?

I have a function

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

I have directory called /images with 500 images in it. The files are all named images_(some_unknown_numbers).png (all png). Now I want run them through a function in a loop and want to output /markedimage/images_1.png, images_2.png, images_3.png and so on.

I want to run this script on Ubuntu so we can use shell too

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妖妓 2024-09-05 15:04:37
<?php

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

foreach (glob("*.png") as $filename) {
    create_watermark($filename, '/watermarked_dir/' . $filename);
}
梦中楼上月下 2024-09-05 15:04:37

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

<?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);
}

I'm not sure whether you want to re-index the numbers for the output files. This example should preserve them:

<?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);
}
顾冷 2024-09-05 15:04:37

我使用了第二个答案

<?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);
}

?>

谢谢尝试并帮助我分配的家伙,我在过去 5 小时内尝试这样做,但失败了,五分钟内就解决了

:D 谢谢xxx

Steve

i used 2nd answer

working one

<?php

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

this answer worked without any change :D thankx but i can't vote for it because i m not registered

@1 i m failed to make it working but this i think if put some effort on it it shud work

<?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);
}

?>

thankx to guyz who tried and helped me allot i was try to do this from last 5 hours but failed her it solved in five minutes

:D thankxxx

Steve

岁月无声 2024-09-05 15:04:37

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

// 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。

请参阅

Untested but this should work, too:

// 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";
    }
}

If there is files in the folder that are not png files, you can use the GlobIterator instead of the DirectoryIterator, but that would require PHP5.3.

See

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文