public function MaskImageEffect::applyEffect in Image Effects 8.2
Same name and namespace in other branches
- 8.3 src/Plugin/ImageEffect/MaskImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\MaskImageEffect::applyEffect()
- 8 src/Plugin/ImageEffect/MaskImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\MaskImageEffect::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/ MaskImageEffect.php, line 185
Class
- MaskImageEffect
- Class MaskImageEffect.
Namespace
Drupal\image_effects\Plugin\ImageEffectCode
public function applyEffect(ImageInterface $image) {
// Get the mask image object.
$mask_image = $this->imageFactory
->get($this->configuration['mask_image']);
if (!$mask_image
->isValid()) {
$this->logger
->error('Image mask failed using the %toolkit toolkit on %path', [
'%toolkit' => $image
->getToolkitId(),
'%path' => $this->configuration['mask_image'],
]);
return FALSE;
}
// Determine mask dimensions if they need to be changed.
if ((bool) $this->configuration['mask_width'] || (bool) $this->configuration['mask_height']) {
$mask_aspect = $mask_image
->getHeight() / $mask_image
->getWidth();
$mask_width = ImageUtility::percentFilter($this->configuration['mask_width'], $image
->getWidth());
$mask_height = ImageUtility::percentFilter($this->configuration['mask_height'], $image
->getHeight());
if ($mask_width && !$mask_height) {
$mask_height = (int) round($mask_width * $mask_aspect);
}
elseif (!$mask_width && $mask_height) {
$mask_width = (int) round($mask_height / $mask_aspect);
}
}
else {
$mask_width = $mask_image
->getWidth();
$mask_height = $mask_image
->getHeight();
}
// Calculate position of mask on source image based on placement option.
list($x, $y) = explode('-', $this->configuration['placement']);
$x_pos = round(image_filter_keyword($x, $image
->getWidth(), $mask_width));
$y_pos = round(image_filter_keyword($y, $image
->getHeight(), $mask_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('mask', [
'mask_image' => $mask_image,
'mask_width' => $mask_width !== $mask_image
->getWidth() ? $mask_width : NULL,
'mask_height' => $mask_height !== $mask_image
->getHeight() ? $mask_height : NULL,
'x_offset' => $x_pos + $x_offset,
'y_offset' => $y_pos + $y_offset,
]);
}