-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Render crops correctly slowly getting somewhere
- Loading branch information
Showing
6 changed files
with
318 additions
and
14 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
Neos.Media/Classes/Domain/Model/Adjustment/FocalPointCropSpecification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neos\Media\Domain\Model\Adjustment; | ||
|
||
use Imagine\Image\Box; | ||
use Imagine\Image\BoxInterface; | ||
use Imagine\Image\Point; | ||
use Imagine\Image\PointInterface; | ||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Annotations\Aspect; | ||
use Neos\Media\Domain\ValueObject\Configuration\AspectRatio; | ||
|
||
#[Flow\Proxy(false)] | ||
final readonly class FocalPointCropSpecification | ||
{ | ||
private function __construct( | ||
public PointInterface $cropOffset, | ||
public BoxInterface $cropDimensions, | ||
public PointInterface $croppedFocalPoint | ||
) { | ||
} | ||
|
||
public static function createFromOriginalDimensionsRequestedDimensionsAndFocalPoint( | ||
BoxInterface $originalDimensions, | ||
PointInterface $originalFocalPoint, | ||
BoxInterface $requestedDimensions, | ||
): self { | ||
$originalAspect = new AspectRatio($originalDimensions->getWidth(), $originalDimensions->getHeight()); | ||
$finalAspect = new AspectRatio($requestedDimensions->getWidth(), $requestedDimensions->getHeight()); | ||
|
||
if ($originalAspect->getRatio() >= $finalAspect->getRatio()) { | ||
// leading dimension = height, width is cropped | ||
$factor = $requestedDimensions->getHeight() / $originalDimensions->getHeight(); | ||
$cropBox = new Box((int)$requestedDimensions->getWidth() / $factor, $requestedDimensions->getHeight() / $factor); | ||
$cropX = $originalFocalPoint->getX() - (int)($cropBox->getWidth() / 2); | ||
$cropXMax = $originalDimensions->getWidth() - $cropBox->getWidth(); | ||
if ($cropX < 0) { | ||
$cropX = 0; | ||
} elseif ($cropX > $cropXMax) { | ||
$cropX = $cropXMax; | ||
} | ||
$cropOffset = new Point($cropX, 0); | ||
} else { | ||
// leading dimension = width, height is cropped | ||
$factor = $requestedDimensions->getWidth() / $originalDimensions->getWidth(); | ||
$cropBox = new Box((int)$requestedDimensions->getWidth() / $factor, $requestedDimensions->getHeight() / $factor); | ||
$cropY = $originalFocalPoint->getY() - (int)($cropBox->getHeight() / 2); | ||
$cropYMax = $originalDimensions->getHeight() - $cropBox->getHeight(); | ||
if ($cropY < 0) { | ||
$cropY = 0; | ||
} elseif ($cropY > $cropYMax) { | ||
$cropY = $cropYMax; | ||
} | ||
$cropOffset = new Point(0, $cropY); | ||
} | ||
|
||
return new self( | ||
$cropOffset, | ||
$cropBox, | ||
new Point( | ||
(int)round(($originalFocalPoint->getX() - $cropOffset->getX()) * $factor), | ||
(int)round(($originalFocalPoint->getY() - $cropOffset->getY()) * $factor) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.