Skip to content

Commit

Permalink
add "Summary" module
Browse files Browse the repository at this point in the history
add "pre-clip" video filter
optims by using `clip`(s) and `copy` when possible
composer cs
  • Loading branch information
jygaulier committed Sep 17, 2024
1 parent c1fa104 commit bc30d52
Show file tree
Hide file tree
Showing 26 changed files with 395 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');

}
}
10 changes: 3 additions & 7 deletions lib/php/rendition-factory-bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ services:
tags:
- { name: !php/const Alchemy\RenditionFactory\Transformer\TransformerModuleInterface::TAG }

# Alchemy\RenditionFactory\Transformer\Video\VideoToAnimatedGifTransformerModule:
# tags:
# - { name: !php/const Alchemy\RenditionFactory\Transformer\TransformerModuleInterface::TAG }
#
# Alchemy\RenditionFactory\Transformer\Video\VideoResizeTransformerModule:
# tags:
# - { name: !php/const Alchemy\RenditionFactory\Transformer\TransformerModuleInterface::TAG }
Alchemy\RenditionFactory\Transformer\Video\SummaryTransformerModule:
tags:
- { name: !php/const Alchemy\RenditionFactory\Transformer\TransformerModuleInterface::TAG }

Alchemy\RenditionFactory\Transformer\Video\FFMpegTransformerModule:
tags:
Expand Down
8 changes: 5 additions & 3 deletions lib/php/rendition-factory/src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Alchemy\RenditionFactory\DTO\CreateRenditionOptions;
use Alchemy\RenditionFactory\MimeType\MimeTypeGuesser;
use Alchemy\RenditionFactory\RenditionCreator;
use InvalidArgumentException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -46,6 +45,7 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$time = microtime(true);
$mimeType = $input->getOption('type');
$src = $input->getArgument('src');
if (!file_exists($src)) {
Expand All @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

if ($mimeType === null) {
if (null === $mimeType) {
$mimeType = $this->mimeTypeGuesser->guessMimeTypeFromPath($src);
}

Expand All @@ -71,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$buildConfig,
$options
);
} catch (InvalidArgumentException $e) {
} catch (\InvalidArgumentException $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));

return 1;
Expand All @@ -96,6 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->renditionCreator->cleanUp();
}

$output->writeln(sprintf('Execution time: %0.2f', microtime(true) - $time));

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Alchemy\RenditionFactory\Config;

use Alchemy\RenditionFactory\DTO\BuildConfig\BuildConfig;
use Alchemy\RenditionFactory\DTO\BuildConfig\FamilyBuildConfig;

