mirror of
https://github.com/Intervention/image.git
synced 2025-08-07 14:26:31 +02:00
added optional position argument to fit command
This commit is contained in:
@@ -18,9 +18,10 @@ class FitCommand extends ResizeCommand
|
|||||||
$width = $this->argument(0)->type('digit')->required()->value();
|
$width = $this->argument(0)->type('digit')->required()->value();
|
||||||
$height = $this->argument(1)->type('digit')->value($width);
|
$height = $this->argument(1)->type('digit')->value($width);
|
||||||
$constraints = $this->argument(2)->type('closure')->value();
|
$constraints = $this->argument(2)->type('closure')->value();
|
||||||
|
$position = $this->argument(3)->type('string')->value('center');
|
||||||
|
|
||||||
// calculate size
|
// calculate size
|
||||||
$cropped = $image->getSize()->fit(new Size($width, $height));
|
$cropped = $image->getSize()->fit(new Size($width, $height), $position);
|
||||||
$resized = clone $cropped;
|
$resized = clone $cropped;
|
||||||
$resized = $resized->resize($width, $height, $constraints);
|
$resized = $resized->resize($width, $height, $constraints);
|
||||||
|
|
||||||
|
@@ -17,9 +17,10 @@ class FitCommand extends \Intervention\Image\Commands\AbstractCommand
|
|||||||
$width = $this->argument(0)->type('digit')->required()->value();
|
$width = $this->argument(0)->type('digit')->required()->value();
|
||||||
$height = $this->argument(1)->type('digit')->value($width);
|
$height = $this->argument(1)->type('digit')->value($width);
|
||||||
$constraints = $this->argument(2)->type('closure')->value();
|
$constraints = $this->argument(2)->type('closure')->value();
|
||||||
|
$position = $this->argument(3)->type('string')->value('center');
|
||||||
|
|
||||||
// calculate size
|
// calculate size
|
||||||
$cropped = $image->getSize()->fit(new Size($width, $height));
|
$cropped = $image->getSize()->fit(new Size($width, $height), $position);
|
||||||
$resized = clone $cropped;
|
$resized = clone $cropped;
|
||||||
$resized = $resized->resize($width, $height, $constraints);
|
$resized = $resized->resize($width, $height, $constraints);
|
||||||
|
|
||||||
|
@@ -222,7 +222,7 @@ class Size
|
|||||||
* @param Size $size
|
* @param Size $size
|
||||||
* @return \Intervention\Image\Size
|
* @return \Intervention\Image\Size
|
||||||
*/
|
*/
|
||||||
public function fit(Size $size)
|
public function fit(Size $size, $position = 'center')
|
||||||
{
|
{
|
||||||
// create size with auto height
|
// create size with auto height
|
||||||
$auto_height = clone $size;
|
$auto_height = clone $size;
|
||||||
@@ -248,8 +248,8 @@ class Size
|
|||||||
$size = $auto_width;
|
$size = $auto_width;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->align('center');
|
$this->align($position);
|
||||||
$size->align('center');
|
$size->align($position);
|
||||||
$size->setPivot($this->relativePosition($size));
|
$size->setPivot($this->relativePosition($size));
|
||||||
|
|
||||||
return $size;
|
return $size;
|
||||||
|
@@ -10,7 +10,7 @@ class FitCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
Mockery::close();
|
Mockery::close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGd()
|
public function testGdFit()
|
||||||
{
|
{
|
||||||
$cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400));
|
$cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400));
|
||||||
$cropped_size->shouldReceive('getWidth')->times(2)->andReturn(800);
|
$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->shouldReceive('resize')->with(200, 100, null)->once()->andReturn($cropped_size);
|
||||||
$cropped_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100));
|
$cropped_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100));
|
||||||
$original_size = Mockery::mock('\Intervention\Image\Size', array(800, 600));
|
$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');
|
$resource = imagecreatefromjpeg(__DIR__.'/images/test.jpg');
|
||||||
$image = Mockery::mock('Intervention\Image\Image');
|
$image = Mockery::mock('Intervention\Image\Image');
|
||||||
$image->shouldReceive('getSize')->once()->andReturn($original_size);
|
$image->shouldReceive('getSize')->once()->andReturn($original_size);
|
||||||
@@ -29,7 +29,26 @@ class FitCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue($result);
|
$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 = Mockery::mock('\Intervention\Image\Size', array(800, 400));
|
||||||
$cropped_size->shouldReceive('getWidth')->once()->andReturn(200);
|
$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->shouldReceive('resize')->with(200, 100, null)->once()->andReturn($cropped_size);
|
||||||
$cropped_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100));
|
$cropped_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100));
|
||||||
$original_size = Mockery::mock('\Intervention\Image\Size', array(800, 600));
|
$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 = Mockery::mock('Imagick');
|
||||||
$imagick->shouldReceive('cropimage')->with(800, 400, 0, 100)->andReturn(true);
|
$imagick->shouldReceive('cropimage')->with(800, 400, 0, 100)->andReturn(true);
|
||||||
$imagick->shouldReceive('resizeimage')->with(200, 100, \Imagick::FILTER_BOX, 1)->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);
|
$result = $command->execute($image);
|
||||||
$this->assertTrue($result);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -356,6 +356,43 @@ class SizeTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(100, $fitted->pivot->y);
|
$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()
|
public function testFitsInto()
|
||||||
{
|
{
|
||||||
$box = new Size(800, 600);
|
$box = new Size(800, 600);
|
||||||
|
Reference in New Issue
Block a user