function media_acquiadam_image_style_deliver in Media: Acquia DAM 7
Implementation of image_style_deliver().
Allows serving images from outside of the file_managed table.
Parameters
string $styleName: The image style being requested.
AcquiaDAM_Assets_AbstractAsset $asset: The asset to view an image of.
Return value
int A menu response code.
1 string reference to 'media_acquiadam_image_style_deliver'
- media_acquiadam_menu in ./
media_acquiadam.module - Implements hook_menu().
File
- includes/
media_acquiadam.image.inc, line 21 - Code for working with DAM image styles.
Code
function media_acquiadam_image_style_deliver($styleName, AcquiaDAM_Assets_AbstractAsset $asset) {
// Drupal does not allow multiple _load style menu arguments so we need to
// manually load this here.
$style = image_style_load($styleName);
if (empty($style)) {
return MENU_NOT_FOUND;
}
try {
$image_uri = $asset
->getThumbnailUrl();
if (empty($image_uri)) {
return MENU_NOT_FOUND;
}
// Prime image styles before loading below.
media_acquiadam_create_asset_image_derivatives($asset);
} catch (Exception $x) {
watchdog_exception('media_acquiadam', $x);
return MENU_NOT_FOUND;
}
$derivative_uri = media_acquiadam_image_style_path($asset['id'], $style, $image_uri);
// The rest of the logic is the same as contined within the original
// remote_stream_wrapper function.
// Don't start generating the image if the derivative already exists or if
// generation is in progress in another thread.
$lock_name = 'image_style_deliver:' . $style['name'] . ':' . drupal_hash_base64($image_uri);
if (!is_file($derivative_uri)) {
$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 = is_file($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
if (!empty($lock_acquired)) {
lock_release($lock_name);
}
if ($success) {
$image = image_load($derivative_uri);
file_transfer($image->source, [
'Content-Type' => $image->info['mime_type'],
'Content-Length' => $image->info['file_size'],
]);
}
else {
watchdog('media_acquiadam', 'Unable to generate the derived image located at %path.', [
'%path' => $derivative_uri,
]);
drupal_add_http_header('Status', '500 Internal Server Error');
print t('Error generating image.');
drupal_exit();
}
}