From 055d57940395bcc86b945ad16e52b4bca25b8552 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 24 May 2014 20:34:28 +0200 Subject: [PATCH] fixed bug when resizing exceeds given values --- src/Intervention/Image/Size.php | 86 +++++++++++++++++++++++++-------- tests/SizeTest.php | 63 ++++++++++++++++++++++-- 2 files changed, 124 insertions(+), 25 deletions(-) diff --git a/src/Intervention/Image/Size.php b/src/Intervention/Image/Size.php index 3c6f6f85..b48a0b9f 100644 --- a/src/Intervention/Image/Size.php +++ b/src/Intervention/Image/Size.php @@ -109,6 +109,71 @@ class Size ); } + // new size with dominant width + $dominant_w_size = clone $this; + $dominant_w_size->resizeHeight($height, $callback); + $dominant_w_size->resizeWidth($width, $callback); + + // new size with dominant height + $dominant_h_size = clone $this; + $dominant_h_size->resizeWidth($width, $callback); + $dominant_h_size->resizeHeight($height, $callback); + + // decide which size to use + if ($dominant_h_size->fitsInto(new self($width, $height))) { + $this->set($dominant_h_size->width, $dominant_h_size->height); + } else { + $this->set($dominant_w_size->width, $dominant_w_size->height); + } + + return $this; + } + + /** + * Scale size according to given constraints + * + * @param integer $width + * @param Closure $callback + * @return Size + */ + private function resizeWidth($width, Closure $callback = null) + { + $constraint = $this->getConstraint($callback); + + if ($constraint->isFixed(Constraint::UPSIZE)) { + $max_width = $constraint->getSize()->getWidth(); + $max_height = $constraint->getSize()->getHeight(); + } + + if (is_numeric($width)) { + + if ($constraint->isFixed(Constraint::UPSIZE)) { + $this->width = ($width > $max_width) ? $max_width : $width; + } else { + $this->width = $width; + } + + if ($constraint->isFixed(Constraint::ASPECTRATIO)) { + $h = intval(round($this->width / $constraint->getSize()->getRatio())); + + if ($constraint->isFixed(Constraint::UPSIZE)) { + $this->height = ($h > $max_height) ? $max_height : $h; + } else { + $this->height = $h; + } + } + } + } + + /** + * Scale size according to given constraints + * + * @param integer $width + * @param Closure $callback + * @return Size + */ + private function resizeHeight($height, Closure $callback = null) + { $constraint = $this->getConstraint($callback); if ($constraint->isFixed(Constraint::UPSIZE)) { @@ -134,27 +199,6 @@ class Size } } } - - if (is_numeric($width)) { - - if ($constraint->isFixed(Constraint::UPSIZE)) { - $this->width = ($width > $max_width) ? $max_width : $width; - } else { - $this->width = $width; - } - - if ($constraint->isFixed(Constraint::ASPECTRATIO)) { - $h = intval(round($this->width / $constraint->getSize()->getRatio())); - - if ($constraint->isFixed(Constraint::UPSIZE)) { - $this->height = ($h > $max_height) ? $max_height : $h; - } else { - $this->height = $h; - } - } - } - - return $this; } /** diff --git a/tests/SizeTest.php b/tests/SizeTest.php index 22cec778..48b5ecdf 100644 --- a/tests/SizeTest.php +++ b/tests/SizeTest.php @@ -77,6 +77,11 @@ class SizeTest extends PHPUnit_Framework_TestCase $this->assertEquals(1000, $size->width); $this->assertEquals(750, $size->height); + $size = new Size(800, 600); + $size->resize(2000, 1000, function ($c) { $c->aspectRatio(); }); + $this->assertEquals(1333, $size->width); + $this->assertEquals(1000, $size->height); + $size = new Size(800, 600); $size->resize(null, 3000, function ($c) { $c->aspectRatio(); }); $this->assertEquals(4000, $size->width); @@ -92,6 +97,11 @@ class SizeTest extends PHPUnit_Framework_TestCase $this->assertEquals(100, $size->width); $this->assertEquals(75, $size->height); + $size = new Size(800, 600); + $size->resize(400, 100, function ($c) { $c->aspectRatio(); }); + $this->assertEquals(133, $size->width); + $this->assertEquals(100, $size->height); + $size = new Size(800, 600); $size->resize(null, 300, function ($c) { $c->aspectRatio(); }); $this->assertEquals(400, $size->width); @@ -111,6 +121,21 @@ class SizeTest extends PHPUnit_Framework_TestCase $size->resize(223, null, function ($c) { $c->aspectRatio(); }); $this->assertEquals(223, $size->width); $this->assertEquals(167, $size->height); + + $size = new Size(600, 800); + $size->resize(300, 300, function ($c) { $c->aspectRatio(); }); + $this->assertEquals(225, $size->width); + $this->assertEquals(300, $size->height); + + $size = new Size(800, 600); + $size->resize(400, 10, function ($c) { $c->aspectRatio(); }); + $this->assertEquals(13, $size->width); + $this->assertEquals(10, $size->height); + + $size = new Size(800, 600); + $size->resize(1000, 1200, function ($c) { $c->aspectRatio(); }); + $this->assertEquals(1000, $size->width); + $this->assertEquals(750, $size->height); } public function testResizeWithCallbackUpsize() @@ -125,6 +150,11 @@ class SizeTest extends PHPUnit_Framework_TestCase $this->assertEquals(400, $size->width); $this->assertEquals(600, $size->height); + $size = new Size(800, 600); + $size->resize(1000, 400, function ($c) { $c->upsize(); }); + $this->assertEquals(800, $size->width); + $this->assertEquals(400, $size->height); + $size = new Size(800, 600); $size->resize(400, 300, function ($c) { $c->upsize(); }); $this->assertEquals(400, $size->width); @@ -155,8 +185,13 @@ class SizeTest extends PHPUnit_Framework_TestCase $size = new Size(800, 600); $size->resize(1000, 300, function ($c) { $c->aspectRatio(); $c->upsize(); }); - $this->assertEquals(800, $size->width); - $this->assertEquals(600, $size->height); + $this->assertEquals(400, $size->width); + $this->assertEquals(300, $size->height); + + $size = new Size(800, 600); + $size->resize(400, 1000, function ($c) { $c->aspectRatio(); $c->upsize(); }); + $this->assertEquals(400, $size->width); + $this->assertEquals(300, $size->height); $size = new Size(800, 600); $size->resize(400, null, function ($c) { $c->aspectRatio(); $c->upsize(); }); @@ -168,6 +203,16 @@ class SizeTest extends PHPUnit_Framework_TestCase $this->assertEquals(400, $size->width); $this->assertEquals(300, $size->height); + $size = new Size(800, 600); + $size->resize(1000, null, function ($c) { $c->aspectRatio(); $c->upsize(); }); + $this->assertEquals(800, $size->width); + $this->assertEquals(600, $size->height); + + $size = new Size(800, 600); + $size->resize(null, 1000, function ($c) { $c->aspectRatio(); $c->upsize(); }); + $this->assertEquals(800, $size->width); + $this->assertEquals(600, $size->height); + $size = new Size(800, 600); $size->resize(100, 100, function ($c) { $c->aspectRatio(); $c->upsize(); }); $this->assertEquals(100, $size->width); @@ -175,8 +220,18 @@ class SizeTest extends PHPUnit_Framework_TestCase $size = new Size(800, 600); $size->resize(300, 200, function ($c) { $c->aspectRatio(); $c->upsize(); }); - $this->assertEquals(300, $size->width); - $this->assertEquals(225, $size->height); + $this->assertEquals(267, $size->width); + $this->assertEquals(200, $size->height); + + $size = new Size(600, 800); + $size->resize(300, 300, function ($c) { $c->aspectRatio(); $c->upsize(); }); + $this->assertEquals(225, $size->width); + $this->assertEquals(300, $size->height); + + $size = new Size(800, 600); + $size->resize(400, 10, function ($c) { $c->aspectRatio(); $c->upsize(); }); + $this->assertEquals(13, $size->width); + $this->assertEquals(10, $size->height); } public function testRelativePosition()