When imagecache doesn't find an image because the file doesn't exist it returns a 404 error. I want to change this behavior so that if an image is not found a default image is returned. I noticed this is what happens when a 403 error is generated.
The reason for this new behavior is that in many cases when you do development it is often the case that you might use the production database which may have thousands of images while your development environment may have just a few. This generates two problems: 1) its difficult to check the layout of the pages when you do development because many images are missing and 2) when you view the site if the page has a lot of missing images apache spends a lot of time handling 404 errors increasing the page load time.
I see two ways of fixing this behavior:
1) Insert code similar to the 403 error, namely:
function _imagecache_cache($presetname, $path) {
// ...
// Check if the path to the file exists.
if (!is_file($src) && !is_file($src = file_create_path($src))) {
// if there is a 404 image, display it.
$accesspath = file_create_path('imagecache/'. $preset .'.403.png');
if (is_file($accesspath)) {
imagecache_transfer($accesspath);
exit;
}
watchdog('imagecache', '404: Unable to find %image ', array('%image' => $src), WATCHDOG_ERROR);
header("HTTP/1.0 404 Not Found");
exit;
}
2) Something similar to 1) but just provide a singe large image and apply the imagecache preset to this image. The code would be something along the lines of:
function _imagecache_cache($presetname, $path) {
// ...
// Check if the path to the file exists.
if (!is_file($src) && !is_file($src = file_create_path($src))) {
// if there is a 404 image apply the preset to it
$accesspath = file_create_path('imagecache/imagecache404.png');
if (is_file($accesspath)) {
$src = $accesspath;
}
else {
watchdog('imagecache', '404: Unable to find %image ', array('%image' => $src), WATCHDOG_ERROR);
header("HTTP/1.0 404 Not Found");
exit;
}
}
If you are interested in this I would be happy to submit a patch and maintain this feature.
Comments
Comment #1
fizk commentedPlease reopen if this is still an issue with ImageCache 6.x-2.0-rc1.