Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/FieldType/DBFocusPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DBFocusPoint extends DBComposite
*/
public function getX(): float
{
return (float) $this->getField('X');
return min(1.0, max(-1.0, (float) $this->getField('X')));
}

/**
Expand All @@ -62,7 +62,7 @@ public function setX(float $value): self
*/
public function getY(): float
{
return (float)$this->getField('Y');
return min(1.0, max(-1.0, (float) $this->getField('Y')));
}

/**
Expand Down Expand Up @@ -281,6 +281,13 @@ public function applyCrop(Image_Backend $backend, array $cropData): ?Image_Backe
$cropAxis = $cropData['CropAxis'];
$cropOffset = $cropData['CropOffset'];

// Cached size is stale, so the crop box may lie outside the image: center instead
if ($backend->getWidth() !== $cropData['x']['OriginalLength']
|| $backend->getHeight() !== $cropData['y']['OriginalLength']
) {
return $backend->croppedResize($width, $height);
}

// Resize based on axis
switch ($cropAxis) {
case 'x':
Expand Down
10 changes: 10 additions & 0 deletions tests/FocusPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public function testOffsetToCoord(): void
$this->assertEquals(1.0, DBFocusPoint::focusOffsetToCoord(1));
}

public function testCoordinatesAreClampedToImage(): void
{
// Bypass the clamping setters, as when values are hydrated from the database
$this->focusPoint->setField('X', 5);
$this->focusPoint->setField('Y', -7);

$this->assertSame(1.0, $this->focusPoint->getX());
$this->assertSame(-1.0, $this->focusPoint->getY());
}

public function testCalculateCrop(): void
{
$this->markTestIncomplete('Implement crop calculation tests');
Expand Down
19 changes: 19 additions & 0 deletions tests/ImageManipulationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ public function testFocusFillMax(): void
}
}

public function testStaleCachedDimensionsFallBackToCenteredCrop(): void
{
/** @var Image|FocusPointImageExtension $img */
$img = $this->objFromFixture(Image::class, 'pngLeftTop');
$this->assertSame(100, $img->getWidth());

// Cached size does not match the 100x100 file, so the crop box would exceed the image
$img->FocusPoint->Width = 200;
$img->FocusPoint->Height = 100;

$cropped = $img->FocusFill(50, 50);
$this->assertNotNull($cropped);
$this->assertTrue($cropped->isSize(50, 50));

$cropped = $img->FocusFill(50, 25);
$this->assertNotNull($cropped);
$this->assertTrue($cropped->isSize(50, 25));
}

public function testPercentages(): void
{
/** @var Image|FocusPointImageExtension $pngLeftTop */
Expand Down
Loading