You are here

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

Expanded class hierarchy of AutomatedCropEffect

File

src/Plugin/ImageEffect/AutomatedCropEffect.php, line 24

Namespace

Drupal\automated_crop\Plugin\ImageEffect
View 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

Namesort descending Modifiers Type Description Overrides
AutomatedCropEffect::$automatedCrop protected property Automated crop object loaded with current image.
AutomatedCropEffect::$automatedCropManager protected property AutomatedCrop object.
AutomatedCropEffect::$imageFactory protected property The image factory service.
AutomatedCropEffect::applyEffect public function Applies an image effect to the image object. Overrides ImageEffectInterface::applyEffect
AutomatedCropEffect::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
AutomatedCropEffect::create public static function Creates an instance of the plugin. Overrides ImageEffectBase::create
AutomatedCropEffect::defaultConfiguration public function Gets default configuration for this plugin. Overrides ImageEffectBase::defaultConfiguration
AutomatedCropEffect::getAutomatedCrop protected function Gets crop coordinates.
AutomatedCropEffect::getSummary public function Returns a render array summarizing the configuration of the image effect. Overrides ImageEffectBase::getSummary
AutomatedCropEffect::submitConfigurationForm public function Form submission handler. Overrides ConfigurableImageEffectBase::submitConfigurationForm
AutomatedCropEffect::transformDimensions public function Determines the dimensions of the styled image. Overrides ImageEffectBase::transformDimensions
AutomatedCropEffect::validateConfigurationForm public function Form validation handler. Overrides ConfigurableImageEffectBase::validateConfigurationForm
AutomatedCropEffect::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides ImageEffectBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ImageEffectBase::$logger protected property A logger instance.
ImageEffectBase::$uuid protected property The image effect ID.
ImageEffectBase::$weight protected property The weight of the image effect.
ImageEffectBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ImageEffectBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ImageEffectBase::getDerivativeExtension public function Returns the extension of the derivative after applying this image effect. Overrides ImageEffectInterface::getDerivativeExtension 1
ImageEffectBase::getUuid public function Returns the unique ID representing the image effect. Overrides ImageEffectInterface::getUuid
ImageEffectBase::getWeight public function Returns the weight of the image effect. Overrides ImageEffectInterface::getWeight
ImageEffectBase::label public function Returns the image effect label. Overrides ImageEffectInterface::label
ImageEffectBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ImageEffectBase::setWeight public function Sets the weight for this image effect. Overrides ImageEffectInterface::setWeight
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.