Batch processing tutorial

In this example we will process many photos from directory and save information about them into the database.

I assume in this tutorial that user images are stored in /users/USER_ID/ directory and thumbs are will be placed in /users/USER_ID/thumbs/

<?php
function callfunc(& $thumb)
{
    global 
$user;

    
// catch errors
    
    
try
    {
        
// square thumb for every image
        // save it to the user's directory
        
$thumb->thumbSquare(100)->save('/users/'.$user->id.'/thumbs/'.$thumb->filename;
        
        
// save information to the database
        
$sql=mysql_real_escape_string('insert into `user_photos` (`photo`) values ("'.$thumb->filename.'")');
        
mysql_query($sql);
        
        
// done
        
return true;
    }
    catch (
Exception $e)
    {
        
// if error occured
        
return 'Error occured for '.$thumb->filename.' - '.$e->getMessage();
    }
}

// run Thumbnailer in batch mode
// make thumb for every jpg, gif and png image
$results=Thumbnailer::batch('callfunc''/users/'.$user->id.'/*.{jpg,gif,png}');

// check for errors
foreach($results as $result)
{
    if (
$result !== true)
    {
        echo 
$result.'<br />';
    }
}

?>