Skip to content

Commit

Permalink
PS-720_alpha-tiff-center (#488)
Browse files Browse the repository at this point in the history
* paste layers one by one, centered
BC-Break
- options `size` and `position` are removed
- option `transparency` is renamed `opacity` to conform to values 0=transparent ; 100=full opaque color

* fix : bad positioning of multiple-layers tiff
add : cli `alchemy:rendition-factory:create` now accepts a directory as input (all files treated)

* rename BackgroundFilter to BackgroundFillFilter (filter name = `background_fill`)
  • Loading branch information
jygaulier authored Nov 26, 2024
1 parent 9e354e0 commit d4dbd09
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 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
@@ -0,0 +1,32 @@
<?php

namespace Alchemy\RenditionFactory\Transformer\Image\Imagine;

use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Point;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;

class BackgroundFillFilterLoader implements LoaderInterface
{
public function __construct(protected ImagineInterface $imagine)
{
}

public function load(ImageInterface $image, array $options = [])
{
$background = $image->palette()->color(
$options['color'] ?? '#fff',
$options['opacity'] ?? 100,
);
$canvas = $this->imagine->create($image->getSize(), $background);

// 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->paste($image, new Point(0, 0));

return $canvas;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Liip\ImagineBundle\Imagine\Filter\Loader\AutoRotateFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\BackgroundFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\CropFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\DownscaleFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\FixedFilterLoader;
Expand Down Expand Up @@ -63,7 +62,7 @@ public function createFilterLoaders(TransformationContextInterface $context): ar
$context,
$this->imagine,
),
'background' => new BackgroundFilterLoader($this->imagine),
'background_fill' => new BackgroundFillFilterLoader($this->imagine),
'strip' => new StripFilterLoader(),
'scale' => new ScaleFilterLoader(),
'upscale' => new UpscaleFilterLoader(),
Expand Down

0 comments on commit d4dbd09

Please sign in to comment.