You are here

function media_acquiadam_create_asset_image_derivatives in Media: Acquia DAM 7

Generate all image derivatives for an asset.

Parameters

AcquiaDAM_Assets_AbstractAsset $asset: The asset to generate derivatives for.

Return value

bool TRUE on success, FALSE on failure.

1 call to media_acquiadam_create_asset_image_derivatives()
media_acquiadam_image_style_deliver in includes/media_acquiadam.image.inc
Implementation of image_style_deliver().

File

includes/media_acquiadam.image.inc, line 93
Code for working with DAM image styles.

Code

function media_acquiadam_create_asset_image_derivatives(AcquiaDAM_Assets_AbstractAsset $asset) {
  try {
    $thumbnail = $asset
      ->getThumbnailUrl();
  } catch (Exception $x) {
    watchdog_exception('media_acquiadam', $x);
    return FALSE;
  }
  $derivatives = [];
  $styles = image_styles();
  foreach ($styles as $style) {
    $uri = media_acquiadam_image_style_path($asset['id'], $style, $thumbnail);

    // Only try and create derivatives that do not exist.
    if (!is_file($uri)) {
      $derivatives[] = [
        'uri' => $uri,
        'style' => $style,
      ];
    }
  }
  if (!empty($derivatives)) {

    // We only want to make this call if there is work to do, otherwise it
    // severely impacts page load performance.
    $image_uri = system_retrieve_file($thumbnail, file_directory_temp());
    if (!empty($image_uri)) {
      foreach ($derivatives as $derivative) {
        image_style_create_derivative($derivative['style'], $image_uri, $derivative['uri']);
      }
      file_unmanaged_delete_recursive($image_uri);
    }
    else {
      watchdog('media_acquiadam', 'Unable to retrieve @thumbnail for @id to create image derivatives.', [
        '@thumbnail' => $thumbnail,
        '@id' => $asset['id'],
      ], WATCHDOG_NOTICE);
      return FALSE;
    }
  }
  return TRUE;
}