You are here

public function WatermarkImageEffect::applyEffect in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/ImageEffect/WatermarkImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\WatermarkImageEffect::applyEffect()
  2. 8.2 src/Plugin/ImageEffect/WatermarkImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\WatermarkImageEffect::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/WatermarkImageEffect.php, line 196

Class

WatermarkImageEffect
Class WatermarkImageEffect.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

public function applyEffect(ImageInterface $image) {

  // Get the watermark image object.
  $watermark_image = $this->imageFactory
    ->get($this->configuration['watermark_image']);
  if (!$watermark_image
    ->isValid()) {
    $this->logger
      ->error('Image watermark failed using the %toolkit toolkit on %path', [
      '%toolkit' => $image
        ->getToolkitId(),
      '%path' => $this->configuration['watermark_image'],
    ]);
    return FALSE;
  }

  // Determine watermark dimensions if they need to be changed.
  if ((bool) $this->configuration['watermark_width'] || (bool) $this->configuration['watermark_height']) {
    $watermark_aspect = $watermark_image
      ->getHeight() / $watermark_image
      ->getWidth();
    $watermark_width = ImageUtility::percentFilter($this->configuration['watermark_width'], $image
      ->getWidth());
    $watermark_height = ImageUtility::percentFilter($this->configuration['watermark_height'], $image
      ->getHeight());
    if ($watermark_width && !$watermark_height) {
      $watermark_height = (int) round($watermark_width * $watermark_aspect);
    }
    elseif (!$watermark_width && $watermark_height) {
      $watermark_width = (int) round($watermark_height / $watermark_aspect);
    }
  }
  else {
    $watermark_width = $watermark_image
      ->getWidth();
    $watermark_height = $watermark_image
      ->getHeight();
  }

  // Calculate position of watermark on source image based on placement
  // option.
  list($x, $y) = explode('-', $this->configuration['placement']);
  $x_pos = round(image_filter_keyword($x, $image
    ->getWidth(), $watermark_width));
  $y_pos = round(image_filter_keyword($y, $image
    ->getHeight(), $watermark_height));

  // Calculate offset based on px/percentage.
  $x_offset = (int) ImageUtility::percentFilter($this->configuration['x_offset'], $image
    ->getWidth());
  $y_offset = (int) ImageUtility::percentFilter($this->configuration['y_offset'], $image
    ->getHeight());
  return $image
    ->apply('watermark', [
    'watermark_image' => $watermark_image,
    'watermark_width' => $watermark_width !== $watermark_image
      ->getWidth() ? $watermark_width : NULL,
    'watermark_height' => $watermark_height !== $watermark_image
      ->getHeight() ? $watermark_height : NULL,
    'x_offset' => $x_pos + $x_offset,
    'y_offset' => $y_pos + $y_offset,
    'opacity' => $this->configuration['opacity'],
  ]);
}