class AutomatedCropEffect in Automated Crop 8
Provide an Automatic crop tools.
Plugin annotation
@ImageEffect(
id = "automated_crop",
label = @Translation("Automated Crop"),
description = @Translation("Applies automated crop 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\automated_crop\Plugin\ImageEffect\AutomatedCropEffect 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 AutomatedCropEffect
File
- src/
Plugin/ ImageEffect/ AutomatedCropEffect.php, line 24
Namespace
Drupal\automated_crop\Plugin\ImageEffectView source
class AutomatedCropEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
/**
* AutomatedCrop object.
*
* @var \Drupal\automated_crop\AutomatedCropManager
*/
protected $automatedCropManager;
/**
* Automated crop object loaded with current image.
*
* @var \Drupal\automated_crop\AutomatedCropInterface|false
*/
protected $automatedCrop;
/**
* The image factory service.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, AutomatedCropManager $plugin_automated_crop, ImageFactory $image_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
$this->automatedCropManager = $plugin_automated_crop;
$this->imageFactory = $image_factory;
}
/**
* {@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('plugin.manager.automated_crop'), $container
->get('image.factory'));
}
/**
* {@inheritdoc}
*/
public function applyEffect(ImageInterface $image) {
/** @var \Drupal\automated_crop\AutomatedCropInterface $crop */
if ($crop = $this
->getAutomatedCrop($image)) {
$anchor = $crop
->anchor();
$size = $crop
->size();
if (!$image
->crop($anchor['x'], $anchor['y'], $size['width'], $size['height'])) {
$this->logger
->error('Automated image crop failed using the %toolkit toolkit on %path (%mimetype, %width x %height)', [
'%toolkit' => $image
->getToolkitId(),
'%path' => $image
->getSource(),
'%mimetype' => $image
->getMimeType(),
'%width' => $image
->getWidth(),
'%height' => $image
->getHeight(),
]);
return FALSE;
}
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getSummary() {
$summary = [
'#theme' => 'automated_crop_summary',
'#data' => $this->configuration,
];
$summary += parent::getSummary();
return $summary;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'width' => NULL,
'height' => NULL,
'min_width' => NULL,
'min_height' => NULL,
'max_width' => NULL,
'max_height' => NULL,
'aspect_ratio' => 'NaN',
'provider' => 'automated_crop_default',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['provider'] = [
'#type' => 'select',
'#required' => TRUE,
'#title' => $this
->t('Automated crop provider'),
'#empty_option' => $this
->t("- Select a Provider -"),
'#options' => $this->automatedCropManager
->getProviderOptionsList(),
'#default_value' => $this->configuration['provider'],
'#description' => $this
->t('The name of automated crop provider plugin to use.'),
];
$form['width'] = [
'#type' => 'number',
'#title' => $this
->t('Width'),
'#default_value' => $this->configuration['width'],
'#field_suffix' => ' ' . $this
->t('pixels'),
'#description' => $this
->t("If your sizes W + H not respect original aspect ratio, the system adapt it to ensure you don't deform image."),
];
$form['height'] = [
'#type' => 'number',
'#title' => $this
->t('Height'),
'#default_value' => $this->configuration['height'],
'#field_suffix' => ' ' . $this
->t('pixels'),
'#description' => $this
->t("If your sizes W + H not respect original aspect ratio, the system adapt it to ensure you don't deform image."),
];
$form['min_sizes'] = [
'#type' => 'details',
'#title' => $this
->t('Min sizes limits'),
'#description' => $this
->t('Define crop size minimum limit.'),
'#open' => FALSE,
];
$form['min_sizes']['min_width'] = [
'#type' => 'number',
'#title' => $this
->t('Min Width'),
'#default_value' => $this->configuration['min_width'],
'#field_suffix' => ' ' . $this
->t('pixels'),
];
$form['min_sizes']['min_height'] = [
'#type' => 'number',
'#title' => $this
->t('Min Height'),
'#default_value' => $this->configuration['min_height'],
'#field_suffix' => ' ' . $this
->t('pixels'),
];
$form['max_sizes'] = [
'#type' => 'details',
'#title' => $this
->t('Max sizes limits'),
'#description' => $this
->t('Define crop size maximum limit.'),
'#open' => FALSE,
];
$form['max_sizes']['max_width'] = [
'#type' => 'number',
'#title' => $this
->t('Max Width'),
'#default_value' => $this->configuration['max_width'],
'#field_suffix' => ' ' . $this
->t('pixels'),
];
$form['max_sizes']['max_height'] = [
'#type' => 'number',
'#title' => $this
->t('Max Height'),
'#default_value' => $this->configuration['max_height'],
'#field_suffix' => ' ' . $this
->t('pixels'),
];
$form['aspect_ratio'] = [
'#title' => $this
->t('Aspect Ratio'),
'#type' => 'textfield',
'#default_value' => $this->configuration['aspect_ratio'],
'#attributes' => [
'placeholder' => 'W:H',
],
'#description' => $this
->t('Set an aspect ratio <b>eg: 16:9</b> or leave this empty for arbitrary aspect ratio'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!empty($form_state
->getValue('aspect_ratio')) && !preg_match(AbstractAutomatedCrop::ASPECT_RATIO_FORMAT_REGEXP, $form_state
->getValue('aspect_ratio'))) {
$form_state
->setError($form['aspect_ratio'], $form['aspect_ratio']['#title'] . ': ' . $this
->t('Invalid aspect ratio format. Should be defined in H:W form.'));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['width'] = $form_state
->getValue('width');
$this->configuration['height'] = $form_state
->getValue('height');
$this->configuration['min_width'] = $form_state
->getValue('min_sizes')['min_width'];
$this->configuration['min_height'] = $form_state
->getValue('min_sizes')['min_height'];
$this->configuration['max_width'] = $form_state
->getValue('max_sizes')['max_width'];
$this->configuration['max_height'] = $form_state
->getValue('max_sizes')['max_height'];
$this->configuration['aspect_ratio'] = $form_state
->getValue('aspect_ratio');
$this->configuration['provider'] = $form_state
->getValue('provider', 'automated_crop_default');
}
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions, $uri) {
/** @var \Drupal\Core\Image\Image $image */
$image = $this->imageFactory
->get($uri);
$sizes = $this
->getAutomatedCrop($image)
->size();
// The new image will have the exact dimensions defined by effect.
$dimensions['width'] = $sizes['width'];
$dimensions['height'] = $sizes['height'];
}
/**
* Gets crop coordinates.
*
* @param \Drupal\Core\Image\ImageInterface $image
* Image object.
*
* @return \Drupal\automated_crop\AutomatedCropInterface|false
* Crop coordinates onto original image.
*/
protected function getAutomatedCrop(ImageInterface $image) {
if (!isset($this->automatedCrop)) {
$this->automatedCrop = FALSE;
$crop_coordinates = $this->automatedCropManager
->createInstance($this->configuration['provider'], [
'image' => $image,
'width' => $this->configuration['width'],
'height' => $this->configuration['height'],
'min_width' => $this->configuration['min_width'],
'min_height' => $this->configuration['min_height'],
'max_width' => $this->configuration['max_width'],
'max_height' => $this->configuration['max_height'],
'aspect_ratio' => $this->configuration['aspect_ratio'],
]);
if ($crop_coordinates) {
$this->automatedCrop = $crop_coordinates;
}
}
return $this->automatedCrop;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AutomatedCropEffect:: |
protected | property | Automated crop object loaded with current image. | |
AutomatedCropEffect:: |
protected | property | AutomatedCrop object. | |
AutomatedCropEffect:: |
protected | property | The image factory service. | |
AutomatedCropEffect:: |
public | function |
Applies an image effect to the image object. Overrides ImageEffectInterface:: |
|
AutomatedCropEffect:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
AutomatedCropEffect:: |
public static | function |
Creates an instance of the plugin. Overrides ImageEffectBase:: |
|
AutomatedCropEffect:: |
public | function |
Gets default configuration for this plugin. Overrides ImageEffectBase:: |
|
AutomatedCropEffect:: |
protected | function | Gets crop coordinates. | |
AutomatedCropEffect:: |
public | function |
Returns a render array summarizing the configuration of the image effect. Overrides ImageEffectBase:: |
|
AutomatedCropEffect:: |
public | function |
Form submission handler. Overrides ConfigurableImageEffectBase:: |
|
AutomatedCropEffect:: |
public | function |
Determines the dimensions of the styled image. Overrides ImageEffectBase:: |
|
AutomatedCropEffect:: |
public | function |
Form validation handler. Overrides ConfigurableImageEffectBase:: |
|
AutomatedCropEffect:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides ImageEffectBase:: |
|
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. |