1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 20:28:21 +01:00

Resizer refactoring

This commit is contained in:
Oliver Vogel 2021-11-10 13:50:23 +00:00
parent 84e946a588
commit 4520580511
2 changed files with 144 additions and 146 deletions

View File

@ -62,9 +62,8 @@ class Resizer
*
* @param SizeInterface $size
*/
public function __construct(SizeInterface $original)
public function __construct()
{
$this->original = $original;
$this->target = new Size(0, 0);
}
@ -128,32 +127,32 @@ class Resizer
return $this;
}
protected function getProportionalWidth(): int
public function toSize(SizeInterface $size): self
{
return $this->setTargetSize($size);
}
protected function getProportionalWidth(SizeInterface $size): int
{
if (! $this->hasTargetHeight()) {
return $this->original->getWidth();
return $size->getWidth();
}
return (int) round($this->target->getHeight() * $this->original->getAspectRatio());
return (int) round($this->target->getHeight() * $size->getAspectRatio());
}
protected function getProportionalHeight(): int
protected function getProportionalHeight(SizeInterface $size): int
{
if (! $this->hasTargetWidth()) {
return $this->original->getHeight();
return $size->getHeight();
}
return (int) round($this->target->getWidth() / $this->original->getAspectRatio());
return (int) round($this->target->getWidth() / $size->getAspectRatio());
}
protected function copyOriginal(): SizeInterface
public function resize(SizeInterface $size): SizeInterface
{
return new Size($this->original->getWidth(), $this->original->getHeight());
}
public function resize(): SizeInterface
{
$resized = $this->copyOriginal();
$resized = clone $size;
if ($this->hasTargetWidth()) {
$resized->setWidth($this->target->getWidth());
@ -166,81 +165,81 @@ class Resizer
return $resized;
}
public function resizeDown(): SizeInterface
public function resizeDown(SizeInterface $size): SizeInterface
{
$resized = $this->copyOriginal();
$resized = clone $size;
if ($this->hasTargetWidth()) {
$resized->setWidth(
min($this->target->getWidth(), $this->original->getWidth())
min($this->target->getWidth(), $size->getWidth())
);
}
if ($this->hasTargetHeight()) {
$resized->setHeight(
min($this->target->getHeight(), $this->original->getHeight())
min($this->target->getHeight(), $size->getHeight())
);
}
return $resized;
}
public function scale(): SizeInterface
public function scale(SizeInterface $size): SizeInterface
{
$resized = $this->copyOriginal();
$resized = clone $size;
if ($this->hasTargetWidth() && $this->hasTargetHeight()) {
$resized->setWidth(min(
$this->getProportionalWidth(),
$this->getProportionalWidth($size),
$this->target->getWidth()
));
$resized->setHeight(min(
$this->getProportionalHeight(),
$this->getProportionalHeight($size),
$this->target->getHeight()
));
} elseif ($this->hasTargetWidth()) {
$resized->setWidth($this->target->getWidth());
$resized->setHeight($this->getProportionalHeight());
$resized->setHeight($this->getProportionalHeight($size));
} elseif ($this->hasTargetHeight()) {
$resized->setWidth($this->getProportionalWidth());
$resized->setWidth($this->getProportionalWidth($size));
$resized->setHeight($this->target->getHeight());
}
return $resized;
}
public function scaleDown(): SizeInterface
public function scaleDown(SizeInterface $size): SizeInterface
{
$resized = $this->copyOriginal();
$resized = clone $size;
if ($this->hasTargetWidth() && $this->hasTargetHeight()) {
$resized->setWidth(min(
$this->getProportionalWidth(),
$this->getProportionalWidth($size),
$this->target->getWidth(),
$this->original->getWidth()
$size->getWidth()
));
$resized->setHeight(min(
$this->getProportionalHeight(),
$this->getProportionalHeight($size),
$this->target->getHeight(),
$this->original->getHeight()
$size->getHeight()
));
} elseif ($this->hasTargetWidth()) {
$resized->setWidth(min(
$this->target->getWidth(),
$this->original->getWidth()
$size->getWidth()
));
$resized->setHeight(min(
$this->getProportionalHeight(),
$this->original->getHeight()
$this->getProportionalHeight($size),
$size->getHeight()
));
} elseif ($this->hasTargetHeight()) {
$resized->setWidth(min(
$this->getProportionalWidth(),
$this->original->getWidth()
$this->getProportionalWidth($size),
$size->getWidth()
));
$resized->setHeight(min(
$this->target->getHeight(),
$this->original->getHeight()
$size->getHeight()
));
}

View File

@ -8,76 +8,75 @@ use PHPUnit\Framework\TestCase;
class ResizerTest extends TestCase
{
public function testConstructor(): void
{
$size = new Size(300, 200);
$resizer = new Resizer($size);
$this->assertInstanceOf(Resizer::class, $resizer);
}
public function testSetTargetSizeByArray()
{
$resizer = new Resizer(new Size(300, 200));
$result = $resizer->setTargetSizeByArray([800, 600]);
$this->assertInstanceOf(Resizer::class, $result);
$this->assertEquals(800, $result->resize()->getWidth());
$this->assertEquals(600, $result->resize()->getHeight());
$size = new Size(300, 200);
$resizer = new Resizer();
$resizer = $resizer->setTargetSizeByArray([800, 600]);
$this->assertInstanceOf(Resizer::class, $resizer);
$this->assertEquals(800, $resizer->resize($size)->getWidth());
$this->assertEquals(600, $resizer->resize($size)->getHeight());
$resizer = new Resizer(new Size(300, 200));
$result = $resizer->setTargetSizeByArray([800]);
$this->assertInstanceOf(Resizer::class, $result);
$this->assertEquals(800, $result->resize()->getWidth());
$this->assertEquals(200, $result->resize()->getHeight());
$size = new Size(300, 200);
$resizer = new Resizer();
$resizer = $resizer->setTargetSizeByArray([800]);
$this->assertInstanceOf(Resizer::class, $resizer);
$this->assertEquals(800, $resizer->resize($size)->getWidth());
$this->assertEquals(200, $resizer->resize($size)->getHeight());
$resizer = new Resizer(new Size(300, 200));
$result = $resizer->setTargetSizeByArray([function ($size) {
$size = new Size(300, 200);
$resizer = new Resizer();
$resizer = $resizer->setTargetSizeByArray([function ($size) {
$size->width(80);
$size->height(40);
}]);
$this->assertInstanceOf(Resizer::class, $result);
$this->assertEquals(80, $result->resize()->getWidth());
$this->assertEquals(40, $result->resize()->getHeight());
$this->assertInstanceOf(Resizer::class, $resizer);
$this->assertEquals(80, $resizer->resize($size)->getWidth());
$this->assertEquals(40, $resizer->resize($size)->getHeight());
$resizer = new Resizer(new Size(300, 200));
$result = $resizer->setTargetSizeByArray([function ($size) {
$size = new Size(300, 200);
$resizer = new Resizer();
$resizer = $resizer->setTargetSizeByArray([function ($size) {
$size->width(80);
}]);
$this->assertInstanceOf(Resizer::class, $result);
$this->assertEquals(80, $result->resize()->getWidth());
$this->assertEquals(200, $result->resize()->getHeight());
$this->assertInstanceOf(Resizer::class, $resizer);
$this->assertEquals(80, $resizer->resize($size)->getWidth());
$this->assertEquals(200, $resizer->resize($size)->getHeight());
$resizer = new Resizer(new Size(300, 200));
$result = $resizer->setTargetSizeByArray([function ($size) {
$size = new Size(300, 200);
$resizer = new Resizer();
$resizer = $resizer->setTargetSizeByArray([function ($size) {
$size->height(10);
}]);
$this->assertInstanceOf(Resizer::class, $result);
$this->assertEquals(300, $result->resize()->getWidth());
$this->assertEquals(10, $result->resize()->getHeight());
$this->assertInstanceOf(Resizer::class, $resizer);
$this->assertEquals(300, $resizer->resize($size)->getWidth());
$this->assertEquals(10, $resizer->resize($size)->getHeight());
}
public function testSetTargetSize(): void
{
$resizer = new Resizer(new Size(300, 200));
$result = $resizer->setTargetSize(new Size(200, 100));
$this->assertInstanceOf(Resizer::class, $result);
$this->assertEquals(200, $result->resize()->getWidth());
$this->assertEquals(100, $result->resize()->getHeight());
$size = new Size(300, 200);
$resizer = new Resizer();
$resizer = $resizer->setTargetSize(new Size(200, 100));
$this->assertInstanceOf(Resizer::class, $resizer);
$this->assertEquals(200, $resizer->resize($size)->getWidth());
$this->assertEquals(100, $resizer->resize($size)->getHeight());
}
public function testResize()
{
$size = new Size(300, 200);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(150);
$result = $resizer->resize();
$result = $resizer->resize($size);
$this->assertEquals(150, $result->getWidth());
$this->assertEquals(200, $result->getHeight());
$size = new Size(300, 200);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(20);
$resizer->height(10);
$result = $resizer->resize();
$result = $resizer->resize($size);
$this->assertEquals(20, $result->getWidth());
$this->assertEquals(10, $result->getHeight());
}
@ -86,53 +85,53 @@ class ResizerTest extends TestCase
{
// 800x600 > 1000x2000 = 800x600
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$resizer->height(2000);
$result = $resizer->resizeDown();
$result = $resizer->resizeDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
// 800x600 > 400x1000 = 400x600
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(400);
$resizer->height(1000);
$result = $resizer->resizeDown();
$result = $resizer->resizeDown($size);
$this->assertEquals(400, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
// 800x600 > 1000x400 = 800x400
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$resizer->height(400);
$result = $resizer->resizeDown();
$result = $resizer->resizeDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(400, $result->getHeight());
// 800x600 > 400x300 = 400x300
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(400);
$resizer->height(300);
$result = $resizer->resizeDown();
$result = $resizer->resizeDown($size);
$this->assertEquals(400, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
// 800x600 > 1000xnull = 800x600
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$result = $resizer->resizeDown();
$result = $resizer->resizeDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
// 800x600 > nullx1000 = 800x600
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->height(1000);
$result = $resizer->resizeDown();
$result = $resizer->resizeDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
}
@ -141,136 +140,136 @@ class ResizerTest extends TestCase
{
// 800x600 > 1000x2000 = 1000x750
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$resizer->height(2000);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(1000, $result->getWidth());
$this->assertEquals(750, $result->getHeight());
// 800x600 > 2000x1000 = 1333x1000
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(2000);
$resizer->height(1000);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(1333, $result->getWidth());
$this->assertEquals(1000, $result->getHeight());
// // 800x600 > nullx3000 = 4000x3000
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->height(3000);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(4000, $result->getWidth());
$this->assertEquals(3000, $result->getHeight());
// // 800x600 > 8000xnull = 8000x6000
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(8000);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(8000, $result->getWidth());
$this->assertEquals(6000, $result->getHeight());
// // 800x600 > 100x400 = 100x75
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(100);
$resizer->height(400);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(100, $result->getWidth());
$this->assertEquals(75, $result->getHeight());
// // 800x600 > 400x100 = 133x100
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(400);
$resizer->height(100);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(133, $result->getWidth());
$this->assertEquals(100, $result->getHeight());
// // 800x600 > nullx300 = 400x300
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->height(300);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(400, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
// // 800x600 > 80xnull = 80x60
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(80);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(80, $result->getWidth());
$this->assertEquals(60, $result->getHeight());
// // 640x480 > 225xnull = 225x169
$size = new Size(640, 480);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(225);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(225, $result->getWidth());
$this->assertEquals(169, $result->getHeight());
// // 640x480 > 223xnull = 223x167
$size = new Size(640, 480);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(223);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(223, $result->getWidth());
$this->assertEquals(167, $result->getHeight());
// // 600x800 > 300x300 = 225x300
$size = new Size(600, 800);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(300);
$resizer->height(300);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(225, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
// // 800x600 > 400x10 = 13x10
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(400);
$resizer->height(10);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(13, $result->getWidth());
$this->assertEquals(10, $result->getHeight());
// // 800x600 > 1000x1200 = 1000x750
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$resizer->height(1200);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(1000, $result->getWidth());
$this->assertEquals(750, $result->getHeight());
$size = new Size(12000, 12);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(4000);
$resizer->height(3000);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(4000, $result->getWidth());
$this->assertEquals(4, $result->getHeight());
$size = new Size(12, 12000);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(4000);
$resizer->height(3000);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(3, $result->getWidth());
$this->assertEquals(3000, $result->getHeight());
$size = new Size(12000, 6000);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(4000);
$resizer->height(3000);
$result = $resizer->scale();
$result = $resizer->scale($size);
$this->assertEquals(4000, $result->getWidth());
$this->assertEquals(2000, $result->getHeight());
}
@ -278,93 +277,93 @@ class ResizerTest extends TestCase
public function testScaleDown()
{
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$resizer->height(2000);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$resizer->height(600);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$resizer->height(300);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(400, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(400);
$resizer->height(1000);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(400, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(400);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(400, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->height(300);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(400, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(1000);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->height(1000);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(800, $result->getWidth());
$this->assertEquals(600, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(100);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(100, $result->getWidth());
$this->assertEquals(75, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(300);
$resizer->height(200);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(267, $result->getWidth());
$this->assertEquals(200, $result->getHeight());
$size = new Size(600, 800);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(300);
$resizer->height(300);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(225, $result->getWidth());
$this->assertEquals(300, $result->getHeight());
$size = new Size(800, 600);
$resizer = new Resizer($size);
$resizer = new Resizer();
$resizer->width(400);
$resizer->height(10);
$result = $resizer->scaleDown();
$result = $resizer->scaleDown($size);
$this->assertEquals(13, $result->getWidth());
$this->assertEquals(10, $result->getHeight());
}