You are here

imagecache_proportions.module in Imagecache Proportions 7

Same filename and directory in other branches
  1. 8 imagecache_proportions.module
  2. 6 imagecache_proportions.module

Field formatter for image fields that allows the user to select between 3 different image styles depending on the proportions of the original image uploaded. One style would be squared for more or less squared images, another for wider images and the last one for taller images.

File

imagecache_proportions.module
View source
<?php

/**
 * @file
 * Field formatter for image fields that allows the user to select between 3
 * different image styles depending on the proportions of the original
 * image uploaded. One style would be squared for more or less squared images,
 * another for wider images and the last one for taller images.
 */

/**
 * Implements hook_field_formatter_info().
 */
function imagecache_proportions_field_formatter_info() {
  return array(
    'vertical_horizontal' => array(
      'label' => t('Imagecache proportions'),
      'field types' => array(
        'image',
      ),
      'settings' => array(
        'horizontal_preset' => '',
        'vertical_preset' => '',
        'style' => '',
        'squared_preset' => '',
        'looseness' => 0,
        'enable_link' => '',
        'modal_style' => '',
        'colorbox' => array(
          'colorbox_image_style' => '',
          'colorbox_gallery' => 'post',
          'colorbox_gallery_custom' => '',
          'colorbox_caption' => 'auto',
          'colorbox_caption_custom' => '',
        ),
        'shadowbox' => array(
          'image_link' => '',
          'gallery' => '',
          'compact' => '',
          'title' => '',
        ),
      ),
      'description' => t('Display the image in horizontal if it is more wide than high or in vertical if it is more high than wide.'),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function imagecache_proportions_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $form = array();
  $options = array(
    t('None'),
  );

  // get a list of all styles for our form options
  foreach (image_styles() as $id => $style) {
    $options[$id] = $id;
  }
  $form['horizontal_preset'] = array(
    '#type' => 'select',
    '#title' => t('Select the horizontal style'),
    '#description' => t('Style used when the image is wider than higher.'),
    '#options' => $options,
    '#default_value' => $settings['horizontal_preset'],
  );
  $form['vertical_preset'] = array(
    '#type' => 'select',
    '#title' => t('Select the vertical style'),
    '#description' => t('Style used when the image is higher than wider.'),
    '#options' => $options,
    '#default_value' => $settings['vertical_preset'],
  );
  $form['squared_preset'] = array(
    '#type' => 'select',
    '#title' => t('Select the squared style'),
    '#description' => t('Style used when the image is equally wider and higher.'),
    '#options' => $options,
    '#default_value' => $settings['squared_preset'],
  );
  $form['looseness'] = array(
    '#type' => 'textfield',
    '#size' => 10,
    '#title' => t('Select the looseness to consider an image squared'),
    '#description' => t('Number of pixels that a image can be wider than higher (or viceversa) to be considered squared.'),
    '#default_value' => $settings['looseness'],
  );
  $link_options = array(
    'none' => t('No link'),
    'entity' => t('Link to the entity (e.g node, taxonomy term...)'),
    'image' => t('Link to image'),
  );

  // If some fancy modal-lightbox module exists, we allow the user to select it.
  if (module_exists('colorbox')) {
    $link_options['colorbox'] = t('Modal window using colorbox');
  }

  // If some fancy modal-lightbox module exists, we allow the user to select it.
  if (module_exists('shadowbox')) {
    $link_options['shadowbox'] = t('Modal window using shadowbox');
  }
  $form['enable_link'] = array(
    '#type' => 'select',
    '#title' => t('Select the type of link of the image'),
    '#description' => t('Type of link of the image, if image selected and one of the js image popups is enabled (colorbox, shadowbox, lightbox2...) the image will open in a popup.'),
    '#options' => $link_options,
    '#default_value' => $settings['enable_link'],
  );
  if (module_exists('shadowbox')) {
    $form['shadowbox'] = array(
      '#type' => 'fieldset',
      '#title' => t('Shadowbox options'),
      '#states' => array(
        'visible' => array(
          ':input[name$="[settings_edit_form][settings][enable_link]"]' => array(
            'value' => 'shadowbox',
          ),
        ),
      ),
    );
    $form['shadowbox'] += imagecache_proportions_shadowbox_form($field, $instance, $view_mode, $form, $form_state);
  }
  if (module_exists('colorbox')) {
    $form['colorbox'] = array(
      '#type' => 'fieldset',
      '#title' => t('Colorbox options'),
      '#states' => array(
        'visible' => array(
          ':input[name$="[settings_edit_form][settings][enable_link]"]' => array(
            'value' => 'colorbox',
          ),
        ),
      ),
    );
    $form['colorbox'] += imagecache_proportions_colorbox_form($field, $instance, $view_mode, $form, $form_state);
  }
  return $form;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function imagecache_proportions_field_formatter_settings_summary($field, $instance, $view_mode) {
  return t('Configure the styles for the image');
}

/**
 * Implements hook_field_formatter_view().
 */
function imagecache_proportions_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  // if there are no images, don't do anything else.
  if (empty($items)) {
    return;
  }
  $settings = $display['settings'];
  if (module_exists('shadowbox') && $settings['enable_link'] == 'shadowbox') {
    $shadowbox_enabled_path = _shadowbox_activation() && variable_get('shadowbox_enabled', TRUE);
    switch ($settings['shadowbox']['gallery']) {
      case 'page':
        $gallery_id = 'gallery';
        break;
      case 'field':
        $gallery_id = $field['field_name'];
        break;
      case 'nid':
        $info = entity_get_info($entity_type);
        $id = $info['entity keys']['id'];
        $gallery_id = "{$entity_type}-{$entity->{$id}}";
        break;
      default:
        $gallery_id = "";
        break;
    }
  }
  foreach ($items as $delta => $item) {
    $extra = array();
    $theme = 'image_formatter';
    $size = getimagesize($item['uri']);
    $proportions = imagecache_proportions_calculate_proportions($size[0], $size[1], $settings['looseness']);
    switch ($settings['enable_link']) {

      // Node is still here for legacy purposes.
      case 'node':
      case 'entity':
        $uri = entity_uri($entity_type, $entity);
        $extra['#image_style'] = $settings[$proportions];
        break;
      case 'image':
        $uri = array(
          'path' => file_create_url($item['uri']),
          'options' => array(),
        );
        $extra['#image_style'] = $settings[$proportions];
        break;
      case 'colorbox':
        $theme = 'colorbox_image_formatter';
        $settings['colorbox']['colorbox_node_style'] = $settings[$proportions];
        $extra['#display_settings'] = $settings['colorbox'];
        $extra['#entity'] = $entity;
        $extra['#entity_type'] = $entity_type;
        break;
      case 'shadowbox':
        switch ($settings['shadowbox']['title']) {
          case 'alt':
            $title = $item['alt'];
            break;
          case 'title':
            $title = $item['title'];
            break;
          case 'node':
            $title = $entity->title;
            break;
          default:
            $title = '';
            break;
        }
        $theme = 'shadowbox_formatter';
        $extra['#itemid'] = $delta;
        $extra['#image_style'] = $settings[$proportions];
        $extra['#image_link'] = $settings['shadowbox']['image_link'];
        $extra['#title'] = $title;
        $extra['#gallery'] = $gallery_id;
        $extra['#compact'] = $settings['shadowbox']['compact'];
        if ($shadowbox_enabled_path) {
          $extra['#attached']['library'][] = array(
            'shadowbox',
            'shadowbox',
          );
        }
        break;
      default:
        $extra['#image_style'] = $settings[$proportions];
        break;
    }
    $element[$delta] = array(
      '#theme' => $theme,
      '#item' => $item,
      '#node' => $entity,
      '#field' => $field,
      '#path' => isset($uri) ? $uri : '',
    );
    $element[$delta] += $extra;
  }
  return $element;
}

/**
 * Helper function to guess if the image is wider, higher or "squarer" taking a look to the looseness.
 */
function imagecache_proportions_calculate_proportions($width, $height, $looseness) {
  if ($width == $height || abs($width - $height) <= $looseness) {
    return 'squared_preset';
  }
  if ($width > $height) {
    return 'horizontal_preset';
  }
  if ($height > $width) {
    return 'vertical_preset';
  }
}

/**
 * Retrieves and processes colorbox formatter settings.
 */
function imagecache_proportions_colorbox_form($field, $instance, $view_mode, $form, &$form_state) {
  $instance['display'][$view_mode]['settings'] = $instance['display'][$view_mode]['settings']['colorbox'];
  $form = colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
  $form['colorbox_caption_custom']['#states']['visible'] = array(
    ':input[name$="[settings_edit_form][settings][colorbox][colorbox_caption]"]' => array(
      'value' => 'custom',
    ),
  );
  $form['colorbox_gallery_custom']['#states']['visible'] = array(
    ':input[name$="[settings_edit_form][settings][colorbox][colorbox_gallery]"]' => array(
      'value' => 'custom',
    ),
  );
  unset($form['colorbox_node_style']);
  return $form;
}

/**
 * Retrieves and processes shadowbox formatter settings.
 */
function imagecache_proportions_shadowbox_form($field, $instance, $view_mode, $form, &$form_state) {
  $instance['display'][$view_mode]['settings'] = $instance['display'][$view_mode]['settings']['shadowbox'];
  $form = shadowbox_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
  unset($form['image_style']);
  return $form;
}

Functions