-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add `set-dpi` module fix bad subdef settings import
- Loading branch information
Showing
4 changed files
with
135 additions
and
9 deletions.
There are no files selected for viewing
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
88 changes: 88 additions & 0 deletions
88
lib/php/rendition-factory/src/Transformer/Image/SetDpiTransformerModule.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,88 @@ | ||
<?php | ||
|
||
namespace Alchemy\RenditionFactory\Transformer\Image; | ||
|
||
use Alchemy\RenditionFactory\Context\TransformationContextInterface; | ||
use Alchemy\RenditionFactory\DTO\FamilyEnum; | ||
use Alchemy\RenditionFactory\DTO\InputFileInterface; | ||
use Alchemy\RenditionFactory\DTO\OutputFile; | ||
use Alchemy\RenditionFactory\DTO\OutputFileInterface; | ||
use Alchemy\RenditionFactory\Transformer\Documentation; | ||
use Alchemy\RenditionFactory\Transformer\TransformerConfigHelper; | ||
use Alchemy\RenditionFactory\Transformer\TransformerModuleInterface; | ||
// use PHPExiftool\Driver\Metadata\Metadata; | ||
use PHPExiftool\Driver\Metadata\MetadataBag; | ||
use PHPExiftool\Exiftool; | ||
use PHPExiftool\Writer; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
|
||
|
||
final readonly class SetDpiTransformerModule implements TransformerModuleInterface | ||
{ | ||
public function __construct(private LoggerInterface $logger) { | ||
} | ||
|
||
public static function getName(): string | ||
{ | ||
return 'set_dpi'; | ||
} | ||
|
||
public function getDocumentation(): Documentation | ||
{ | ||
$treeBuilder = TransformerConfigHelper::createBaseTree(self::getName()); | ||
$this->buildConfiguration($treeBuilder->getRootNode()->children()); | ||
|
||
return new Documentation( | ||
$treeBuilder, | ||
<<<HEADER | ||
Change the dpi metadata of an image (no resampling). | ||
HEADER | ||
); | ||
} | ||
|
||
public function buildConfiguration(NodeBuilder $builder): void | ||
{ | ||
// @formatter:off | ||
$builder | ||
->arrayNode('options') | ||
->children() | ||
->IntegerNode('dpi') | ||
->isRequired() | ||
->example('72') | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
// @formatter:on | ||
} | ||
|
||
public function transform(InputFileInterface $inputFile, array $options, TransformationContextInterface $context): OutputFileInterface | ||
{ | ||
if($inputFile->getFamily() !== FamilyEnum::Image) { | ||
throw new \InvalidArgumentException('Input file must be an image'); | ||
} | ||
$dpi = $options['dpi']; | ||
$this->logger->info(sprintf('Setting DPI to %s', $dpi)); | ||
$writer = Writer::create( | ||
new Exiftool($this->logger) | ||
); | ||
$writer->write( | ||
$inputFile->getPath(), | ||
new MetadataBag([ | ||
// new Metadata([ | ||
// 'dpi' => $dpi, | ||
// ]), | ||
]), | ||
null, | ||
[$dpi, $dpi] | ||
); | ||
|
||
return new OutputFile( | ||
$inputFile->getPath(), | ||
$inputFile->getType(), | ||
FamilyEnum::Image, | ||
false // TODO implement projection | ||
); | ||
} | ||
} |