You are here

public function FocusScaleCropImageEffect::applyEffect in Image Focus Crop 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 ResizeImageEffect::applyEffect

File

src/Plugin/ImageEffect/FocusScaleCropImageEffect.php, line 25

Class

FocusScaleCropImageEffect
Crops with focus an image resource.

Namespace

Drupal\image_focus\Plugin\ImageEffect

Code

public function applyEffect(ImageInterface $image) {
  $width = $this->configuration['width'];
  $height = $this->configuration['height'];
  list($cx, $cy) = $this
    ->getFocalPoint($image);
  $scale = max($width / $image
    ->getWidth(), $height / $image
    ->getHeight());
  if (!$image
    ->resize($image
    ->getWidth() * $scale, $image
    ->getHeight() * $scale)) {
    $this->logger
      ->error('Image resize failed 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;
  }
  $x = max(0, min($image
    ->getWidth() - $width, $cx * $scale - $width / 2));
  $y = max(0, min($image
    ->getHeight() - $height, $cy * $scale - $height / 2));
  if (!$image
    ->crop($x, $y, $width, $height)) {
    $this->logger
      ->error('Image crop failed 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;
  }
  return TRUE;
}