class MaskImageEffect in Image Effects 8
Same name and namespace in other branches
- 8.3 src/Plugin/ImageEffect/MaskImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\MaskImageEffect
- 8.2 src/Plugin/ImageEffect/MaskImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\MaskImageEffect
Class MaskImageEffect.
Plugin annotation
@ImageEffect(
id = "image_effects_mask",
label = @Translation("Mask"),
description = @Translation("Apply a mask to the image.")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\image\ImageEffectBase implements ContainerFactoryPluginInterface, ImageEffectInterface
- class \Drupal\image\ConfigurableImageEffectBase implements ConfigurableImageEffectInterface
- class \Drupal\image_effects\Plugin\ImageEffect\MaskImageEffect implements ContainerFactoryPluginInterface uses AnchorTrait
- class \Drupal\image\ConfigurableImageEffectBase implements ConfigurableImageEffectInterface
- class \Drupal\image\ImageEffectBase implements ContainerFactoryPluginInterface, ImageEffectInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of MaskImageEffect
File
- src/
Plugin/ ImageEffect/ MaskImageEffect.php, line 24
Namespace
Drupal\image_effects\Plugin\ImageEffectView source
class MaskImageEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
use AnchorTrait;
/**
* The image factory service.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* The image selector plugin.
*
* @var \Drupal\image_effects\Plugin\ImageEffectsPluginBaseInterface
*/
protected $imageSelector;
/**
* Constructs a MaskImageEffect object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory service.
* @param \Drupal\image_effects\Plugin\ImageEffectsPluginBaseInterface $image_selector
* The image selector plugin.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, LoggerInterface $logger, ImageFactory $image_factory, ImageEffectsPluginBaseInterface $image_selector) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
$this->imageFactory = $image_factory;
$this->imageSelector = $image_selector;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.factory')
->get('image'), $container
->get('image.factory'), $container
->get('plugin.manager.image_effects.image_selector')
->getPlugin());
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'mask_image' => '',
'mask_width' => NULL,
'mask_height' => NULL,
'placement' => 'center-center',
'x_offset' => NULL,
'y_offset' => NULL,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function getSummary() {
$summary = [
'#theme' => 'image_effects_mask_summary',
'#data' => $this->configuration,
];
$summary += parent::getSummary();
// Get the human readable label for placement.
$summary['#data']['placement'] = $this
->anchorOptions()[$summary['#data']['placement']];
return $summary;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = [
'#title' => $this
->t('Mask image'),
'#description' => $this
->t('Image to use for masking. The mask file should be a grayscale image, where full white pixels will let the original image through, and full black pixels will hide it.'),
'#default_value' => $this->configuration['mask_image'],
'#required' => TRUE,
];
$form['mask_image'] = $this->imageSelector
->selectionElement($options);
$form['mask_resize'] = [
'#type' => 'details',
'#title' => $this
->t('Mask resize'),
'#description' => $this
->t('Select dimensions either in pixels or as percentage of the <strong>current canvas</strong>. Leaving one dimension empty will resize the mask maintaing its aspect ratio. Leave both dimensions empty to apply the mask in its original dimensions.'),
'#open' => TRUE,
];
$form['mask_resize']['mask_width'] = [
'#type' => 'image_effects_px_perc',
'#title' => $this
->t('Mask width'),
'#default_value' => $this->configuration['mask_width'],
'#size' => 5,
'#maxlength' => 5,
'#required' => FALSE,
];
$form['mask_resize']['mask_height'] = [
'#type' => 'image_effects_px_perc',
'#title' => $this
->t('Mask height'),
'#default_value' => $this->configuration['mask_height'],
'#size' => 5,
'#maxlength' => 5,
'#required' => FALSE,
];
$form['placement'] = [
'#type' => 'radios',
'#title' => $this
->t('Placement'),
'#options' => $this
->anchorOptions(),
'#theme' => 'image_anchor',
'#default_value' => $this->configuration['placement'],
'#description' => $this
->t('Position of the mask on the canvas.'),
'#required' => TRUE,
];
$form['x_offset'] = [
'#type' => 'image_effects_px_perc',
'#title' => $this
->t('Horizontal offset'),
'#description' => $this
->t("Additional horizontal offset from placement. Enter a value, and specify if pixels or percent of the canvas width. '+' or no sign shifts the mask rightward, '-' sign leftward."),
'#default_value' => $this->configuration['x_offset'],
'#maxlength' => 4,
'#size' => 4,
];
$form['y_offset'] = [
'#type' => 'image_effects_px_perc',
'#title' => $this
->t('Vertical offset'),
'#description' => $this
->t("Additional vertical offset from placement. Enter a value, and specify if pixels or percent of the canvas height. '+' or no sign shifts the mask downward, '-' sign upward."),
'#default_value' => $this->configuration['y_offset'],
'#maxlength' => 4,
'#size' => 4,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['mask_image'] = $form_state
->getValue('mask_image');
$this->configuration['mask_width'] = $form_state
->getValue([
'mask_resize',
'mask_width',
]);
$this->configuration['mask_height'] = $form_state
->getValue([
'mask_resize',
'mask_height',
]);
$this->configuration['placement'] = $form_state
->getValue('placement');
$this->configuration['x_offset'] = $form_state
->getValue('x_offset');
$this->configuration['y_offset'] = $form_state
->getValue('y_offset');
}
/**
* {@inheritdoc}
*/
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,
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AnchorTrait:: |
protected | function | Returns an array of options for anchoring an image. | |
ConfigurableImageEffectBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
ImageEffectBase:: |
protected | property | A logger instance. | |
ImageEffectBase:: |
protected | property | The image effect ID. | |
ImageEffectBase:: |
protected | property | The weight of the image effect. | |
ImageEffectBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
ImageEffectBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ImageEffectBase:: |
public | function |
Returns the extension of the derivative after applying this image effect. Overrides ImageEffectInterface:: |
1 |
ImageEffectBase:: |
public | function |
Returns the unique ID representing the image effect. Overrides ImageEffectInterface:: |
|
ImageEffectBase:: |
public | function |
Returns the weight of the image effect. Overrides ImageEffectInterface:: |
|
ImageEffectBase:: |
public | function |
Returns the image effect label. Overrides ImageEffectInterface:: |
|
ImageEffectBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ImageEffectBase:: |
public | function |
Sets the weight for this image effect. Overrides ImageEffectInterface:: |
|
ImageEffectBase:: |
public | function |
Determines the dimensions of the styled image. Overrides ImageEffectInterface:: |
4 |
MaskImageEffect:: |
protected | property | The image factory service. | |
MaskImageEffect:: |
protected | property | The image selector plugin. | |
MaskImageEffect:: |
public | function |
Applies an image effect to the image object. Overrides ImageEffectInterface:: |
|
MaskImageEffect:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
MaskImageEffect:: |
public static | function |
Creates an instance of the plugin. Overrides ImageEffectBase:: |
|
MaskImageEffect:: |
public | function |
Gets default configuration for this plugin. Overrides ImageEffectBase:: |
|
MaskImageEffect:: |
public | function |
Returns a render array summarizing the configuration of the image effect. Overrides ImageEffectBase:: |
|
MaskImageEffect:: |
public | function |
Form submission handler. Overrides ConfigurableImageEffectBase:: |
|
MaskImageEffect:: |
public | function |
Constructs a MaskImageEffect object. Overrides ImageEffectBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |