You are here

function textimage_background_effect in Textimage 7.3

Implements 'textimage_background' image effect callback.

Parameters

object $image: An image object.

array $data: An array of attributes to use.

Return value

bool true on success, false on failure to apply the effect.

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

File

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

Code

function textimage_background_effect(&$image, $data) {

  // Include toolkit specific image functions.
  if (!_textimage_load_toolkit_functions()) {
    return FALSE;
  }

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

  // Handle background image.
  switch ($data['background_image']['mode']) {

    // If a background image is selected, it will override any
    // image built thus far.
    case 'select':
      if (!isset($data['background_image']['uri'])) {
        _textimage_diag(t('Textimage could not find an image to load.'), WATCHDOG_ERROR, __FUNCTION__);
        return FALSE;
      }
      $new_image = image_load($data['background_image']['uri']);
      if ($new_image) {
        foreach ($new_image as $property => $value) {
          $image->{$property} = $value;
        }
      }
      else {
        _textimage_diag(t('Textimage failed loading image %image', array(
          '%image' => $data['background_image']['uri'],
        )), WATCHDOG_ERROR, __FUNCTION__);
        return FALSE;
      }
      break;

    // If passing through the image from the last effect, no special
    // treatment needed.
    case 'passthrough':
      break;

    // If explicitly requested not to have a background image, create
    // a transparent 1x1 image.
    case '':
    default:
      $new_image = image_toolkit_invoke('textimage_create_transparent', $image, array(
        1,
        1,
        TextimageImager::getState('gif_transparency_color'),
      ));
      if ($new_image) {
        foreach ($new_image as $property => $value) {
          $image->{$property} = $value;
        }
      }
      break;
  }
  $success = TRUE;

  // Handle exact sizing impacts on original image.
  if ($data['exact']['width'] || $data['exact']['height']) {

    // If current image is larger than size requested, call out to
    // scale/resize/crop effects to reduce image size depending
    // on options selected.
    if ($data['exact']['width'] <= $image->info['width'] || $data['exact']['height'] <= $image->info['height']) {
      switch ($data['exact']['dimensions']) {
        case 'scale':
          $scale_data = array(
            'width' => $data['exact']['width'],
            'height' => $data['exact']['height'],
            'upscale' => 0,
          );
          $success = image_scale_effect($image, $scale_data);
          break;
        case 'resize':
          $resize_data = array(
            'width' => $data['exact']['width'] ? $data['exact']['width'] : $image->info['width'],
            'height' => $data['exact']['height'] ? $data['exact']['height'] : $image->info['height'],
          );
          $success = image_resize_effect($image, $resize_data);
          break;
        case 'crop':
          $crop_data = array(
            'width' => min($data['exact']['width'], $image->info['width']),
            'height' => min($data['exact']['height'], $image->info['height']),
            'anchor' => $data['exact']['crop'],
          );
          $success = image_crop_effect($image, $crop_data);
          break;
      }
      if (!$success) {
        _textimage_diag(t('Textimage failed image processing.'), WATCHDOG_ERROR, __FUNCTION__);
        return FALSE;
      }
    }
  }

  // If resizing, call out to canvasactions_definecanvas_effect()
  // to finalise layout.
  if ($data['exact']['width'] || $data['exact']['height'] || $data['relative']['leftdiff'] || $data['relative']['rightdiff'] || $data['relative']['topdiff'] || $data['relative']['bottomdiff']) {
    $canvas_data = array(
      'RGB' => array(
        'HEX' => $data['background']['color'],
      ),
      'under' => TRUE,
      'exact' => array(
        'width' => $data['exact']['width'],
        'height' => $data['exact']['height'],
        'xpos' => $data['exact']['xpos'],
        'ypos' => $data['exact']['ypos'],
      ),
      'relative' => array(
        'leftdiff' => $data['relative']['leftdiff'],
        'rightdiff' => $data['relative']['rightdiff'],
        'topdiff' => $data['relative']['topdiff'],
        'bottomdiff' => $data['relative']['bottomdiff'],
      ),
    );
    if ($image->info['mime_type'] == 'image/gif') {

      // For .gif format, if transparent background set transparency color.
      if (!$canvas_data['RGB']['HEX']) {
        $canvas_data['RGB']['HEX'] = TextimageImager::getState('gif_transparency_color');
      }
      $success = canvasactions_definecanvas_effect($image, $canvas_data);
    }
    else {
      $success = canvasactions_definecanvas_effect($image, $canvas_data);
    }
  }
  if (!$success) {
    _textimage_diag(t('Textimage failed image processing.'), WATCHDOG_ERROR, __FUNCTION__);
    return FALSE;
  }

  // Stores background color for later effects.
  if ($data['background']['repeat']) {
    TextimageImager::setState('background_color', $data['background']['color']);
  }

  // Reset transparency color for .gif format.
  if ($image->info['mime_type'] == 'image/gif') {
    image_toolkit_invoke('textimage_set_transparency', $image, array(
      TextimageImager::getState('gif_transparency_color'),
    ));
  }
  return $success;
}