class BackgroundImageEffect in Image Effects 8
Same name and namespace in other branches
- 8.3 src/Plugin/ImageEffect/BackgroundImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\BackgroundImageEffect
- 8.2 src/Plugin/ImageEffect/BackgroundImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\BackgroundImageEffect
Class BackgroundImageEffect.
Plugin annotation
@ImageEffect(
id = "image_effects_background",
label = @Translation("Background"),
description = @Translation("Places the source image anywhere over a selected background 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\BackgroundImageEffect implements ContainerFactoryPluginInterface
- 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 BackgroundImageEffect
File
- src/
Plugin/ ImageEffect/ BackgroundImageEffect.php, line 23
Namespace
Drupal\image_effects\Plugin\ImageEffectView source
class BackgroundImageEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
/**
* The image factory service.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* The image selector plugin.
*
* @var \Drupal\image_effects\Plugin\ImageEffectsPluginBaseInterface
*/
protected $imageSelector;
/**
* Constructs an BackgroundImageEffect 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 [
'placement' => 'center-center',
'x_offset' => 0,
'y_offset' => 0,
'opacity' => 100,
'background_image' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function getSummary() {
$summary = [
'#theme' => 'image_effects_background_summary',
'#data' => $this->configuration,
];
$summary += parent::getSummary();
return $summary;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = [
'#title' => $this
->t('Background image'),
'#description' => $this
->t('Image to use for background.'),
'#default_value' => $this->configuration['background_image'],
];
$form['background_image'] = $this->imageSelector
->selectionElement($options);
$form['placement'] = [
'#type' => 'radios',
'#title' => $this
->t('Placement'),
'#options' => [
'left-top' => $this
->t('Top left'),
'center-top' => $this
->t('Top center'),
'right-top' => $this
->t('Top right'),
'left-center' => $this
->t('Center left'),
'center-center' => $this
->t('Center'),
'right-center' => $this
->t('Center right'),
'left-bottom' => $this
->t('Bottom left'),
'center-bottom' => $this
->t('Bottom center'),
'right-bottom' => $this
->t('Bottom right'),
],
'#theme' => 'image_anchor',
'#default_value' => $this->configuration['placement'],
'#description' => $this
->t('Position of the source image on the background image.'),
];
$form['x_offset'] = [
'#type' => 'number',
'#title' => $this
->t('Horizontal offset'),
'#field_suffix' => 'px',
'#description' => $this
->t('Additional horizontal offset from placement.'),
'#default_value' => $this->configuration['x_offset'],
'#maxlength' => 4,
'#size' => 4,
];
$form['y_offset'] = [
'#type' => 'number',
'#title' => $this
->t('Vertical offset'),
'#field_suffix' => 'px',
'#description' => $this
->t('Additional vertical offset from placement.'),
'#default_value' => $this->configuration['y_offset'],
'#maxlength' => 4,
'#size' => 4,
];
$form['opacity'] = [
'#type' => 'number',
'#title' => $this
->t('Opacity'),
'#field_suffix' => '%',
'#description' => $this
->t('Opacity of the source image when overlaid on the background image, in percentage. 0% means fully transparent, 100% fully opaque.'),
'#default_value' => $this->configuration['opacity'],
'#min' => 0,
'#max' => 100,
'#maxlength' => 3,
'#size' => 3,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$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');
$this->configuration['opacity'] = $form_state
->getValue('opacity');
$this->configuration['background_image'] = $form_state
->getValue('background_image');
// Stores background image width and height in configuration to avoid the
// need to fetch the image from storage in ::transformDimensions.
$background_image = $this->imageFactory
->get($this->configuration['background_image']);
$this->configuration['background_image_width'] = $background_image
->getWidth();
$this->configuration['background_image_height'] = $background_image
->getHeight();
}
/**
* {@inheritdoc}
*/
public function applyEffect(ImageInterface $image) {
$background_image = $this->imageFactory
->get($this->configuration['background_image']);
if (!$background_image
->isValid()) {
$this->logger
->error('Image background failed using the %toolkit toolkit on %path', [
'%toolkit' => $image
->getToolkitId(),
'%path' => $this->configuration['background_image'],
]);
return FALSE;
}
list($x, $y) = explode('-', $this->configuration['placement']);
$x_pos = image_filter_keyword($x, $background_image
->getWidth(), $image
->getWidth());
$y_pos = image_filter_keyword($y, $background_image
->getHeight(), $image
->getHeight());
return $image
->apply('background', [
'x_offset' => $x_pos + $this->configuration['x_offset'],
'y_offset' => $y_pos + $this->configuration['y_offset'],
'opacity' => $this->configuration['opacity'],
'background_image' => $background_image,
]);
}
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions, $uri) {
$dimensions['width'] = $this->configuration['background_image_width'];
$dimensions['height'] = $this->configuration['background_image_height'];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BackgroundImageEffect:: |
protected | property | The image factory service. | |
BackgroundImageEffect:: |
protected | property | The image selector plugin. | |
BackgroundImageEffect:: |
public | function |
Applies an image effect to the image object. Overrides ImageEffectInterface:: |
|
BackgroundImageEffect:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
BackgroundImageEffect:: |
public static | function |
Creates an instance of the plugin. Overrides ImageEffectBase:: |
|
BackgroundImageEffect:: |
public | function |
Gets default configuration for this plugin. Overrides ImageEffectBase:: |
|
BackgroundImageEffect:: |
public | function |
Returns a render array summarizing the configuration of the image effect. Overrides ImageEffectBase:: |
|
BackgroundImageEffect:: |
public | function |
Form submission handler. Overrides ConfigurableImageEffectBase:: |
|
BackgroundImageEffect:: |
public | function |
Determines the dimensions of the styled image. Overrides ImageEffectBase:: |
|
BackgroundImageEffect:: |
public | function |
Constructs an BackgroundImageEffect object. Overrides ImageEffectBase:: |
|
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:: |
|
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. |