Skip to content

Commit

Permalink
WIP: Basic infrastructure for focal points in thumbnails and image va…
Browse files Browse the repository at this point in the history
…riants

No actual calculation yet
  • Loading branch information
mficzel committed Jun 5, 2024
1 parent 068508d commit e63bfeb
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Neos.Media/Classes/Domain/Model/FocalPointTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace Neos\Media\Domain\Model;

/*
* This file is part of the Neos.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Doctrine\ORM\Mapping as ORM;

/**
* Trait for methods regarding the focal point of an asset
*/
trait FocalPointTrait
{
/**
* @var integer
* @ORM\Column(nullable=true)
*/
protected $focalPointX = null;

/**
* @var integer
* @ORM\Column(nullable=true)
*/
protected $focalPointY = null;

public function getFocalPointX(): ?int
{
return $this->focalPointX;
}

public function setFocalPointX(?int $focalPointX): void
{
$this->focalPointX = $focalPointX;
}

public function getFocalPointY(): ?int
{
return $this->focalPointY;
}

public function setFocalPointY(?int $focalPointY): void
{
$this->focalPointY = $focalPointY;
}
}
1 change: 1 addition & 0 deletions Neos.Media/Classes/Domain/Model/ImageVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
class ImageVariant extends Asset implements AssetVariantInterface, ImageInterface
{
use DimensionsTrait;
use FocalPointTrait;

/**
* @var ImageService
Expand Down
1 change: 1 addition & 0 deletions Neos.Media/Classes/Domain/Model/Thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Thumbnail implements ImageInterface
{
use DimensionsTrait;
use QualityTrait;
use FocalPointTrait;

/**
* @var ThumbnailGeneratorStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function canRefresh(Thumbnail $thumbnail)
public function refresh(Thumbnail $thumbnail)
{
try {
/**
* @todo ... add additional crop to ensure that the focal point is
* in view after resizing ... needs common understanding wit
* the thumbnail service here: Packages/Neos/Neos.Media/Classes/Domain/Service/ThumbnailService.php:151
*/
$adjustments = [
new ResizeImageAdjustment(
[
Expand Down
12 changes: 12 additions & 0 deletions Neos.Media/Classes/Domain/Service/ThumbnailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\ImageInterface;
use Neos\Media\Domain\Model\ImageVariant;
use Neos\Media\Domain\Model\Thumbnail;
use Neos\Media\Domain\Model\ThumbnailConfiguration;
use Neos\Media\Domain\Repository\ThumbnailRepository;
Expand Down Expand Up @@ -147,6 +148,17 @@ public function getThumbnail(AssetInterface $asset, ThumbnailConfiguration $conf
if ($thumbnail === null) {
$thumbnail = new Thumbnail($asset, $configuration);

if ($asset instanceof ImageVariant) {
// @todo: needs common understanding of dimension change with resize adjustment
// - if a focal point was set
// - calculate target dimensions here
// - calculate new focalPointAfter transformation
// - store focal point in new image
// has to work closely with: Packages/Neos/Neos.Media/Classes/Domain/Model/ThumbnailGenerator/ImageThumbnailGenerator.php:58
$thumbnail->setFocalPointX($asset->getFocalPointX() ? $asset->getFocalPointX() + 1 : null);
$thumbnail->setFocalPointY($asset->getFocalPointY() ? $asset->getFocalPointY() + 1 : null);
}

// If the thumbnail strategy failed to generate a valid thumbnail
if ($async === false && $thumbnail->getResource() === null && $thumbnail->getStaticResource() === null) {
// the thumbnail should not be persisted at this point, but remove is a no-op if the thumbnail
Expand Down
36 changes: 36 additions & 0 deletions Neos.Media/Migrations/Mysql/Version20240604184831.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Neos\Flow\Persistence\Doctrine\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240604184831 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on "mysql".');

$this->addSql('ALTER TABLE neos_media_domain_model_imagevariant ADD focalpointx INT DEFAULT NULL, ADD focalpointy INT DEFAULT NULL');
$this->addSql('ALTER TABLE neos_media_domain_model_thumbnail ADD focalpointx INT DEFAULT NULL, ADD focalpointy INT DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on "mysql".');

$this->addSql('ALTER TABLE neos_media_domain_model_imagevariant DROP focalpointx, DROP focalpointy');
$this->addSql('ALTER TABLE neos_media_domain_model_thumbnail DROP focalpointx, DROP focalpointy');

}
}

0 comments on commit e63bfeb

Please sign in to comment.