AutomatedCropDefault.php in Automated Crop 8
File
src/Plugin/AutomatedCrop/AutomatedCropDefault.php
View source
<?php
namespace Drupal\automated_crop\Plugin\AutomatedCrop;
use Drupal\automated_crop\AbstractAutomatedCrop;
final class AutomatedCropDefault extends AbstractAutomatedCrop {
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;
}
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));
}
$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;
}
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;
}
protected function findUnknownValue() {
if (!$this
->hasSizes()) {
return FALSE;
}
$valueToSearch = 'width';
if (!empty($this->cropBox['width']) && empty($this->cropBox['height'])) {
$valueToSearch = 'height';
}
return $valueToSearch;
}
protected function calculateUnknownValue($value) {
return round($value * $this->delta);
}
}