You are here

function _textimage_wrapper_resize in Textimage 7.3

Recalculate wrapper image size.

When wrapper overflows the original image, and scaling is set on.

1 call to _textimage_wrapper_resize()
textimage_text_effect in effects/textimage_text.inc
Implements 'textimage_text' image effect callback.

File

effects/textimage_text.inc, line 1108
Implementation of the 'textimage_text' image effect.

Code

function _textimage_wrapper_resize($image, $wrapper, $data) {
  $resized = FALSE;

  // Background image dimensions.
  $image_width = $image->info['width'];
  $image_height = $image->info['height'];

  // Wrapper image dimensions.
  $wrapper_width = $wrapper->info['width'];
  $wrapper_height = $wrapper->info['height'];

  // Determine wrapper offset, based on placement option and direct
  // offset indicated in settings.
  $offset_wrapper = array(
    'xpos' => ceil(image_filter_keyword($data['layout']['x_pos'], $image_width, $wrapper_width)) + $data['layout']['x_offset'],
    'ypos' => ceil(image_filter_keyword($data['layout']['y_pos'], $image_height, $wrapper_height)) + $data['layout']['y_offset'],
  );

  // Position of wrapper's bottom right point.
  $xc_pos = $offset_wrapper['xpos'] + $wrapper_width;
  $yc_pos = $offset_wrapper['ypos'] + $wrapper_height;

  // Redetermine offset wrapper position and size based on
  // background image size.
  $offset_wrapper['xpos'] = max(0, $offset_wrapper['xpos']);
  $offset_wrapper['ypos'] = max(0, $offset_wrapper['ypos']);
  $xc_pos = min($image_width, $xc_pos);
  $yc_pos = min($image_height, $yc_pos);
  $offset_wrapper['width'] = $xc_pos - $offset_wrapper['xpos'];
  $offset_wrapper['height'] = $yc_pos - $offset_wrapper['ypos'];

  // If negative width/height, then the wrapper is totally
  // overflowing the background, and we cannot resize it.
  if ($offset_wrapper['width'] < 0 || $offset_wrapper['height'] < 0) {
    return array(
      FALSE,
      array(),
    );
  }

  // Determine if scaling needed. Take the side that is shrinking
  // most.
  $width_resize_index = $offset_wrapper['width'] / $wrapper_width;
  $height_resize_index = $offset_wrapper['height'] / $wrapper_height;
  if ($width_resize_index < 1 || $height_resize_index < 1) {
    $resized = TRUE;
    if ($width_resize_index < $height_resize_index) {
      $offset_wrapper['height'] = NULL;
    }
    else {
      $offset_wrapper['width'] = NULL;
    }
  }
  return array(
    $resized,
    $offset_wrapper,
  );
}