You are here

function textimage_text_effect in Textimage 7.3

Implements 'textimage_text' image effect callback.

Overlay text on an image resource.

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_text_effect'
textimage_image_effect_info in ./textimage.module
Implements hook_image_effect_info().

File

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

Code

function textimage_text_effect($image, $data) {

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

  // Get the text wrapper resource.
  if (!($wrapper = _textimage_get_text_wrapper($image, $data))) {
    return FALSE;
  }

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

  // Offset wrapper dimensions.
  $offset_wrapper = array();

  // Determine needed resizing/repositioning of background image and/or
  // text wrapper image, based on the settings.
  switch ($data['layout']['overflow_action']) {
    case 'extend':

      // The background image new dimensions, after extension.
      $image_new = array(
        'xpos' => 0,
        'ypos' => 0,
        'width' => $image_width,
        'height' => $image_height,
      );

      // The size of the frame sides for color filling.
      $frame = array(
        'top' => 0,
        'right' => 0,
        'bottom' => 0,
        'left' => 0,
      );

      // Check wrapper image overflowing the original image.
      list($resized, $offset_wrapper) = _textimage_background_image_resize($image, $wrapper, $data, $image_new, $frame);
      if ($resized) {

        // Call out to canvasactions_definecanvas_effect(), transparent
        // background.
        $canvas_data = array(
          'RGB' => array(
            'HEX' => NULL,
          ),
          'under' => TRUE,
          'exact' => $image_new,
        );
        if (!canvasactions_definecanvas_effect($image, $canvas_data)) {
          return FALSE;
        }

        // Color fill the frame with carried on background color.
        if ($main_bg_color = TextimageImager::getState('background_color')) {

          // Top rectangle.
          if ($frame['top']) {
            $points = array(
              0,
              0,
              $image_new['width'] - 1,
              0,
              $image_new['width'] - 1,
              $frame['top'] - 1,
              0,
              $frame['top'] - 1,
            );
            _textimage_draw_rectangle($image, $points, $main_bg_color);
          }

          // Bottom rectangle.
          if ($frame['bottom']) {
            $points = array(
              0,
              $image_height + $frame['top'],
              $image_new['width'] - 1,
              $image_height + $frame['top'],
              $image_new['width'] - 1,
              $image_new['height'] - 1,
              0,
              $image_new['height'] - 1,
            );
            _textimage_draw_rectangle($image, $points, $main_bg_color);
          }

          // Left rectangle.
          if ($frame['left']) {
            $points = array(
              0,
              $frame['top'],
              $frame['left'] - 1,
              $frame['top'],
              $frame['left'] - 1,
              $frame['top'] + $image_height - 1,
              0,
              $frame['top'] + $image_height - 1,
            );
            _textimage_draw_rectangle($image, $points, $main_bg_color);
          }

          // Right rectangle.
          if ($frame['right']) {
            $points = array(
              $frame['left'] + $image_width,
              $frame['top'],
              $image_new['width'] - 1,
              $frame['top'],
              $image_new['width'] - 1,
              $frame['top'] + $image_height - 1,
              $frame['left'] + $image_width,
              $frame['top'] + $image_height - 1,
            );
            _textimage_draw_rectangle($image, $points, $main_bg_color);
          }
        }
      }
      break;
    case 'scaletext':

      // Check if scaling down is needed.
      list($resized, $offset_wrapper) = _textimage_wrapper_resize($image, $wrapper, $data);
      if ($resized) {
        $scale_data = array(
          'width' => $offset_wrapper['width'],
          'height' => $offset_wrapper['height'],
          'upscale' => 0,
        );
        if (!image_scale_effect($wrapper, $scale_data)) {
          return FALSE;
        }
      }
      break;
    case 'crop':
    default:

      // Nothing to do, just place the wrapper at offset required.
      $x_offset = ceil(image_filter_keyword($data['layout']['x_pos'], $image_width, $wrapper->info['width']));
      $y_offset = ceil(image_filter_keyword($data['layout']['y_pos'], $image_height, $wrapper->info['height']));
      $offset_wrapper['xpos'] = $x_offset + $data['layout']['x_offset'];
      $offset_wrapper['ypos'] = $y_offset + $data['layout']['y_offset'];
      break;
  }

  // Finally, lay the wrapper over the source image.
  if (!empty($offset_wrapper)) {
    if (!image_overlay($image, $wrapper, $offset_wrapper['xpos'], $offset_wrapper['ypos'])) {
      return FALSE;
    }
  }

  // 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 TRUE;
}