You are here

final class AutomatedCropDefault in Automated Crop 8

Class Generic routing entity mapper.

Plugin annotation


@AutomatedCrop(
  id = "automated_crop_default",
  label = @Translation("Automated crop"),
  description = @Translation("The default strategy for automatic crop."),
)

Hierarchy

Expanded class hierarchy of AutomatedCropDefault

File

src/Plugin/AutomatedCrop/AutomatedCropDefault.php, line 16

Namespace

Drupal\automated_crop\Plugin\AutomatedCrop
View source
final class AutomatedCropDefault extends AbstractAutomatedCrop {

  /**
   * {@inheritdoc}
   */
  public function calculateCropBoxCoordinates() {
    $this->cropBox['x'] = $this->originalImageSizes['width'] / 2 - $this->cropBox['width'] / 2;
    $this->cropBox['y'] = $this->originalImageSizes['height'] / 2 - $this->cropBox['height'] / 2;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateCropBoxSize() {
    if (!$this
      ->hasSizes() && !$this
      ->hasHardSizes()) {
      $this
        ->automatedCropBoxCalculation();
    }
    if ('width' === $this
      ->findUnknownValue()) {
      $value = !empty($this->cropBox['width']) ? $this->cropBox['width'] : $this->cropBox['max_width'];
      $this
        ->setCropBoxSize($this
        ->calculateUnknownValue($value), $this->cropBox['height']);
    }
    if ('height' === $this
      ->findUnknownValue()) {
      $value = !empty($this->cropBox['height']) ? $this->cropBox['height'] : $this->cropBox['max_height'];
      $this
        ->setCropBoxSize($this->cropBox['width'], $this
        ->calculateUnknownValue($value));
    }

    // Initialize auto crop area & unsure we can't exceed original image sizes.
    $width = min(max($this->cropBox['width'], $this->cropBox['min_width']), $this->cropBox['max_width']);
    $height = min(max($this->cropBox['height'], $this->cropBox['min_height']), $this->cropBox['max_height']);
    $this
      ->setCropBoxSize($width, $height);
    return $this;
  }

  /**
   * Calculate size automatically based on origin image width.
   *
   * This method admit you want to crop the height of your image in another,
   * ratio with respect of original image homothety. If you not define any,
   * ratio in plugin configuration, nothing happen. If you define a new ratio,
   * your image will conserve his original width but the height will,
   * calculated to respect plugin ratio given.
   *
   * This method contains a system that avoids exceeding,
   * the maximum sizes of the cropBox. Pay attention with the,
   * configurations of max width/height.
   */
  protected function automatedCropBoxCalculation() {
    $delta = $this
      ->getDelta();
    $width = $this->originalImageSizes['width'];
    $height = round($this->cropBox['max_height'] * $delta);
    if (!empty($this->cropBox['max_height']) && $height > $this->cropBox['max_height']) {
      $height = $this->cropBox['max_height'];
      $width = round($height * $delta);
    }
    if (!empty($this->cropBox['max_width']) && $width > $this->cropBox['max_width']) {
      $width = $this->cropBox['max_width'];
      $height = round($width * $delta);
    }
    $this->cropBox['width'] = $width;
    $this->cropBox['height'] = $height;
  }

  /**
   * Evaluate if with or height need to be calculated.
   *
   * If we have already ALL cropBox sizes we just need to apply,
   * it don't need to evaluate missing values.
   *
   * @return bool|string
   *   The value to find or False if cropBox have any sizes found.
   */
  protected function findUnknownValue() {
    if (!$this
      ->hasSizes()) {
      return FALSE;
    }
    $valueToSearch = 'width';
    if (!empty($this->cropBox['width']) && empty($this->cropBox['height'])) {
      $valueToSearch = 'height';
    }
    return $valueToSearch;
  }

  /**
   * Calculate the new value of given width or height respecting homothety.
   *
   * @param int $value
   *   Value to convert with image delta to found compatible new sizes.
   *
   * @return int
   *   The new height or width respect the homothety of image.
   */
  protected function calculateUnknownValue($value) {
    return round($value * $this->delta);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractAutomatedCrop::$aspectRatio protected property The machine name of this crop type.
AbstractAutomatedCrop::$autoCropArea protected property The percentage of automatic cropping area when initializes.
AbstractAutomatedCrop::$cropBox protected property All available value expected to calculate crop box area.
AbstractAutomatedCrop::$delta protected property The delta obtained by dividing the height by width.
AbstractAutomatedCrop::$entityTypeManager protected property The entity type manager service.
AbstractAutomatedCrop::$image protected property The image object to crop.
AbstractAutomatedCrop::$label protected property Plugin label.
AbstractAutomatedCrop::$originalImageSizes protected property The machine name of this crop type.
AbstractAutomatedCrop::anchor public function Gets crop anchor (top-left corner of crop area). Overrides AutomatedCropInterface::anchor
AbstractAutomatedCrop::ASPECT_RATIO_FORMAT_REGEXP constant Aspect ratio validation regexp.
AbstractAutomatedCrop::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
AbstractAutomatedCrop::calculateGcd protected static function Calculate the greatest common denominator of two numbers.
AbstractAutomatedCrop::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AbstractAutomatedCrop::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration
AbstractAutomatedCrop::getAspectRatio public function Get the aspect ratio. Overrides AutomatedCropInterface::getAspectRatio
AbstractAutomatedCrop::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
AbstractAutomatedCrop::getDelta public function Gets the delta of image. Overrides AutomatedCropInterface::getDelta
AbstractAutomatedCrop::getImage public function Gets the plugin image object to crop. Overrides AutomatedCropInterface::getImage
AbstractAutomatedCrop::getOriginalSize public function Get the original image sizes of image to be cropped. Overrides AutomatedCropInterface::getOriginalSize
AbstractAutomatedCrop::hasHardSizes public function Evaluate if crop box has Hard sizes defined.
AbstractAutomatedCrop::hasSizes public function Evaluate if user have set one of crop box area sizes.
AbstractAutomatedCrop::initCropBox public function Initializes the properties of the plugins according to the configurations.
AbstractAutomatedCrop::label public function Returns the display label. Overrides AutomatedCropInterface::label
AbstractAutomatedCrop::setAnchor public function Set crop anchor (top-left corner of crop area). Overrides AutomatedCropInterface::setAnchor
AbstractAutomatedCrop::setAspectRatio public function Set the aspect ratio from plugin object. Overrides AutomatedCropInterface::setAspectRatio
AbstractAutomatedCrop::setAutoCropArea public function Define the percentage of automatic cropping area when initializes. Overrides AutomatedCropInterface::setAutoCropArea
AbstractAutomatedCrop::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
AbstractAutomatedCrop::setCropBoxProperties protected function Set all crop box properties from plugin configuration.
AbstractAutomatedCrop::setCropBoxSize public function Set crop box sizes. Overrides AutomatedCropInterface::setCropBoxSize
AbstractAutomatedCrop::setDelta public function Calculate and set the delta to apply of each size calculation of cropBox. Overrides AutomatedCropInterface::setDelta
AbstractAutomatedCrop::setImage public function Set the image resource from plugin configuration to be cropped. Overrides AutomatedCropInterface::setImage
AbstractAutomatedCrop::setMaxSizes public function Gets crop box size. Overrides AutomatedCropInterface::setMaxSizes
AbstractAutomatedCrop::setOriginalSize public function Set the original sizes of image. Overrides AutomatedCropInterface::setOriginalSize
AbstractAutomatedCrop::size public function Gets crop box size. Overrides AutomatedCropInterface::size
AbstractAutomatedCrop::__construct public function Constructs AutomatedCrop plugin. Overrides PluginBase::__construct
AutomatedCropDefault::automatedCropBoxCalculation protected function Calculate size automatically based on origin image width.
AutomatedCropDefault::calculateCropBoxCoordinates public function Calculation of the coordinates of the crop area. Overrides AbstractAutomatedCrop::calculateCropBoxCoordinates
AutomatedCropDefault::calculateCropBoxSize public function Calculation of the dimensions of the crop area. Overrides AbstractAutomatedCrop::calculateCropBoxSize
AutomatedCropDefault::calculateUnknownValue protected function Calculate the new value of given width or height respecting homothety.
AutomatedCropDefault::findUnknownValue protected function Evaluate if with or height need to be calculated.
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.