public function RelativeCropImageEffect::applyEffect in Image Effects 8.3
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 CropImageEffect::applyEffect
File
- src/
Plugin/ ImageEffect/ RelativeCropImageEffect.php, line 23
Class
- RelativeCropImageEffect
- Provides an image effect that crops images to a ratio.
Namespace
Drupal\image_effects\Plugin\ImageEffectCode
public function applyEffect(ImageInterface $image) {
$dimensions = [
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
];
// Bail if the image is invalid.
if ($dimensions['width'] === NULL || $dimensions['height'] === NULL) {
return FALSE;
}
$original_dimensions = $dimensions;
$this
->transformDimensions($dimensions, $image
->getSource());
// Pick the right anchor depending on whether the image is being cropped in
// width or in height.
if ($dimensions['width'] !== $original_dimensions['width']) {
$x = image_filter_keyword($this->configuration['anchor']['width'], $original_dimensions['width'], $dimensions['width']);
$y = 0;
}
elseif ($dimensions['height'] !== $original_dimensions['height']) {
$x = 0;
$y = image_filter_keyword($this->configuration['anchor']['height'], $original_dimensions['height'], $dimensions['height']);
}
else {
// If the image already has the correct dimensions, do not do anything.
return TRUE;
}
if (!$image
->crop($x, $y, $dimensions['width'], $dimensions['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;
}