1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 12:11:26 +02:00

Added Size::align()

This commit is contained in:
Oliver Vogel
2021-11-03 18:48:10 +00:00
parent def7a406af
commit e2b9c17ba3
2 changed files with 132 additions and 0 deletions

View File

@@ -89,4 +89,91 @@ class Size implements SizeInterface
{ {
return $this->getWidth() < $this->getHeight(); return $this->getWidth() < $this->getHeight();
} }
/**
* Aligns current size's pivot point to given position
* and moves point automatically by offset.
*
* @param string $position
* @param int $offset_x
* @param int $offset_y
* @return Size
*/
public function align(string $position, int $offset_x = 0, int $offset_y = 0): self
{
switch (strtolower($position)) {
case 'top':
case 'top-center':
case 'top-middle':
case 'center-top':
case 'middle-top':
$x = intval($this->width / 2);
$y = 0 + $offset_y;
break;
case 'top-right':
case 'right-top':
$x = $this->width - $offset_x;
$y = 0 + $offset_y;
break;
case 'left':
case 'left-center':
case 'left-middle':
case 'center-left':
case 'middle-left':
$x = 0 + $offset_x;
$y = intval($this->height / 2);
break;
case 'right':
case 'right-center':
case 'right-middle':
case 'center-right':
case 'middle-right':
$x = $this->width - $offset_x;
$y = intval($this->height / 2);
break;
case 'bottom-left':
case 'left-bottom':
$x = 0 + $offset_x;
$y = $this->height - $offset_y;
break;
case 'bottom':
case 'bottom-center':
case 'bottom-middle':
case 'center-bottom':
case 'middle-bottom':
$x = intval($this->width / 2);
$y = $this->height - $offset_y;
break;
case 'bottom-right':
case 'right-bottom':
$x = $this->width - $offset_x;
$y = $this->height - $offset_y;
break;
case 'center':
case 'middle':
case 'center-center':
case 'middle-middle':
$x = intval($this->width / 2) + $offset_x;
$y = intval($this->height / 2) + $offset_y;
break;
default:
case 'top-left':
case 'left-top':
$x = 0 + $offset_x;
$y = 0 + $offset_y;
break;
}
$this->pivot->setPosition($x, $y);
return $this;
}
} }

View File

@@ -98,4 +98,49 @@ class SizeTest extends TestCase
$box = new Size(200, 300); $box = new Size(200, 300);
$this->assertTrue($box->isPortrait()); $this->assertTrue($box->isPortrait());
} }
public function testAlign(): void
{
$box = new Size(640, 480);
$this->assertEquals(0, $box->getPivot()->getX());
$this->assertEquals(0, $box->getPivot()->getY());
$box->align('top-left', 3, 3);
$this->assertEquals(3, $box->getPivot()->getX());
$this->assertEquals(3, $box->getPivot()->getY());
$box->align('top', 3, 3);
$this->assertEquals(320, $box->getPivot()->getX());
$this->assertEquals(3, $box->getPivot()->getY());
$box->align('top-right', 3, 3);
$this->assertEquals(637, $box->getPivot()->getX());
$this->assertEquals(3, $box->getPivot()->getY());
$box->align('left', 3, 3);
$this->assertEquals(3, $box->getPivot()->getX());
$this->assertEquals(240, $box->getPivot()->getY());
$box->align('center', 3, 3);
$this->assertEquals(323, $box->getPivot()->getX());
$this->assertEquals(243, $box->getPivot()->getY());
$box->align('right', 3, 3);
$this->assertEquals(637, $box->getPivot()->getX());
$this->assertEquals(240, $box->getPivot()->getY());
$box->align('bottom-left', 3, 3);
$this->assertEquals(3, $box->getPivot()->getX());
$this->assertEquals(477, $box->getPivot()->getY());
$box->align('bottom', 3, 3);
$this->assertEquals(320, $box->getPivot()->getX());
$this->assertEquals(477, $box->getPivot()->getY());
$result = $box->align('bottom-right', 3, 3);
$this->assertEquals(637, $box->getPivot()->getX());
$this->assertEquals(477, $box->getPivot()->getY());
$this->assertInstanceOf(Size::class, $result);
}
} }