You are here

function textimage_background_effect_dimensions in Textimage 7.3

Implements 'textimage_background' image dimensions callback.

1 string reference to 'textimage_background_effect_dimensions'
textimage_image_effect_info in ./textimage.module
Implements hook_image_effect_info().

File

effects/textimage_background.inc, line 577
Implementation of the 'textimage_background' image effect.

Code

function textimage_background_effect_dimensions(array &$dimensions, array $data) {

  // Merge input data with effect defaults.
  $data = drupal_array_merge_deep(_textimage_background_effect_defaults(), $data);

  // If exact size WxH, set and return.
  if ($data['exact']['width'] and $data['exact']['height']) {
    $dimensions['width'] = $data['exact']['width'];
    $dimensions['height'] = $data['exact']['height'];
    return;
  }

  // Fetches WxH of the background image.
  switch ($data['background_image']['mode']) {
    case 'select':
      if (!isset($data['background_image']['uri'])) {
        _textimage_diag(t('Textimage could not find an image to load.'), WATCHDOG_ERROR, __FUNCTION__);
        return;
      }
      $new_image = image_load($data['background_image']['uri']);
      if (!$new_image) {
        _textimage_diag(t('Textimage failed loading image %image', array(
          '%image' => $data['background_image']['uri'],
        )), WATCHDOG_ERROR, __FUNCTION__);
        return;
      }
      $width = $new_image->info['width'];
      $height = $new_image->info['height'];
      break;

    // If passing through the image from the last effect, retain
    // the size.
    case 'passthrough':
      $width = $dimensions['width'];
      $height = $dimensions['height'];
      break;

    // If explicitly requested not to have a background image, WxH is 1x1.
    case '':
    default:
      $width = $height = 1;
      break;
  }
  if ($data['exact']['width'] or $data['exact']['height']) {

    // Handle exact sizing.
    if ($data['exact']['width'] <= $width or $data['exact']['height'] <= $height) {

      // If current image is larger than size requested, call out to
      // scale/resize/crop effects to reduce image size depending
      // on options selected.
      switch ($data['exact']['dimensions']) {
        case 'scale':
          $scale_data = array(
            'width' => $data['exact']['width'],
            'height' => $data['exact']['height'],
            'upscale' => 0,
          );
          image_scale_dimensions($dimensions, $scale_data);
          return;
        case 'resize':
          $resize_data = array(
            'width' => $data['exact']['width'] ? $data['exact']['width'] : $width,
            'height' => $data['exact']['height'] ? $data['exact']['height'] : $height,
          );
          image_resize_dimensions($dimensions, $resize_data);
          return;
        case 'crop':
          $crop_data = array(
            'width' => min($data['exact']['width'], $width),
            'height' => min($data['exact']['height'], $height),
            'anchor' => $data['exact']['crop'],
          );
          image_resize_dimensions($dimensions, $crop_data);
          return;
      }
      _textimage_diag(t('Textimage image dimensions resizing failed.'), WATCHDOG_ERROR, __FUNCTION__);
      return FALSE;
    }
  }
  elseif ($data['relative']['leftdiff'] or $data['relative']['rightdiff'] or $data['relative']['topdiff'] or $data['relative']['bottomdiff']) {

    // Handle relative size.
    $dimensions['width'] = $width + $data['relative']['leftdiff'] + $data['relative']['rightdiff'];
    $dimensions['height'] = $height + $data['relative']['topdiff'] + $data['relative']['bottomdiff'];
    return;
  }
}