You are here

public function TextOverlayImageEffect::applyEffect in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageEffect/TextOverlayImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\TextOverlayImageEffect::applyEffect()
  2. 8 src/Plugin/ImageEffect/TextOverlayImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\TextOverlayImageEffect::applyEffect()

Applies an image effect to the image object.

Parameters

\Drupal\Core\Image\ImageInterface $image: An image file object.

Return value

bool TRUE on success. FALSE if unable to perform the image effect on the image.

Overrides ImageEffectInterface::applyEffect

File

src/Plugin/ImageEffect/TextOverlayImageEffect.php, line 768

Class

TextOverlayImageEffect
Overlays text on the image, defining text font, size and positioning.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

public function applyEffect(ImageInterface $image) {

  // Preserve current background image dimensions.
  if ($image
    ->getWidth() === 1 && $image
    ->getHeight() === 1) {

    // Special case: when width and height of the source image is 1, we set
    // starting width and height within the effect to be zero. This way we
    // avoid that text overlays that extend the source image lead to images
    // with a single transparent or colored pixel in the center of the image.
    $image_width = 0;
    $image_height = 0;
  }
  else {
    $image_width = $image
      ->getWidth();
    $image_height = $image
      ->getHeight();
  }
  $this->info['image_width'] = $image_width;
  $this->info['image_height'] = $image_height;

  // Get the text wrapper Image object.
  if (!($wrapper = $this
    ->getTextWrapper())) {
    return FALSE;
  }

  // Determine if background image needs resizing.
  if ($this->configuration['layout']['overflow_action'] == 'extend') {

    // The size of the frame sides for color filling.
    $this->info['frame_top'] = 0;
    $this->info['frame_right'] = 0;
    $this->info['frame_bottom'] = 0;
    $this->info['frame_left'] = 0;

    // Check wrapper image overflowing the original image.
    if ($this
      ->canvasResizeNeeded($wrapper)) {

      // Apply set_canvas, transparent background.
      $data = [
        'width' => $this->info['image_width'],
        'height' => $this->info['image_height'],
        'x_pos' => $this->info['image_xpos'],
        'y_pos' => $this->info['image_ypos'],
      ];
      if (!$image
        ->apply('set_canvas', $data)) {
        return FALSE;
      }

      // Color fill the frame with extended color.
      if ($main_bg_color = $this->configuration['layout']['extended_color']) {

        // Top rectangle.
        $rectangle = new PositionedRectangle();
        if ($this->info['frame_top']) {
          $rectangle
            ->setFromCorners([
            'c_a' => [
              0,
              $this->info['frame_top'] - 1,
            ],
            'c_b' => [
              $this->info['image_width'] - 1,
              $this->info['frame_top'] - 1,
            ],
            'c_c' => [
              $this->info['image_width'] - 1,
              0,
            ],
            'c_d' => [
              0,
              0,
            ],
          ]);
          if (!$image
            ->apply('draw_rectangle', [
            'rectangle' => $rectangle,
            'fill_color' => $main_bg_color,
          ])) {
            return FALSE;
          }
        }

        // Bottom rectangle.
        if ($this->info['frame_bottom']) {
          $rectangle
            ->setFromCorners([
            'c_a' => [
              0,
              $this->info['image_height'] - 1,
            ],
            'c_b' => [
              $this->info['image_width'] - 1,
              $this->info['image_height'] - 1,
            ],
            'c_c' => [
              $this->info['image_width'] - 1,
              $image_height + $this->info['frame_top'],
            ],
            'c_d' => [
              0,
              $image_height + $this->info['frame_top'],
            ],
          ]);
          if (!$image
            ->apply('draw_rectangle', [
            'rectangle' => $rectangle,
            'fill_color' => $main_bg_color,
          ])) {
            return FALSE;
          }
        }

        // Left rectangle.
        if ($this->info['frame_left']) {
          $rectangle
            ->setFromCorners([
            'c_a' => [
              0,
              $this->info['frame_top'] + $image_height - 1,
            ],
            'c_b' => [
              $this->info['frame_left'] - 1,
              $this->info['frame_top'] + $image_height - 1,
            ],
            'c_c' => [
              $this->info['frame_left'] - 1,
              $this->info['frame_top'],
            ],
            'c_d' => [
              0,
              $this->info['frame_top'],
            ],
          ]);
          if (!$image
            ->apply('draw_rectangle', [
            'rectangle' => $rectangle,
            'fill_color' => $main_bg_color,
          ])) {
            return FALSE;
          }
        }

        // Right rectangle.
        if ($this->info['frame_right']) {
          $rectangle
            ->setFromCorners([
            'c_a' => [
              $this->info['frame_left'] + $image_width,
              $this->info['frame_top'] + $image_height - 1,
            ],
            'c_b' => [
              $this->info['image_width'] - 1,
              $this->info['frame_top'] + $image_height - 1,
            ],
            'c_c' => [
              $this->info['image_width'] - 1,
              $this->info['frame_top'],
            ],
            'c_d' => [
              $this->info['frame_left'] + $image_width,
              $this->info['frame_top'],
            ],
          ]);
          if (!$image
            ->apply('draw_rectangle', [
            'rectangle' => $rectangle,
            'fill_color' => $main_bg_color,
          ])) {
            return FALSE;
          }
        }
      }
    }
  }
  else {

    // Nothing to do, just place the wrapper at offset required.
    $x_offset = ceil(image_filter_keyword($this->configuration['layout']['x_pos'], $image_width, $wrapper
      ->getWidth()));
    $y_offset = ceil(image_filter_keyword($this->configuration['layout']['y_pos'], $image_height, $wrapper
      ->getHeight()));
    $this->info['wrapper_xpos'] = $x_offset + $this->configuration['layout']['x_offset'];
    $this->info['wrapper_ypos'] = $y_offset + $this->configuration['layout']['y_offset'];
  }

  // Finally, lay the wrapper over the source image.
  if (!$image
    ->apply('watermark', [
    'watermark_image' => $wrapper,
    'x_offset' => $this->info['wrapper_xpos'],
    'y_offset' => $this->info['wrapper_ypos'],
  ])) {
    return FALSE;
  }
  return TRUE;
}