You are here

function bynder_batch_reload_derivatives_process in Bynder 7

Processes the derivative list and sets new image styles for each one.

1 string reference to 'bynder_batch_reload_derivatives_process'
bynder_batch_reload_derivatives in includes/bynder.admin.inc
Batch callback to fetch derivative information.

File

includes/bynder.admin.inc, line 185

Code

function bynder_batch_reload_derivatives_process(&$context) {
  $context['message'] = "Updating derivatives data.";
  variable_set('bynder_derivatives', '');

  // Hardcode the default derivatives so we don't need an extra API call.
  $thumbnails = [
    'mini',
    'webimage',
    'thul',
  ];
  try {
    $derivatives = BynderMediaApi::getBynderApi()
      ->getDerivatives();
    if ($derivatives) {
      foreach ($derivatives as $derivative) {
        $thumbnails[] = $derivative['prefix'];
      }
    }
    $customDerivative = variable_get('bynder_custom_derivative');
    foreach ($thumbnails as $derivative) {
      if (!image_style_load('bynder_' . $derivative)) {
        $context['message'] = "Creating image style for derivative: {$derivative}";
        $style = image_style_save(array(
          'name' => 'bynder_' . $derivative,
          'label' => t('Bynder derivative: ' . $derivative),
        ));
        if ($derivative == $customDerivative) {
          $effect = array(
            'name' => 'bynder_custom_resolution_resize',
            'data' => array(
              'width' => 400,
              'height' => 400,
            ),
            'isid' => $style['isid'],
          );
          image_effect_save($effect);
        }
      }
    }
  } catch (Exception $e) {
    watchdog('bynder', $e
      ->getMessage());
    drupal_set_message(t("There was an error while saving new image styles."), 'error');
    $context['finished'] = 1;
  }
  variable_set('bynder_derivatives', serialize($thumbnails));
}