You are here

function bynder_embed_url in Bynder 7

Create the embed url for a fragment.

@todo add support for type and effect

Parameters

string $fragment_id idHash of entity.:

string $type web, mini, thul:

array $type effect:

1 call to bynder_embed_url()
BynderMediaStreamWrapper::getWebimagePath in includes/BynderMediaStreamWrapper.inc
Function getWebimagePath().

File

includes/bynder.utils.inc, line 17
utility functions for the bynder module

Code

function bynder_embed_url($idHash, $filename, $imageStyle = false) {
  $bynderEntity = db_select('bynder_media_entity', 'b')
    ->fields('b')
    ->condition('b.bynder_hash_id', $idHash, '=')
    ->execute()
    ->fetchAll();
  if (!empty($bynderEntity)) {
    $bynderStyles = unserialize(variable_get('bynder_derivatives'));
    $imageDerivatives = unserialize($bynderEntity[0]->derivatives);
    $styleName = str_replace('bynder_', '', $imageStyle);

    // Process custom Bynder derivatives
    if (in_array($styleName, $bynderStyles)) {
      if (isset($imageDerivatives[$styleName])) {
        return $imageDerivatives[$styleName];
      }
    }
    else {
      $imageStyles = image_styles();
      $effects = isset($imageStyles[$imageStyle]['effects']) ? $imageStyles[$imageStyle]['effects'] : false;
      if ($effects) {
        foreach ($effects as $effect) {

          // We return straightaway because we only support one at the time.
          if ($effect['effect callback'] == 'bynder_custom_resolution_effect') {
            $derivativeUrl = customResolutionUrl($idHash, $effect);
            if ($derivativeUrl != 'pending' && $derivativeUrl !== false) {
              return $derivativeUrl;
            }
            elseif (user_access('administer media bynder')) {
              if ($derivativeUrl == 'pending') {
                drupal_set_message(t('The image style: @imageStyle for image @filename is being
                                    generated. The next time you refresh the page it should return the correct image style.', array(
                  '@imageStyle' => $imageStyles[$imageStyle]['label'],
                  '@filename' => $filename,
                )), 'warning');
              }
              else {
                drupal_set_message(t('Unable to generate image style: @imageStyle for image @filename.
                                    Make sure this asset is set to "Public" and the custom derivative is properly set up in your Bynder portal
                                    and try again.', array(
                  '@imageStyle' => $imageStyles[$imageStyle]['label'],
                  '@filename' => $filename,
                )), 'warning');
              }
            }
          }
        }
      }
    }

    // Check if we have the webimage default derivative for that image.
    if (isset($imageDerivatives['webimage'])) {
      drupal_set_message(t('No valid image style found for image @filename, it has been defaulted to the original image style.', array(
        '@filename' => $filename,
      )), 'warning');
      return $imageDerivatives['webimage'];
    }
  }
  drupal_set_message(t('No valid image style found for image @filename, please check your configuration.', array(
    '@filename' => $filename,
  )), 'warning');

  // If everything else fails we default to image not found.
  return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'bynder') . '/assets/no-image.png';
}