View source
<?php
namespace Drupal\automated_crop;
use Drupal\Component\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
abstract class AbstractAutomatedCrop extends PluginBase implements AutomatedCropInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
const ASPECT_RATIO_FORMAT_REGEXP = '/^\\d{1,3}+:\\d{1,3}+$/';
protected $label;
protected $entityTypeManager;
protected $cropBox = [
'width' => 0,
'height' => 0,
'min_width' => 0,
'min_height' => 0,
'max_width' => 0,
'max_height' => 0,
'x' => 0,
'y' => 0,
'aspect_ratio' => 'NaN',
];
protected $originalImageSizes;
protected $aspectRatio;
protected $image;
protected $autoCropArea = 1;
protected $delta;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->initCropBox();
$this
->calculateCropBoxSize();
$this
->calculateCropBoxCoordinates();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
public abstract function calculateCropBoxSize();
public abstract function calculateCropBoxCoordinates();
public function initCropBox() {
$this
->setImage($this->configuration['image']);
$this
->setCropBoxProperties();
$this
->setOriginalSize();
$this
->setAspectRatio();
$this
->setDelta();
return $this;
}
public function label() {
return $this->label;
}
public function setOriginalSize() {
$this->originalImageSizes['width'] = (int) $this->image
->getWidth();
$this->originalImageSizes['height'] = (int) $this->image
->getHeight();
if (empty($this->cropBox['max_width']) && empty($this->cropBox['max_height'])) {
$this
->setMaxSizes($this->originalImageSizes['width'], $this->originalImageSizes['height']);
}
return $this;
}
public function getOriginalSize() {
$this->originalImageSizes;
}
public function setImage($image) {
$this->image = $image;
return $this;
}
public function getImage() {
return $this->image;
}
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;
}
public function setDelta() {
$ratio = explode(':', $this
->getAspectRatio());
$this->delta = (int) $ratio['1'] / (int) $ratio['0'];
return $this;
}
public function getDelta() {
return $this->delta;
}
public function getAspectRatio() {
return $this->aspectRatio;
}
public function setAnchor(array $coordinates = []) {
array_merge($coordinates, $this->cropBox);
return $this;
}
public function anchor() {
return [
'x' => (int) $this->cropBox['x'],
'y' => (int) $this->cropBox['y'],
];
}
public function size() {
return [
'width' => $this->cropBox['width'],
'height' => $this->cropBox['height'],
];
}
public function setMaxSizes($maxWidth, $maxHeight) {
if (!empty($maxWidth)) {
$this->cropBox['max_width'] = $maxWidth;
}
if (!empty($maxHeight)) {
$this->cropBox['max_height'] = $maxHeight;
}
return $this;
}
public function setCropBoxSize(int $width, int $height) {
$this->cropBox['width'] = max($this->cropBox['min_width'], $width * $this->autoCropArea);
$this->cropBox['height'] = max($this->cropBox['min_height'], $height * $this->autoCropArea);
return $this;
}
public function calculateDependencies() {
return [];
}
public function defaultConfiguration() {
return [];
}
public function setConfiguration(array $configuration) {
$configuration += $this
->defaultConfiguration();
$this->configuration = $configuration;
}
public function getConfiguration() {
return $this->configuration;
}
public function setAutoCropArea($num) {
$this->autoCropArea = $num;
return $this;
}
public function hasHardSizes() {
return !empty($this->cropBox['width']) && !empty($this->cropBox['height']) ? TRUE : FALSE;
}
public function hasSizes() {
if (!empty($this->cropBox['width'])) {
return TRUE;
}
if (!empty($this->cropBox['height'])) {
return TRUE;
}
return FALSE;
}
protected static function calculateGcd($a, $b) {
if ($b > $a) {
$gcd = self::calculateGcd($b, $a);
}
else {
while ($b > 0) {
$t = $b;
$b = $a % $b;
$a = $t;
}
$gcd = $a;
}
return $gcd;
}
protected function setCropBoxProperties() {
foreach ($this->configuration as $element => $value) {
if (array_key_exists($element, $this->cropBox) && !empty($value)) {
$this->cropBox[$element] = $value;
}
}
return $this;
}
}