You are here

public function FocalPointScaleAndCropImageEffect::applyEffect in Focal Point 8

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 FocalPointEffectBase::applyEffect

File

src/Plugin/ImageEffect/FocalPointScaleAndCropImageEffect.php, line 22

Class

FocalPointScaleAndCropImageEffect
Scales and crops image while keeping its focal point close to centered.

Namespace

Drupal\focal_point\Plugin\ImageEffect

Code

public function applyEffect(ImageInterface $image) {
  parent::applyEffect($image);

  // First, attempt to resize the image.
  $originalDimensions = $this
    ->getOriginalImageSize();
  $resize_data = self::calculateResizeData($originalDimensions['width'], $originalDimensions['height'], $this->configuration['width'], $this->configuration['height']);
  if (!$image
    ->resize($resize_data['width'], $resize_data['height'])) {
    $this->logger
      ->error('Focal point scale and crop failed while resizing using the %toolkit toolkit on %path (%mimetype, %dimensions)', [
      '%toolkit' => $image
        ->getToolkitId(),
      '%path' => $image
        ->getSource(),
      '%mimetype' => $image
        ->getMimeType(),
      '%dimensions' => $image
        ->getWidth() . 'x' . $image
        ->getHeight(),
    ]);
    return FALSE;
  }

  // Next, attempt to crop the image.
  $crop = $this
    ->getCrop($image);
  return $this
    ->applyCrop($image, $crop);
}