I have this PHP code that resizes images perfectly: smart_resize_image(path/to/image.jpg);
Now I want to run it on all images inside a folder say parent
and all of its subfolders child1
, child2
etc.. every hour.
Is this something i could do on a share host? Yes, I can setup cron but I'm not sure how to run it on folders and its subfolders.
Thanks!
You're looking for a recursive algorithm, to drill down through folders:
resizing( '/folder' );
function resizing( $folder )
{
foreach( scandir( $folder ) as $file )
{
$filepath = $folder . '/' . $file;
if( preg_match( '/^\./', $file ) ) continue; // not . and ..
if( is_dir( $filepath ) ) resizing( $filepath ); // Send it off to drill - with this function!!
// It's time to resize
smart_resize_image( $filepath );
}
}
you may lookup for files using glob function and then loop the results through your resize method. Be aware as it must be done by cron as it probably will take more time that allowed in a webserver script context.
You have to build a recursive function. It is quite simple. Just start reading first directory and if you find any directories for each make a callback to itself to parse each folder. It's not a very consuming function.
See this: http://www.codingforums.com/showthread.php?t=71882
Dump all file matching your desired extensions to an array then run your resize function for them. To avoid any memory issues you can call the resize via CLI. Just make a file that will take the file path as an argument (not argument not get) and call it with shell_exec(). This way it will run them nicely one by one in a separate process without affecting the main one.
Multi-processing in PHP .. hehe