You are here

function image_gd_textimage_text_to_image in Textimage 7.3

Creates a new image resource and overlays the text over it.

File

effects/textimage_text.gd.inc, line 23
GD2 toolkit for image manipulation within Textimage.

Code

function image_gd_textimage_text_to_image($image, $data = array()) {

  // Create the image resource, fill transparent.
  $new_image = image_toolkit_invoke('textimage_create_transparent', $image, array(
    $image->info['width'],
    $image->info['height'],
    $data['gif_transparency_color'],
  ));
  if ($new_image) {
    $image->resource = $new_image->resource;
  }
  else {
    return FALSE;
  }

  // Draw and fill the outer text box, if required.
  if ($data['layout']['background_color']) {
    _textimage_draw_rectangle($image, $data['outer_box'], $data['layout']['background_color']);
  }

  // In debug mode, visually display the text boxes.
  if ($data['debug_visuals']) {
    _textimage_draw_box($image, $data['inner_box'], $data['layout']['background_color'], TRUE);
    _textimage_draw_box($image, $data['outer_box'], $data['layout']['background_color'], TRUE);

    // Wrapper.
    _textimage_draw_box($image, array(
      0,
      0,
      $image->info['width'] - 1,
      0,
      $image->info['width'] - 1,
      $image->info['height'] - 1,
      0,
      $image->info['height'] - 1,
    ), '#000000');
  }

  // Foreground text color.
  $foreground_color = image_toolkit_invoke('textimage_imagecolor', $image, array(
    array(
      'rgba' => $data['font']['color'],
    ),
  ));

  // Determine if outline/shadow is required.
  $outline = $shadow = FALSE;
  if ($data['font']['stroke_mode'] == 'outline' && ($data['font']['outline_top'] || $data['font']['outline_right'] || $data['font']['outline_bottom'] || $data['font']['outline_left']) && $data['font']['stroke_color']) {
    $outline = TRUE;
  }
  elseif ($data['font']['stroke_mode'] == 'shadow' && ($data['font']['shadow_x_offset'] || $data['font']['shadow_y_offset'] || $data['font']['shadow_width'] || $data['font']['shadow_height']) && $data['font']['stroke_color']) {
    $shadow = TRUE;
  }

  // Process each of the text lines.
  $current_y = 0;
  foreach ($data['text_lines'] as $text_line) {

    // This text line's box size.
    $text_line_box = _textimage_get_bounding_box($image, $text_line, 1, $data['font']['size'], $data['font']['uri']);
    $text_line_box
      ->set('height', $data['line_height']);

    // Manage text alignment within the line.
    $x_delta = $data['inner_width'] - $text_line_box
      ->get('width');
    $current_y += $data['line_height'];
    switch ($data['text']['align']) {
      case 'center':
        $x_offset = round($x_delta / 2);
        break;
      case 'right':
        $x_offset = $x_delta;
        break;
      case 'left':
      default:
        $x_offset = 0;
        break;
    }

    // Get details for the rotated/translated text line box.
    $text_line_box_t = $text_line_box
      ->getTranslatedBox($data['font']['angle'], array(
      $data['layout']['padding_left'] + $x_offset,
      $data['layout']['padding_top'] + $current_y - $data['line_height'],
    ), $data['topLeftCornerPosition']);
    list($x_pos, $y_pos) = $text_line_box_t
      ->get('basepoint');

    // Overlays the text outline/shadow, if required.
    if ($outline || $shadow) {
      $stroke_color = image_toolkit_invoke('textimage_imagecolor', $image, array(
        array(
          'rgba' => $data['font']['stroke_color'],
        ),
      ));
      if ($outline) {
        $stroke_x_pos = $x_pos;
        $stroke_y_pos = $y_pos;
        $stroke_top = $data['font']['outline_top'];
        $stroke_right = $data['font']['outline_right'];
        $stroke_bottom = $data['font']['outline_bottom'];
        $stroke_left = $data['font']['outline_left'];
      }
      elseif ($shadow) {
        $stroke_x_pos = $x_pos + $data['font']['shadow_x_offset'];
        $stroke_y_pos = $y_pos + $data['font']['shadow_y_offset'];
        $stroke_top = 0;
        $stroke_right = $data['font']['shadow_width'];
        $stroke_bottom = $data['font']['shadow_height'];
        $stroke_left = 0;
      }
      $data_stroke = array(
        'size' => $data['font']['size'],
        'angle' => -$data['font']['angle'],
        'fontfile' => $data['font']['uri'],
        'text' => $text_line,
        'x' => $stroke_x_pos,
        'y' => $stroke_y_pos,
        'textcolor' => $foreground_color,
        'strokecolor' => $stroke_color,
        'top' => $stroke_top,
        'right' => $stroke_right,
        'bottom' => $stroke_bottom,
        'left' => $stroke_left,
      );
      image_toolkit_invoke('textimage_text_stroke', $image, array(
        $data_stroke,
      ));
    }

    // Overlays the text.
    imagettftext($image->resource, $data['font']['size'], -$data['font']['angle'], $x_pos, $y_pos, $foreground_color, $data['font']['uri'], $text_line);

    // In debug mode, display a polygon enclosing the text line.
    if ($data['debug_visuals']) {
      _textimage_draw_debug_box($image, $text_line_box_t, $data['layout']['background_color'], TRUE);
    }

    // Add interline spacing (leading) before next iteration.
    $current_y += $data['text']['line_spacing'];
  }

  // Finalise image.
  imagealphablending($image->resource, TRUE);
  imagesavealpha($image->resource, TRUE);
  return TRUE;
}