function _imagecache_cache in ImageCache 6.2
Same name and namespace in other branches
- 5.2 imagecache.module \_imagecache_cache()
Handle request validation and responses to ImageCache requests.
ImageCache generate images but not send them to a browser.
See also
imagecache_generate_image() if you're writing code that needs to have
2 calls to _imagecache_cache()
- imagecache_cache in ./
imagecache.module - callback for handling public files imagecache requests.
- imagecache_cache_private in ./
imagecache.module - callback for handling private files imagecache requests
File
- ./
imagecache.module, line 437 - Dynamic image resizer and image cacher.
Code
function _imagecache_cache($presetname, $path) {
if (!($preset = imagecache_preset_by_name($presetname))) {
// Send a 404 if we don't know of a preset.
header("HTTP/1.0 404 Not Found");
exit;
}
// umm yeah deliver it early if it is there. especially useful
// to prevent lock files from being created when delivering private files.
$dst = imagecache_create_path($preset['presetname'], $path);
if (is_file($dst)) {
imagecache_transfer($dst);
}
// preserve path for watchdog.
$src = $path;
// Check if the path to the file exists.
if (!is_file($src) && !is_file($src = file_create_path($src))) {
watchdog('imagecache', '404: Unable to find %image ', array(
'%image' => $src,
), WATCHDOG_ERROR);
header("HTTP/1.0 404 Not Found");
exit;
}
// Bail if the requested file isn't an image you can't request .php files
// etc...
if (!getimagesize($src)) {
watchdog('imagecache', '403: File is not an image %image ', array(
'%image' => $src,
), WATCHDOG_ERROR);
header('HTTP/1.0 403 Forbidden');
exit;
}
$lockfile = file_directory_temp() . '/' . $preset['presetname'] . basename($src);
if (file_exists($lockfile)) {
watchdog('imagecache', 'ImageCache already generating: %dst, Lock file: %tmp.', array(
'%dst' => $dst,
'%tmp' => $lockfile,
), WATCHDOG_NOTICE);
// 307 Temporary Redirect, to myself. Lets hope the image is done next time around.
header('Location: ' . request_uri(), TRUE, 307);
exit;
}
touch($lockfile);
// register the shtdown function to clean up lock files. by the time shutdown
// functions are being called the cwd has changed from document root, to
// server root so absolute paths must be used for files in shutdown functions.
register_shutdown_function('file_delete', realpath($lockfile));
// check if deriv exists... (file was created between apaches request handler and reaching this code)
// otherwise try to create the derivative.
if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
imagecache_transfer($dst);
}
// Generate an error if image could not generate.
watchdog('imagecache', 'Failed generating an image from %image using imagecache preset %preset.', array(
'%image' => $path,
'%preset' => $preset['presetname'],
), WATCHDOG_ERROR);
header("HTTP/1.0 500 Internal Server Error");
exit;
}