Skip to content

Commit

Permalink
fix : bad positioning of multiple-layers tiff
Browse files Browse the repository at this point in the history
add : cli `alchemy:rendition-factory:create` now accepts a directory as input (all files treated)
  • Loading branch information
jygaulier committed Nov 25, 2024
1 parent 4fb492f commit 098fcfb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
31 changes: 29 additions & 2 deletions lib/php/rendition-factory/src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,42 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$time = microtime(true);
$mimeType = $input->getOption('type');
$ret = 0;
$src = $input->getArgument('src');
if (is_dir($src)) {
if (false === ($od = opendir($src))) {
$output->writeln(sprintf('Directory "%s" could not be opened.', $src));

return 1;
}
while ($f = readdir($od)) {
if ('.' === $f || '..' === $f) {
continue;
}
$ret |= $this->doFile($input, $output, $src.'/'.$f);
}
closedir($od);
} else {
$ret = $this->doFile($input, $output, $src);
}

return $ret;
}

protected function doFile(InputInterface $input, OutputInterface $output, string $src): int
{
if (!file_exists($src)) {
$output->writeln(sprintf('File "%s" does not exist.', $src));

return 1;
}

$time = microtime(true);
$output->writeln('');
$output->writeln(sprintf('Processing file: %s', $src));

$mimeType = $input->getOption('type');

if (null === $mimeType) {
$mimeType = $this->mimeTypeGuesser->guessMimeTypeFromPath($src);
$output->writeln(sprintf('MIME type guessed: %s', $mimeType ?? 'unknown'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ public function load(ImageInterface $image, array $options = [])
$options['color'] ?? '#fff',
$options['opacity'] ?? 100,
);
$imageSize = $image->getSize();
$canvas = $this->imagine->create($image->getSize(), $background);

$imageW = $imageSize->getWidth();
$imageH = $imageSize->getHeight();
// DO NOT REMOVE THIS LINE
// This is a workaround to avoid a bug in Imagine that causes wrong positionning
// when the image has multiple layers
$unused = $image->layers()[0];

$canvas = $this->imagine->create($imageSize, $background);

/**
* @var ImageInterface $layer
*/
foreach ($image->layers() as $layer) {
$layerSize = $layer->getSize();
$layerDW = ($imageW - $layerSize->getWidth()) / 2;
$layerDH = ($imageH - $layerSize->getHeight()) / 2;
$canvas->paste($layer, new Point($layerDW, $layerDH));
}
$canvas->paste($image, new Point(0, 0));

return $canvas;
}
Expand Down

0 comments on commit 098fcfb

Please sign in to comment.