Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Adding a test for the upscaling prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Nov 9, 2016
1 parent 2b314e2 commit 5661782
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Lib/ImageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function squareCenterCrop(array $options = []) {
$width = $imageSize['x'];
$height = $imageSize['y'];

if (isset($options['preventUpscaling']) && $options['preventUpscaling'] === true) {
if (isset($options['preventUpscale']) && $options['preventUpscale'] === true) {
if ($options['size'] > $width || $options['size'] > $height) {
return;
}
Expand Down Expand Up @@ -253,9 +253,9 @@ public function widen(array $options = []) {
throw new \InvalidArgumentException(__d('imagine', 'You must pass a size value!'));
}

if (isset($options['preventUpscaling']) && $options['preventUpscaling'] === true) {
$imageSize = $this->getImageSize($Model, $Image);
if ($imageSize['x'] > $options['size']) {
if (isset($options['preventUpscale']) && $options['preventUpscale'] === true) {
$imageSize = $this->getImageSize();
if ($options['size'] > $imageSize['x']) {
return;
}
}
Expand All @@ -277,9 +277,9 @@ public function heighten(array $options = []) {
throw new \InvalidArgumentException(__d('imagine', 'You must pass a size value!'));
}

if (isset($options['preventUpscaling']) && $options['preventUpscaling'] === true) {
if (isset($options['preventUpscale']) && $options['preventUpscale'] === true) {
$imageSize = $this->getImageSize();
if ($imageSize['y'] > $options['size']) {
if ($options['size'] > $imageSize['y']) {
return;
}
}
Expand Down Expand Up @@ -361,7 +361,7 @@ public function scale(array $options = []) {
throw new \InvalidArgumentException(__d('imagine', 'You must pass a factor value!'));
}

if (isset($options['preventUpscaling']) && $options['preventUpscaling'] === true && $options['factor'] > 1.0) {
if (isset($options['preventUpscale']) && $options['preventUpscale'] === true && $options['factor'] > 1.0) {
return;
}

Expand Down Expand Up @@ -430,7 +430,7 @@ public function thumbnail(array $options = []) {
}

$imageSize = $this->getImageSize();
if (isset($options['preventUpscaling']) && $options['preventUpscaling'] === true) {
if (isset($options['preventUpscale']) && $options['preventUpscale'] === true) {
if (isset($options['height']) && $options['height'] > $imageSize['y']) {
return;
}
Expand All @@ -450,6 +450,7 @@ public function thumbnail(array $options = []) {
}

$size = new Box($options['width'], $options['height']);
$imageSize = $this->_image->getSize();
$ratios = array(
$size->getWidth() / $imageSize->getWidth(),
$size->getHeight() / $imageSize->getHeight()
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase/Model/Behavior/ImagineBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,50 @@ public function testWidenAndHeighten() {
$this->assertEquals($result, [151, 200, 'x' => 151, 'y' => 200]);
}

/**
* testPreventUpscale
*
* @return void
*/
public function testPreventUpscale() {
$image = Plugin::path('Burzum/Imagine') . 'tests' . DS . 'Fixture' . DS . 'titus.jpg';

// Height
$this->Model->processImage($image, TMP . 'heighten-upscale.jpg', [], [
'heighten' => [
'size' => 2000,
'preventUpscale' => true
],
]
);

$result = $this->Model->getImageSize(TMP . 'heighten-upscale.jpg');
$this->assertEquals($result, [500, 664, 'x' => 500, 'y' => 664]);

// Width
$this->Model->processImage($image, TMP . 'widen-upscale.jpg', [], [
'widen' => [
'size' => 2000,
'preventUpscale' => true
]
]);

$result = $this->Model->getImageSize(TMP . 'widen-upscale.jpg');
$this->assertEquals($result, [500, 664, 'x' => 500, 'y' => 664]);

// Thumbnail
$this->Model->processImage($image, TMP . 'thumbnail-upscale.jpg', [], [
'thumbnail' => [
'height' => 2000,
'width' => 2000,
'preventUpscale' => true
]
]);

$result = $this->Model->getImageSize(TMP . 'thumbnail-upscale.jpg');
$this->assertEquals($result, [500, 664, 'x' => 500, 'y' => 664]);
}

/**
* testScale
*
Expand Down

0 comments on commit 5661782

Please sign in to comment.