From 2d69ff5a5de2189346ffc4bcbe24ef92146dafe4 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 26 Jul 2014 11:27:23 +0200 Subject: [PATCH] added optional constraints for fit() --- .../Image/Gd/Commands/FitCommand.php | 7 +++++-- .../Image/Imagick/Commands/FitCommand.php | 15 ++++++++------ tests/FitCommandTest.php | 20 +++++++++++-------- tests/GdSystemTest.php | 13 ++++++++++++ tests/ImagickSystemTest.php | 13 ++++++++++++ 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/Intervention/Image/Gd/Commands/FitCommand.php b/src/Intervention/Image/Gd/Commands/FitCommand.php index c49afc82..fb688711 100644 --- a/src/Intervention/Image/Gd/Commands/FitCommand.php +++ b/src/Intervention/Image/Gd/Commands/FitCommand.php @@ -17,12 +17,15 @@ class FitCommand extends ResizeCommand { $width = $this->argument(0)->type('digit')->required()->value(); $height = $this->argument(1)->type('digit')->value($width); + $constraints = $this->argument(2)->type('closure')->value(); // calculate size - $fitted = $image->getSize()->fit(new Size($width, $height)); + $cropped = $image->getSize()->fit(new Size($width, $height)); + $resized = clone $cropped; + $resized = $resized->resize($width, $height, $constraints); // modify image - $this->modify($image, 0, 0, $fitted->pivot->x, $fitted->pivot->y, $width, $height, $fitted->getWidth(), $fitted->getHeight()); + $this->modify($image, 0, 0, $cropped->pivot->x, $cropped->pivot->y, $resized->getWidth(), $resized->getHeight(), $cropped->getWidth(), $cropped->getHeight()); return true; } diff --git a/src/Intervention/Image/Imagick/Commands/FitCommand.php b/src/Intervention/Image/Imagick/Commands/FitCommand.php index 7061b9ec..3d84cd6b 100644 --- a/src/Intervention/Image/Imagick/Commands/FitCommand.php +++ b/src/Intervention/Image/Imagick/Commands/FitCommand.php @@ -16,20 +16,23 @@ class FitCommand extends \Intervention\Image\Commands\AbstractCommand { $width = $this->argument(0)->type('digit')->required()->value(); $height = $this->argument(1)->type('digit')->value($width); + $constraints = $this->argument(2)->type('closure')->value(); // calculate size - $fitted = $image->getSize()->fit(new Size($width, $height)); + $cropped = $image->getSize()->fit(new Size($width, $height)); + $resized = clone $cropped; + $resized = $resized->resize($width, $height, $constraints); // crop image $image->getCore()->cropImage( - $fitted->width, - $fitted->height, - $fitted->pivot->x, - $fitted->pivot->y + $cropped->width, + $cropped->height, + $cropped->pivot->x, + $cropped->pivot->y ); // resize image - $image->getCore()->resizeImage($width, $height, \Imagick::FILTER_BOX, 1); + $image->getCore()->resizeImage($resized->getWidth(), $resized->getHeight(), \Imagick::FILTER_BOX, 1); $image->getCore()->setImagePage(0,0,0,0); return true; diff --git a/tests/FitCommandTest.php b/tests/FitCommandTest.php index ec2cdccb..af134020 100644 --- a/tests/FitCommandTest.php +++ b/tests/FitCommandTest.php @@ -12,12 +12,13 @@ class FitCommandTest extends PHPUnit_Framework_TestCase public function testGd() { - $fitted_size = Mockery::mock('\Intervention\Image\Size', array(800, 400)); - $fitted_size->shouldReceive('getWidth')->once()->andReturn(800); - $fitted_size->shouldReceive('getHeight')->once()->andReturn(400); - $fitted_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100)); + $cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400)); + $cropped_size->shouldReceive('getWidth')->times(2)->andReturn(800); + $cropped_size->shouldReceive('getHeight')->times(2)->andReturn(400); + $cropped_size->shouldReceive('resize')->with(200, 100, null)->once()->andReturn($cropped_size); + $cropped_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100)); $original_size = Mockery::mock('\Intervention\Image\Size', array(800, 600)); - $original_size->shouldReceive('fit')->once()->andReturn($fitted_size); + $original_size->shouldReceive('fit')->once()->andReturn($cropped_size); $resource = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); $image = Mockery::mock('Intervention\Image\Image'); $image->shouldReceive('getSize')->once()->andReturn($original_size); @@ -30,10 +31,13 @@ class FitCommandTest extends PHPUnit_Framework_TestCase public function testImagick() { - $fitted_size = Mockery::mock('\Intervention\Image\Size', array(800, 400)); - $fitted_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100)); + $cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400)); + $cropped_size->shouldReceive('getWidth')->once()->andReturn(200); + $cropped_size->shouldReceive('getHeight')->once()->andReturn(100); + $cropped_size->shouldReceive('resize')->with(200, 100, null)->once()->andReturn($cropped_size); + $cropped_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100)); $original_size = Mockery::mock('\Intervention\Image\Size', array(800, 600)); - $original_size->shouldReceive('fit')->once()->andReturn($fitted_size); + $original_size->shouldReceive('fit')->once()->andReturn($cropped_size); $imagick = Mockery::mock('Imagick'); $imagick->shouldReceive('cropimage')->with(800, 400, 0, 100)->andReturn(true); $imagick->shouldReceive('resizeimage')->with(200, 100, \Imagick::FILTER_BOX, 1)->andReturn(true); diff --git a/tests/GdSystemTest.php b/tests/GdSystemTest.php index 4440ab3e..d66ede0f 100644 --- a/tests/GdSystemTest.php +++ b/tests/GdSystemTest.php @@ -496,6 +496,19 @@ class GdSystemTest extends PHPUnit_Framework_TestCase $this->assertTransparentPosition($img, 6, 2); } + public function testFitImageWithConstraintUpsize() + { + $img = $this->manager()->make('tests/images/trim.png'); + $img->fit(300, 150, function ($constraint) {$constraint->upsize();}); + $this->assertInternalType('int', $img->getWidth()); + $this->assertInternalType('int', $img->getHeight()); + $this->assertEquals(50, $img->getWidth()); + $this->assertEquals(25, $img->getHeight()); + $this->assertColorAtPosition('#00aef0', $img, 0, 0); + $this->assertColorAtPosition('#afa94c', $img, 17, 0); + $this->assertColorAtPosition('#ffa601', $img, 24, 0); + } + public function testFlipImageHorizontal() { $img = $this->manager()->make('tests/images/tile.png'); diff --git a/tests/ImagickSystemTest.php b/tests/ImagickSystemTest.php index 944c8fe6..ffaff417 100644 --- a/tests/ImagickSystemTest.php +++ b/tests/ImagickSystemTest.php @@ -496,6 +496,19 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase $this->assertTransparentPosition($img, 6, 2); } + public function testFitImageWithConstraintUpsize() + { + $img = $this->manager()->make('tests/images/trim.png'); + $img->fit(300, 150, function ($constraint) {$constraint->upsize();}); + $this->assertInternalType('int', $img->getWidth()); + $this->assertInternalType('int', $img->getHeight()); + $this->assertEquals(50, $img->getWidth()); + $this->assertEquals(25, $img->getHeight()); + $this->assertColorAtPosition('#00aef0', $img, 0, 0); + $this->assertColorAtPosition('#afa94c', $img, 17, 0); + $this->assertColorAtPosition('#ffa601', $img, 24, 0); + } + public function testFlipImageHorizontal() { $img = $this->manager()->make('tests/images/tile.png');