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.phpAfter 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 -ir "\.(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.phpThe 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_l...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/large_p...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/medium/...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/medium_...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/medium_...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/thumbna...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/thumbna...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/thumbna...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/large/u...
200 OK http://pics.ericlondon.com/sites/ericlondon.com/files/imagecache/large_landscape/userpics/10020/2007_07_21_003.JPG









