Drupal 6: Programmatically generate all images for each imagecache preset using a Drush PHP script

My Drupal photo gallery implements a total of 9 imagecache presets, but due to a PHP memory_limit cap of 90MB in my previous hosting contract, I frequently encountered issues generating images.

Since I was on a shared hosting plan, PHP was executed as CGI and a service ran on the server to kill PHP scripts that reached the PHP memory_limit. This resulted in the following error, which prevented imagecache from creating missing images and redirecting the user properly.

Premature end of script headers: index.php

After changing my hosting plan and increasing my server resources, I decided to write a PHP script to programmatically generate all the missing images. This script when executed on the shell using Drush, will get a list of every image in the files directory, and make an HTTP request for each imagecache preset URL.

<?php

// store original working directory
define('ORIGINAL_DIRECTORY', getcwd());

// define site http host
define('HTTP_HOST','pics.ericlondon.com');

// get imagecache presets data
$presets_data = imagecache_presets();

// loop through presets data and collect preset names
$presets = array();
foreach ($presets_data as $key => $value) {
  $presets[] = $value['presetname'];
}

// get a list of pictures
chdir(file_directory_path());
// NOTE: the following line uses find, grep, and sed to generate a list of image files.
// It will vary depending on which file extensions to include and which directories to ignore
$command = 'find . -type f | egrep -i "\.(gif|jpeg|jpg|png)$" | sed "s/^\.\///" | egrep -iv "^(imagecache|imagefield_thumbs)\/"';
$output = `$command`;
$files = explode("\n", trim($output));
chdir(ORIGINAL_DIRECTORY);

// loop through file list
foreach ($files as $file) {

  // loop through each preset
  foreach ($presets as $preset) {

    // define url
    $url = 'http://' . HTTP_HOST . '/' . file_directory_path() . '/imagecache/' . $preset . '/' . $file;

    // request url
    $http_request_data = drupal_http_request($url);

    // log entry
    $log_entry = $http_request_data->code . " " . $http_request_data->status_message . " " . $url;
    file_put_contents('ic_preset_log.txt', $log_entry . "\n", FILE_APPEND);

  }

}
?>

I executed this script on the shell using the following drush command:

drush scr generate_presets.php

The script logs every request and can be reviewed afterward:

$ tail ic_preset_log.txt
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/large_landscape/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/large_portrait/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/medium/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/medium_landscape/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/medium_portrait/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/thumbnail/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/thumbnail_landscape/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/thumbnail_portrait/userpics/10020/118_1881.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/large/userpics/10020/2007_07_21_003.JPG
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/large_landscape/userpics/10020/2007_07_21_003.JPG