interface FileLoaderInterface
{
Expand Down
4 changes: 1 addition & 3 deletions lib/php/rendition-factory/src/Config/YamlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ private function parseConfig(array $data): BuildConfig
$families = [];
foreach ($data as $familyKey => $familyConfig) {
if (null === $family = FamilyEnum::tryFrom($familyKey)) {
throw new ModelException(sprintf('Invalid file type family "%s". Expected one of %s', $familyKey, implode(', ', array_map(
fn (FamilyEnum $family) => $family->value,
FamilyEnum::cases()))));
throw new ModelException(sprintf('Invalid file type family "%s". Expected one of %s', $familyKey, implode(', ', array_map(fn (FamilyEnum $family) => $family->value, FamilyEnum::cases()))));
}

if (null !== $familyConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ public function __construct(
private MimeTypeGuesser $mimeTypeGuesser,
?string $workingDirectory = null,
private ?HttpClientInterface $client = null,
private ?LoggerInterface $logger = null
)
{
private ?LoggerInterface $logger = null,
) {
$this->workingDirectory = $workingDirectory ?? sys_get_temp_dir();
}

public function create(
?CreateRenditionOptions $options = null
): TransformationContext
{
?CreateRenditionOptions $options = null,
): TransformationContext {
$baseDir = $options?->getWorkingDirectory() ?? $this->workingDirectory;
$cacheDir = $options?->getCacheDirectory() ?? $baseDir.'/cache';
$dateWorkingDir = $baseDir.'/'.date('Y-m-d');
Expand Down
1 change: 0 additions & 1 deletion lib/php/rendition-factory/src/DTO/BaseFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Alchemy\RenditionFactory\DTO;


abstract readonly class BaseFile implements BaseFileInterface
{
public function __construct(
Expand Down
2 changes: 2 additions & 0 deletions lib/php/rendition-factory/src/DTO/BaseFileInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public function getPath(): string;
public function getType(): string;

public function getFamily(): FamilyEnum;

public function getExtension(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
{
public function __construct(
private array $families,
)
{
) {
}

public function getFamily(FamilyEnum $family): ?FamilyBuildConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public function __construct(
private array $transformations,

private array $normalization,
)
{
) {
}

public function getTransformations(): array
Expand All @@ -22,5 +21,4 @@ public function getNormalization(): array
{
return $this->normalization;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public function __construct(
private string $module,
private array $options,
private ?string $description,
)
{
) {
}

public function getModule(): string
Expand Down
3 changes: 1 addition & 2 deletions lib/php/rendition-factory/src/DTO/CreateRenditionOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public function __construct(
private ?string $workingDirectory = null,
private ?string $cacheDirectory = null,
private ?MetadataContainerInterface $metadataContainer = null,
)
{
) {
}

public function getWorkingDirectory(): ?string
Expand Down
4 changes: 2 additions & 2 deletions lib/php/rendition-factory/src/DTO/Metadata/MetadataArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
final readonly class MetadataArray implements MetadataContainerInterface
{
public function __construct(
private array $metadata
private array $metadata,
) {
}

public function getMetadata(string $name): string|null
public function getMetadata(string $name): ?string
{
return $this->metadata[$name] ?? null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

interface MetadataContainerInterface
{
public function getMetadata(string $name): string|null;
public function getMetadata(string $name): ?string;

public function getTemplatingContext(): array;
}
8 changes: 4 additions & 4 deletions lib/php/rendition-factory/src/FileFamilyGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function isAnimatedGif(string $src): bool
$count = 0;

while (!feof($fh) && $count < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$chunk = fread($fh, 1024 * 100); // read 100kb at a time
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $chunk);
}

Expand All @@ -63,12 +63,12 @@ private function isAnimatedGif(string $src): bool
private function isWebpAnimated(string $src): bool
{
$result = false;
$fh = fopen($src, "rb");
$fh = fopen($src, 'rb');
fseek($fh, 12);
if (fread($fh, 4) === 'VP8X') {
if ('VP8X' === fread($fh, 4)) {
fseek($fh, 16);
$myByte = fread($fh, 1);
$result = (bool)(((ord($myByte) >> 1) & 1));
$result = (bool) ((ord($myByte) >> 1) & 1);
}
fclose($fh);

Expand Down
19 changes: 5 additions & 14 deletions lib/php/rendition-factory/src/RenditionCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,16 @@ public function createRendition(
string $src,
string $mimeType,
BuildConfig $buildConfig,
?CreateRenditionOptions $options = null
): OutputFileInterface
{
?CreateRenditionOptions $options = null,
): OutputFileInterface {
$inputFile = new InputFile($src, $mimeType, $this->fileFamilyGuesser->getFamily($src, $mimeType));
if (null == $familyBuildConfig = $buildConfig->getFamily($inputFile->getFamily())) {
throw new \InvalidArgumentException(sprintf(
'No build config defined for family "%s" (type: "%s")',
$inputFile->getFamily()->value,
$mimeType,
));
throw new \InvalidArgumentException(sprintf('No build config defined for family "%s" (type: "%s")', $inputFile->getFamily()->value, $mimeType));
}

$transformations = $familyBuildConfig->getTransformations();
if (empty($transformations)) {
throw new \InvalidArgumentException(sprintf(
'No transformation defined for family "%s" (type: "%s")',
$inputFile->getFamily()->value,
$mimeType,
));
throw new \InvalidArgumentException(sprintf('No transformation defined for family "%s" (type: "%s")', $inputFile->getFamily()->value, $mimeType));
}

$context = $this->contextFactory->create(
Expand Down Expand Up @@ -83,7 +74,7 @@ public function cleanUp(): void

private static function recursiveRmDir(string $dir): void
{
$files = array_diff(scandir($dir), array('.','..'));
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$path = $dir.'/'.$file;
if (is_dir($path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
{
public function __construct(
private TransformationContext $context,
)
{
) {
}

public function offsetExists(mixed $offset): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ public function __construct(
private TransformationContext $context,
private ImagineInterface $imagine,
private TemplateResolverInterface $templateResolver,
private string $fontDirectory = __DIR__.'/../../../../../fonts'
)
{
private string $fontDirectory = __DIR__.'/../../../../../fonts',
) {
}

public function load(ImageInterface $image, array $options = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
public function __construct(
private ImagineInterface $imagine,
private TemplateResolverInterface $templateResolver,
)
{
) {
}

public function createFilterManager(TransformationContext $context): FilterManager
Expand All @@ -48,7 +47,7 @@ public function createFilterManager(TransformationContext $context): FilterManag
'resize' => new ResizeFilterLoader(),
'thumbnail' => new ThumbnailFilterLoader(),
'crop' => new CropFilterLoader(),
// 'grayscale' => new GrayscaleFilterLoader(), Disabled because too slow (OnPixelBased)
// 'grayscale' => new GrayscaleFilterLoader(), Disabled because too slow (OnPixelBased)
'watermark' => new WatermarkDownloadProxyFilterLoader(
$context,
$this->imagine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
{
public function __construct(
private ImagineFilterFactory $filterFactory,
)
{
) {
}

public static function getName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
public function __construct(
private TransformationContext $context,
private ImagineInterface $imagine,
)
{
) {
}

public function load(ImageInterface $image, array $options = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(
private MimeTypeGuesser $mimeTypeGuesser,
private HttpClientInterface $client,
private LoggerInterface $logger,
private ?MetadataContainerInterface $metadata = null
private ?MetadataContainerInterface $metadata = null,
) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Alchemy\RenditionFactory\Transformer\Video\FFMpeg\Format\Video;

class WebM extends \FFMpeg\Format\Video\WebM
{
protected $videoCodecs = [];

public function __construct($audioCodec = 'libvorbis', $videoCodec = 'libvpx')
{
$this->videoCodecs = parent::getAvailableVideoCodecs();
if (!in_array('copy', $this->videoCodecs)) {
$this->videoCodecs[] = 'copy';
}
parent::__construct($audioCodec, $videoCodec);
}

public function getAvailableVideoCodecs()
{
return $this->videoCodecs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Alchemy\RenditionFactory\Transformer\Video\FFMpeg\Format\Video;

class X264 extends \FFMpeg\Format\Video\X264
{
protected $videoCodecs = [];

public function __construct($audioCodec = 'aac', $videoCodec = 'libx264')
{
$this->videoCodecs = parent::getAvailableVideoCodecs();
if (!in_array('copy', $this->videoCodecs)) {
$this->videoCodecs[] = 'copy';
}
parent::__construct($audioCodec, $videoCodec);
}

public function getAvailableVideoCodecs()
{
return $this->videoCodecs;
}
}
Loading

0 comments on commit bc30d52

Please sign in to comment.