diff --git a/src/Intervention/Image/Gd/Commands/FitCommand.php b/src/Intervention/Image/Gd/Commands/FitCommand.php index 06e61a11..492b7218 100644 --- a/src/Intervention/Image/Gd/Commands/FitCommand.php +++ b/src/Intervention/Image/Gd/Commands/FitCommand.php @@ -18,9 +18,10 @@ 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(); + $position = $this->argument(3)->type('string')->value('center'); // calculate size - $cropped = $image->getSize()->fit(new Size($width, $height)); + $cropped = $image->getSize()->fit(new Size($width, $height), $position); $resized = clone $cropped; $resized = $resized->resize($width, $height, $constraints); diff --git a/src/Intervention/Image/Imagick/Commands/FitCommand.php b/src/Intervention/Image/Imagick/Commands/FitCommand.php index dad7bf4b..7e316299 100644 --- a/src/Intervention/Image/Imagick/Commands/FitCommand.php +++ b/src/Intervention/Image/Imagick/Commands/FitCommand.php @@ -17,9 +17,10 @@ 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(); + $position = $this->argument(3)->type('string')->value('center'); // calculate size - $cropped = $image->getSize()->fit(new Size($width, $height)); + $cropped = $image->getSize()->fit(new Size($width, $height), $position); $resized = clone $cropped; $resized = $resized->resize($width, $height, $constraints); diff --git a/src/Intervention/Image/Size.php b/src/Intervention/Image/Size.php index da81e724..bfea4d41 100644 --- a/src/Intervention/Image/Size.php +++ b/src/Intervention/Image/Size.php @@ -222,7 +222,7 @@ class Size * @param Size $size * @return \Intervention\Image\Size */ - public function fit(Size $size) + public function fit(Size $size, $position = 'center') { // create size with auto height $auto_height = clone $size; @@ -248,8 +248,8 @@ class Size $size = $auto_width; } - $this->align('center'); - $size->align('center'); + $this->align($position); + $size->align($position); $size->setPivot($this->relativePosition($size)); return $size; diff --git a/tests/FitCommandTest.php b/tests/FitCommandTest.php index af134020..0d7cb6e6 100644 --- a/tests/FitCommandTest.php +++ b/tests/FitCommandTest.php @@ -10,7 +10,7 @@ class FitCommandTest extends PHPUnit_Framework_TestCase Mockery::close(); } - public function testGd() + public function testGdFit() { $cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400)); $cropped_size->shouldReceive('getWidth')->times(2)->andReturn(800); @@ -18,7 +18,7 @@ class FitCommandTest extends PHPUnit_Framework_TestCase $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($cropped_size); + $original_size->shouldReceive('fit')->with(Mockery::any(), 'center')->once()->andReturn($cropped_size); $resource = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); $image = Mockery::mock('Intervention\Image\Image'); $image->shouldReceive('getSize')->once()->andReturn($original_size); @@ -29,7 +29,26 @@ class FitCommandTest extends PHPUnit_Framework_TestCase $this->assertTrue($result); } - public function testImagick() + public function testGdFitWithPosition() + { + $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')->with(Mockery::any(), 'top-left')->once()->andReturn($cropped_size); + $resource = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); + $image = Mockery::mock('Intervention\Image\Image'); + $image->shouldReceive('getSize')->once()->andReturn($original_size); + $image->shouldReceive('getCore')->once()->andReturn($resource); + $image->shouldReceive('setCore')->once(); + $command = new FitGd(array(200, 100, null, 'top-left')); + $result = $command->execute($image); + $this->assertTrue($result); + } + + public function testImagickFit() { $cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400)); $cropped_size->shouldReceive('getWidth')->once()->andReturn(200); @@ -37,7 +56,7 @@ class FitCommandTest extends PHPUnit_Framework_TestCase $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($cropped_size); + $original_size->shouldReceive('fit')->with(Mockery::any(), 'center')->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); @@ -49,4 +68,25 @@ class FitCommandTest extends PHPUnit_Framework_TestCase $result = $command->execute($image); $this->assertTrue($result); } + + public function testImagickFitWithPosition() + { + $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')->with(Mockery::any(), 'top-left')->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); + $imagick->shouldReceive('setimagepage')->with(0, 0, 0, 0)->andReturn(true); + $image = Mockery::mock('Intervention\Image\Image'); + $image->shouldReceive('getSize')->once()->andReturn($original_size); + $image->shouldReceive('getCore')->times(3)->andReturn($imagick); + $command = new FitImagick(array(200, 100, null, 'top-left')); + $result = $command->execute($image); + $this->assertTrue($result); + } } diff --git a/tests/SizeTest.php b/tests/SizeTest.php index 48b5ecdf..4be87ee0 100644 --- a/tests/SizeTest.php +++ b/tests/SizeTest.php @@ -356,6 +356,43 @@ class SizeTest extends PHPUnit_Framework_TestCase $this->assertEquals(100, $fitted->pivot->y); } + /** + * @dataProvider providerFitWithPosition + */ + public function testFitWithPosition(Size $box, $position, $x, $y) + { + $fitted = $box->fit(new Size(100, 100), $position); + $this->assertEquals(600, $fitted->width); + $this->assertEquals(600, $fitted->height); + $this->assertEquals($x, $fitted->pivot->x); + $this->assertEquals($y, $fitted->pivot->y); + } + + public function providerFitWithPosition() + { + return array( + array(new Size(800, 600), 'top-left', 0, 0), + array(new Size(800, 600), 'top', 100, 0), + array(new Size(800, 600), 'top-right', 200, 0), + array(new Size(800, 600), 'left', 0, 0), + array(new Size(800, 600), 'center', 100, 0), + array(new Size(800, 600), 'right', 200, 0), + array(new Size(800, 600), 'bottom-left', 0, 0), + array(new Size(800, 600), 'bottom', 100, 0), + array(new Size(800, 600), 'bottom-right', 200, 0), + + array(new Size(600, 800), 'top-left', 0, 0), + array(new Size(600, 800), 'top', 0, 0), + array(new Size(600, 800), 'top-right', 0, 0), + array(new Size(600, 800), 'left', 0, 100), + array(new Size(600, 800), 'center', 0, 100), + array(new Size(600, 800), 'right', 0, 100), + array(new Size(600, 800), 'bottom-left', 0, 200), + array(new Size(600, 800), 'bottom', 0, 200), + array(new Size(600, 800), 'bottom-right', 0, 200), + ); + } + public function testFitsInto() { $box = new Size(800, 600);