mirror of
https://github.com/Intervention/image.git
synced 2025-08-30 09:10:21 +02:00
added optional constraints for fit()
This commit is contained in:
@@ -17,12 +17,15 @@ 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();
|
||||||
|
|
||||||
// calculate size
|
// 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
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -16,20 +16,23 @@ 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();
|
||||||
|
|
||||||
// calculate size
|
// 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
|
// crop image
|
||||||
$image->getCore()->cropImage(
|
$image->getCore()->cropImage(
|
||||||
$fitted->width,
|
$cropped->width,
|
||||||
$fitted->height,
|
$cropped->height,
|
||||||
$fitted->pivot->x,
|
$cropped->pivot->x,
|
||||||
$fitted->pivot->y
|
$cropped->pivot->y
|
||||||
);
|
);
|
||||||
|
|
||||||
// resize image
|
// 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);
|
$image->getCore()->setImagePage(0,0,0,0);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@@ -12,12 +12,13 @@ class FitCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testGd()
|
public function testGd()
|
||||||
{
|
{
|
||||||
$fitted_size = Mockery::mock('\Intervention\Image\Size', array(800, 400));
|
$cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400));
|
||||||
$fitted_size->shouldReceive('getWidth')->once()->andReturn(800);
|
$cropped_size->shouldReceive('getWidth')->times(2)->andReturn(800);
|
||||||
$fitted_size->shouldReceive('getHeight')->once()->andReturn(400);
|
$cropped_size->shouldReceive('getHeight')->times(2)->andReturn(400);
|
||||||
$fitted_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 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 = 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');
|
$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);
|
||||||
@@ -30,10 +31,13 @@ class FitCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testImagick()
|
public function testImagick()
|
||||||
{
|
{
|
||||||
$fitted_size = Mockery::mock('\Intervention\Image\Size', array(800, 400));
|
$cropped_size = Mockery::mock('\Intervention\Image\Size', array(800, 400));
|
||||||
$fitted_size->pivot = Mockery::mock('\Intervention\Image\Point', array(0, 100));
|
$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 = 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 = 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);
|
||||||
|
@@ -496,6 +496,19 @@ class GdSystemTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertTransparentPosition($img, 6, 2);
|
$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()
|
public function testFlipImageHorizontal()
|
||||||
{
|
{
|
||||||
$img = $this->manager()->make('tests/images/tile.png');
|
$img = $this->manager()->make('tests/images/tile.png');
|
||||||
|
@@ -496,6 +496,19 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertTransparentPosition($img, 6, 2);
|
$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()
|
public function testFlipImageHorizontal()
|
||||||
{
|
{
|
||||||
$img = $this->manager()->make('tests/images/tile.png');
|
$img = $this->manager()->make('tests/images/tile.png');
|
||||||
|
Reference in New Issue
Block a user