You are here

bynder.formatters.inc in Bynder 7

bynder/includes/bynder.formatters.inc Formatters for Media: Bynder.

File

includes/bynder.formatters.inc
View source
<?php

/**
 * @file
 * bynder/includes/bynder.formatters.inc
 * Formatters for Media: Bynder.
 */

/**
 * Implements hook_file_formatter_info().
 * $info: Array of information on file formatters exposed by hook_file_formatter_info() implementations.
 */
function bynder_file_formatter_info_alter(&$info) {
  $info['file_bynder_image'] = array(
    'label' => t('Bynder Preview Image'),
    'file types' => array(
      'image',
    ),
    'default settings' => array(
      'image_style' => '',
    ),
    'view callback' => 'bynder_file_formatter_image_view',
    'settings callback' => 'bynder_file_formatter_image_settings',
    'mime types' => array(
      'image/bynder',
    ),
  );
  return $info;
}

/**
 * Format image view.
 */
function bynder_file_formatter_image_view($file, $display, $langcode) {
  $scheme = file_uri_scheme($file->uri);
  if ($scheme == 'bynder') {
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
    $image_style = $display['settings']['image_style'];
    $element = array(
      '#theme' => 'image',
      '#path' => $wrapper
        ->getWebImagePath($image_style, $file->filename),
      '#style_name' => $image_style,
      '#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
    );
    return $element;
  }
}

/**
 * Implements hook_file_formatter_FORMATTER_settings().
 */
function bynder_file_formatter_image_settings($form, &$form_state, $settings) {
  $element = array();
  $element['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#options' => image_style_options(false),
    '#default_value' => $settings['image_style'],
    '#empty_option' => t('None (original image)'),
  );
  return $element;
}

/**
 * Implements hook_image_effect_info().
 * declare effect callbacks
 */
function bynder_image_effect_info() {
  return array(
    'bynder_custom_resolution_crop' => array(
      'label' => t('Bynder custom resolution crop'),
      'help' => t('Crops to the exact dimensions specified. Please note that in order for this function to work you need to call this effect only as it uses an external image from Bynder. Due to this external nature additional drupal effects can\'t be applied'),
      'effect callback' => 'bynder_custom_resolution_effect',
      'dimensions callback' => 'bynder_custom_resolution_dimensions',
      'form callback' => 'bynder_image_effect_form',
    ),
    'bynder_custom_resolution_extend' => array(
      'label' => t('Bynder custom resolution extend'),
      'help' => t('Resizes the image and extends to return the dimensions specified. Please note that in order for this function to work you need to call this effect only as it uses an external image from Bynder. Due to this external nature additional drupal effects can\'t be applied'),
      'effect callback' => 'bynder_custom_resolution_effect',
      'dimensions callback' => 'bynder_custom_resolution_dimensions',
      'form callback' => 'bynder_image_effect_form',
    ),
    'bynder_custom_resolution_resize' => array(
      'label' => t('Bynder custom resolution resize'),
      'help' => t('Resizes maintaining aspect ratio. Please note that in order for this function to work you need to call this effect only as it uses an external image from Bynder. Due to this external nature additional drupal effects can\'t be applied'),
      'effect callback' => 'bynder_custom_resolution_effect',
      'dimensions callback' => 'bynder_custom_resolution_dimensions',
      'form callback' => 'bynder_image_effect_form',
    ),
  );
}

/**
 * Form structure for the image scale form.
 *
 * Note that this is not a complete form, it only contains the portion of the
 * form for configuring the scale options. Therefore it does not not need to
 * include metadata about the effect, nor a submit button.
 *
 * @param $data
 *   The current configuration for this scale effect.
 */
function bynder_image_effect_form($data) {
  $form = image_resize_form($data);
  $form['#element_validate'] = array(
    'bynder_image_effect_form_validate',
  );
  $form['width']['#required'] = true;
  $form['height']['#required'] = true;
  return $form;
}
function bynder_image_effect_form_validate($element, &$form_state) {
  if (!variable_get('bynder_custom_derivative')) {
    form_error($element, t('You need to set up the <a href="' . url(BYNDER_SETTINGS_URL, array(
      'absolute' => true,
    )) . '" class="oauth-link" target="_blank">Bynder custom derivative name</a>
                before you can add image styles.'));
  }
}

/**
 * Implements callback 'effect callback'
 * will return the image from the API, instead of modifying the image as intended
 */
function bynder_custom_resolution_effect(&$image, $data) {
  if (file_uri_scheme($image->source) == 'bynder') {
    if (!bynder_custom_resolution($image)) {
      watchdog('image', 'The image %file could not be colorize because the imagefilter() function is not available in this PHP installation.', array(
        '%file' => $image->source,
      ), WATCHDOG_ERROR);
      return FALSE;
    }
  }
  return true;
}

/**
 * Filter effect function
 */
function bynder_custom_resolution(stdClass $image) {
  return imagefilter($image->resource, null);
}
function bynder_custom_resolution_dimensions(array &$dimensions, array $data) {
  if ($dimensions['width'] && $dimensions['height']) {
    $dimensions['width'] = $data['width'];
    $dimensions['height'] = $data['height'];
  }
}

Functions

Namesort descending Description
bynder_custom_resolution Filter effect function
bynder_custom_resolution_dimensions
bynder_custom_resolution_effect Implements callback 'effect callback' will return the image from the API, instead of modifying the image as intended
bynder_file_formatter_image_settings Implements hook_file_formatter_FORMATTER_settings().
bynder_file_formatter_image_view Format image view.
bynder_file_formatter_info_alter Implements hook_file_formatter_info(). $info: Array of information on file formatters exposed by hook_file_formatter_info() implementations.
bynder_image_effect_form Form structure for the image scale form.
bynder_image_effect_form_validate
bynder_image_effect_info Implements hook_image_effect_info(). declare effect callbacks