Skip to content

Commit

Permalink
Set DefaultMetadataReader when ext-exif is not present
Browse files Browse the repository at this point in the history
This fixes the issue where an exif reader is used, but ext-exif
is not loaded.

Closes: #501
  • Loading branch information
cedricziel committed Jan 3, 2017
1 parent 1eac259 commit 764a1b6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
67 changes: 67 additions & 0 deletions DependencyInjection/Compiler/MetadataReaderCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) /~https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* By default, a metadata reader based on the exif php extension is used.
* This compiler pass checks if the extension is loaded an switches to a simpler
* implementation if not.
*/
class MetadataReaderCompilerPass implements CompilerPassInterface
{
const METADATA_READER_PARAM = 'liip_imagine.meta_data.reader.class';

const DEFAULT_METADATA_READER_CLASS = 'Imagine\Image\Metadata\DefaultMetadataReader';

const EXIF_METADATA_READER_CLASS = 'Imagine\Image\Metadata\ExifMetadataReader';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!extension_loaded('exif') && $this->isDefaultMetadataReader($container)) {
$container->setParameter(self::METADATA_READER_PARAM, self::DEFAULT_METADATA_READER_CLASS);
$this->logMetadataReaderReplaced($container);
}
}

/**
* @param ContainerBuilder $container
*
* @return bool
*/
protected function isDefaultMetadataReader(ContainerBuilder $container)
{
$currentMetadataReaderParameter = $container->getParameter(self::METADATA_READER_PARAM);

return $currentMetadataReaderParameter === self::EXIF_METADATA_READER_CLASS;
}

/**
* @param ContainerBuilder $container
*/
protected function logMetadataReaderReplaced(ContainerBuilder $container)
{
$compiler = $container->getCompiler();
$formatter = $compiler->getLoggingFormatter();
$message = 'Automatically replaced Imagine ExifMetadataReader with DefaultMetadataReader; '.
'you might experience issues with LiipImagineBundle; reason: PHP extension "exif" is missing; solution: '.
'for advanced metadata extraction install the PHP extension "exif" or set a custom MetadataReader '.
'through the "liip_imagine.meta_data.reader.class" parameter';

$compiler->addLogMessage($formatter->format($this, $message));
}
}
2 changes: 2 additions & 0 deletions LiipImagineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Liip\ImagineBundle\DependencyInjection\Compiler\FiltersCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\LoadersCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\PostProcessorsCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\ResolversCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FileSystemLoaderFactory;
Expand All @@ -38,6 +39,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new FiltersCompilerPass());
$container->addCompilerPass(new PostProcessorsCompilerPass());
$container->addCompilerPass(new ResolversCompilerPass());
$container->addCompilerPass(new MetadataReaderCompilerPass());

/** @var $extension LiipImagineExtension */
$extension = $container->getExtension('liip_imagine');
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"suggest": {
"amazonwebservices/aws-sdk-for-php": "Required to use AWS version 1 cache resolver.",
"aws/aws-sdk-php": "Required to use AWS version 2/3 cache resolver.",
"ext-exif": "Required to read metadata from Exif information",
"league/flysystem": "Required to use FlySystem data loader or cache resolver.",
"monolog/monolog": "A psr/log compatible logger is required to enable logging.",
"twig/twig": "Required to use the provided Twig extension. Version 1.12 or greater needed."
Expand Down

0 comments on commit 764a1b6

Please sign in to comment.