image_style_generate()Menu callback; Given a style and image path, generate a derivative.
This menu callback is always served after checking a token to prevent generation of unnecessary images. After generating an image transfer it to the requesting agent via file_transfer().
modules/image/image.module, line 585
<?php
function image_style_generate() {
$args = func_get_args();
$style = array_shift($args);
$style_name = $style['name'];
$scheme = array_shift($args);
$path = implode('/', $args);
$path = $scheme . '://' . $path;
$path_md5 = md5($path);
$destination = image_style_path($style['name'], $path);
// Check that it's a defined style and that access was granted by
// image_style_url().
if (!$style || !cache_get('access:' . $style_name . ':' . $path_md5, 'cache_image')) {
drupal_access_denied();
drupal_exit();
}
// Don't start generating the image if the derivate already exists or if
// generation is in progress in another thread.
$lock_name = 'image_style_generate:' . $style_name . ':' . $path_md5;
if (!file_exists($destination)) {
$lock_acquired = lock_acquire($lock_name);
if (!$lock_acquired) {
// Tell client to retry again in 3 seconds. Currently no browsers are known
// to support Retry-After.
drupal_add_http_header('Status', '503 Service Unavailable');
drupal_add_http_header('Retry-After', 3);
print t('Image generation in progress. Try again shortly.');
drupal_exit();
}
}
// Try to generate the image, unless another thread just did it while we were
// acquiring the lock.
$success = file_exists($destination) || image_style_create_derivative($style, $path, $destination);
if (!empty($lock_acquired)) {
lock_release($lock_name);
}
if ($success) {
$image = image_load($destination);
file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
}
else {
watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $destination));
drupal_add_http_header('Status', '500 Internal Server Error');
print t('Error generating image.');
drupal_exit();
}
}
?>