You are here

public function AbstractAutomatedCrop::setAspectRatio in Automated Crop 8

Set the aspect ratio from plugin object.

If the plugin configuration is set to `NaN` or not respect the pattern of, aspect ratio (W:H) we will calculate if from the original sizes of image. You should be sure to prevent this case to avoid potential, destructive crop.

Return value

self The plugin object with aspect_ratio property set.

Overrides AutomatedCropInterface::setAspectRatio

1 call to AbstractAutomatedCrop::setAspectRatio()
AbstractAutomatedCrop::initCropBox in src/AbstractAutomatedCrop.php
Initializes the properties of the plugins according to the configurations.

File

src/AbstractAutomatedCrop.php, line 214

Class

AbstractAutomatedCrop
Provides a base class for each AutomatedCrop provider plugins.

Namespace

Drupal\automated_crop

Code

public function setAspectRatio() {
  $aspect_ratio = isset($this->configuration['aspect_ratio']) ? $this->configuration['aspect_ratio'] : 'NaN';
  if ('NaN' !== $aspect_ratio && preg_match(self::ASPECT_RATIO_FORMAT_REGEXP, $aspect_ratio)) {
    $this->aspectRatio = $aspect_ratio;
    return $this;
  }
  $gcd = $this
    ->calculateGcd($this->originalImageSizes['width'], $this->originalImageSizes['height']);
  $this->aspectRatio = round($this->originalImageSizes['width'] / $gcd) . ':' . round($this->originalImageSizes['height'] / $gcd);
  return $this;
}