Skip to content

Commit

Permalink
add patch #3023475 and compatibility with D9
Browse files Browse the repository at this point in the history
  • Loading branch information
Aneida committed Oct 26, 2021
1 parent c367bf1 commit af400e8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
8 changes: 7 additions & 1 deletion automated_crop.info.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
name: Automated Crop
description: 'Provides automated crop features.'
core: 8.x
# core: 8.x
package: Media
type: module
dependencies:
- drupal:image
- drupal:user

# Information added by Drupal.org packaging script on 2018-07-14
version: '2.x'
core_version_requirement: ^8 || ^9
project: 'automated_crop'
datestamp: 1531559029
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Automated Crop Api Provider
description: 'Provides automated crop for crop api as fallback automatic crop provider.'
core: 8.x
core_version_requirement: ^8 || ^9
package: Media
type: module
dependencies:
Expand Down
5 changes: 3 additions & 2 deletions src/AutomatedCropInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Drupal\automated_crop;

use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;

/**
* Provides an interface defining the AutomatedCrop plugin objects.
*/
interface AutomatedCropInterface extends PluginInspectionInterface, ConfigurablePluginInterface {
interface AutomatedCropInterface extends PluginInspectionInterface, ConfigurableInterface, DependentPluginInterface {

/**
* Returns the display label.
Expand Down
43 changes: 31 additions & 12 deletions src/Plugin/AutomatedCrop/AutomatedCropDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,25 @@ public function calculateCropBoxSize() {
$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(!$this->cropBox['width'] || !$this->cropBox['height']) {
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']);
} else {
$width = $this->cropBox['width'];
$height = $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;
Expand All @@ -67,7 +73,7 @@ public function calculateCropBoxSize() {
protected function automatedCropBoxCalculation() {
$delta = $this->getDelta();
$width = $this->originalImageSizes['width'];
$height = round($this->cropBox['max_height'] * $delta);
$height = $this->originalImageSizes['height'];

if (!empty($this->cropBox['max_height']) && $height > $this->cropBox['max_height']) {
$height = $this->cropBox['max_height'];
Expand All @@ -79,6 +85,19 @@ protected function automatedCropBoxCalculation() {
$height = round($width * $delta);
}

if (!empty($this->cropBox['aspect_ratio'])) {
$measures = explode(':', $this->cropBox['aspect_ratio']);
$aspectRatio = $measures[0]/$measures[1];

if ($width > $height) {
$width = round($this->originalImageSizes['height'] * $aspectRatio);
$height = $this->originalImageSizes['height'];
} else {
$height = round($this->originalImageSizes['width'] * $aspectRatio);
$width = $this->originalImageSizes['width'];
}
}

$this->cropBox['width'] = $width;
$this->cropBox['height'] = $height;
}
Expand Down
11 changes: 8 additions & 3 deletions src/Tests/AutomatedCropFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Drupal\automated_crop\Tests;

use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;

/**
* Functional tests for Automated Crop.
*
* @group crop
*/
class AutomatedCropFunctionalTest extends WebTestBase {
class AutomatedCropFunctionalTest extends BrowserTestBase {

/**
* Modules to enable.
Expand All @@ -18,6 +18,11 @@ class AutomatedCropFunctionalTest extends WebTestBase {
*/
public static $modules = ['automated_crop', 'file'];

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* Admin user.
*
Expand All @@ -41,7 +46,7 @@ protected function setUp() {
$this->adminUser = $this->drupalCreateUser(['administer image styles']);

// Create test image style.
$this->testStyle = $this->container->get('entity.manager')->getStorage('image_style')->create([
$this->testStyle = $this->container->get('entity_type.manager')->getStorage('image_style')->create([
'name' => 'test',
'label' => 'Test image style',
'effects' => [],
Expand Down

0 comments on commit af400e8

Please sign in to comment.