1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-29 16:50:07 +02:00

added optional constraints for fit()

This commit is contained in:
Oliver Vogel
2014-07-26 11:27:23 +02:00
parent 0a3e460184
commit 2d69ff5a5d
5 changed files with 52 additions and 16 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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);

View File

@@ -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');

View File

@@ -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');