Skip to content

Commit

Permalink
Merge pull request #1199 from franmomu/merge_from_1_to_2
Browse files Browse the repository at this point in the history
Upgrade 2.0 from 1.0
  • Loading branch information
dbu committed Aug 6, 2019
2 parents 647d51e + fe8a2f0 commit 715e567
Show file tree
Hide file tree
Showing 60 changed files with 987 additions and 323 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -31,15 +31,14 @@ matrix:
env:
- SYMFONY_VERSION=4.0.*
- COMPOSER_FLAGS="--prefer-lowest"
- DEPENDENCIES="symfony/phpunit-bridge:^4"
- php: 7.2
- php: 7.2
env: SYMFONY_VERSION=3.4.*
- php: 7.2
env: SYMFONY_VERSION=4.0.*
- php: 7.2
env:
- DEPENDENCIES="symfony/phpunit-bridge:^4"
- DEPENDENCIES="symfony/phpunit-bridge:^4.2"
- COMPOSER_UPDATE_FLAGS="--no-dev"
- php: 7.2
env:
Expand Down
6 changes: 3 additions & 3 deletions Async/ResolveCache.php
Expand Up @@ -22,7 +22,7 @@ class ResolveCache implements \JsonSerializable
private $path;

/**
* @var array|null|\string[]
* @var array|\string[]|null
*/
private $filters;

Expand Down Expand Up @@ -52,7 +52,7 @@ public function getPath(): string
}

/**
* @return null|\string[]
* @return \string[]|null
*/
public function getFilters()
{
Expand Down Expand Up @@ -88,7 +88,7 @@ public static function jsonDeserialize(string $json): self
throw new LogicException('The message does not contain "path" but it is required.');
}

if (!(null === $data['filters'] || is_array($data['filters']))) {
if (!(null === $data['filters'] || \is_array($data['filters']))) {
throw new LogicException('The message filters could be either null or array.');
}

Expand Down
2 changes: 1 addition & 1 deletion Binary/Loader/ChainLoader.php
Expand Up @@ -68,7 +68,7 @@ private static function getLoaderExceptionMessage(string $path, array $exception
return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [
$path,
implode(', ', $loaders),
count($loaders),
\count($loaders),
implode(', ', $exceptions),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion Binary/Loader/StreamLoader.php
Expand Up @@ -39,7 +39,7 @@ public function __construct($wrapperPrefix, $context = null)
{
$this->wrapperPrefix = $wrapperPrefix;

if ($context && !is_resource($context)) {
if ($context && !\is_resource($context)) {
throw new \InvalidArgumentException('The given context is no valid resource.');
}

Expand Down
2 changes: 1 addition & 1 deletion Binary/Locator/FileSystemLocator.php
Expand Up @@ -131,7 +131,7 @@ private function sanitizeAbsolutePath(string $path): string
return 0 === mb_strpos($path, $root);
});

if (0 === count($roots)) {
if (0 === \count($roots)) {
throw new NotLoadableException(
sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"', $path, implode(':', $this->roots))
);
Expand Down
2 changes: 1 addition & 1 deletion Controller/ImagineController.php
Expand Up @@ -110,7 +110,7 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter)
$resolver = $request->get('resolver');
$runtimeConfig = $request->query->get('filters', []);

if (!is_array($runtimeConfig)) {
if (!\is_array($runtimeConfig)) {
throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"', $runtimeConfig));
}

Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/FiltersCompilerPass.php
Expand Up @@ -23,7 +23,7 @@ public function process(ContainerBuilder $container)
{
$tags = $container->findTaggedServiceIds('liip_imagine.filter.loader');

if (count($tags) > 0 && $container->hasDefinition('liip_imagine.filter.manager')) {
if (\count($tags) > 0 && $container->hasDefinition('liip_imagine.filter.manager')) {
$manager = $container->getDefinition('liip_imagine.filter.manager');

foreach ($tags as $id => $tag) {
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/LoadersCompilerPass.php
Expand Up @@ -23,7 +23,7 @@ public function process(ContainerBuilder $container)
{
$tags = $container->findTaggedServiceIds('liip_imagine.binary.loader');

if (count($tags) > 0 && $container->hasDefinition('liip_imagine.data.manager')) {
if (\count($tags) > 0 && $container->hasDefinition('liip_imagine.data.manager')) {
$manager = $container->getDefinition('liip_imagine.data.manager');

foreach ($tags as $id => $tag) {
Expand Down
Expand Up @@ -53,7 +53,7 @@ public function process(ContainerBuilder $container): void
*/
protected function isExifExtensionLoaded(): bool
{
return extension_loaded('exif');
return \extension_loaded('exif');
}

/**
Expand Down
Expand Up @@ -28,7 +28,7 @@ public function process(ContainerBuilder $container)
{
$tags = $container->findTaggedServiceIds('liip_imagine.filter.post_processor');

if (count($tags) > 0 && $container->hasDefinition('liip_imagine.filter.manager')) {
if (\count($tags) > 0 && $container->hasDefinition('liip_imagine.filter.manager')) {
$manager = $container->getDefinition('liip_imagine.filter.manager');

foreach ($tags as $id => $tag) {
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/ResolversCompilerPass.php
Expand Up @@ -23,7 +23,7 @@ public function process(ContainerBuilder $container)
{
$tags = $container->findTaggedServiceIds('liip_imagine.cache.resolver');

if (count($tags) > 0 && $container->hasDefinition('liip_imagine.cache.manager')) {
if (\count($tags) > 0 && $container->hasDefinition('liip_imagine.cache.manager')) {
$manager = $container->getDefinition('liip_imagine.cache.manager');

foreach ($tags as $id => $tag) {
Expand Down
10 changes: 5 additions & 5 deletions DependencyInjection/Configuration.php
Expand Up @@ -77,23 +77,23 @@ public function getConfigTreeBuilder()
$v['loaders'] = [];
}

if (false === is_array($v['loaders'])) {
if (false === \is_array($v['loaders'])) {
throw new \LogicException('Loaders has to be array');
}

if (false === array_key_exists('default', $v['loaders'])) {
if (false === \array_key_exists('default', $v['loaders'])) {
$v['loaders']['default'] = ['filesystem' => null];
}

if (empty($v['resolvers'])) {
$v['resolvers'] = [];
}

if (false === is_array($v['resolvers'])) {
if (false === \is_array($v['resolvers'])) {
throw new \LogicException('Resolvers has to be array');
}

if (false === array_key_exists('default', $v['resolvers'])) {
if (false === \array_key_exists('default', $v['resolvers'])) {
$v['resolvers']['default'] = ['web_path' => null];
}

Expand All @@ -107,7 +107,7 @@ public function getConfigTreeBuilder()
->scalarNode('driver')->defaultValue('gd')
->validate()
->ifTrue(function ($v) {
return !in_array($v, ['gd', 'imagick', 'gmagick'], true);
return !\in_array($v, ['gd', 'imagick', 'gmagick'], true);
})
->thenInvalid('Invalid imagine driver specified: %s')
->end()
Expand Down
Expand Up @@ -105,7 +105,7 @@ private function resolveDataRoots(array $staticPaths, array $config, ContainerBu
$resourcePaths = [];

foreach ($this->getBundleResourcePaths($container) as $name => $path) {
if (('whitelist' === $config['access_control_type']) === in_array($name, $config['access_control_list'], true) && is_dir($path)) {
if (('whitelist' === $config['access_control_type']) === \in_array($name, $config['access_control_list'], true) && is_dir($path)) {
$resourcePaths[$name] = $path;
}
}
Expand Down Expand Up @@ -159,7 +159,7 @@ private function getBundlePathsUsingNamedObj(array $classes)
throw new InvalidArgumentException(sprintf('Unable to resolve bundle "%s" while auto-registering bundle resource paths.', $c), null, $exception);
}

$paths[$r->getShortName()] = dirname($r->getFileName());
$paths[$r->getShortName()] = \dirname($r->getFileName());
}

return $paths;
Expand Down
2 changes: 1 addition & 1 deletion Events/CacheResolveEvent.php
Expand Up @@ -41,7 +41,7 @@ class CacheResolveEvent extends Event
*
* @param string $path
* @param string $filter
* @param null|string $url
* @param string|null $url
*/
public function __construct($path, $filter, $url = null)
{
Expand Down
18 changes: 18 additions & 0 deletions Exception/Imagine/Filter/LoadFilterException.php
@@ -0,0 +1,18 @@
<?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\Exception\Imagine\Filter;

use Liip\ImagineBundle\Exception\ExceptionInterface;

class LoadFilterException extends \RuntimeException implements ExceptionInterface
{
}
4 changes: 2 additions & 2 deletions Imagine/Cache/CacheManager.php
Expand Up @@ -226,10 +226,10 @@ public function remove($paths = null, $filters = null)
{
if (null === $filters) {
$filters = array_keys($this->filterConfig->all());
} elseif (!is_array($filters)) {
} elseif (!\is_array($filters)) {
$filters = [$filters];
}
if (!is_array($paths)) {
if (!\is_array($paths)) {
$paths = [$paths];
}

Expand Down
2 changes: 1 addition & 1 deletion Imagine/Cache/Resolver/AbstractFilesystemResolver.php
Expand Up @@ -165,7 +165,7 @@ protected function getRequest()
protected function makeFolder($dir)
{
if (!is_dir($dir)) {
$parent = dirname($dir);
$parent = \dirname($dir);
try {
$this->makeFolder($parent);
$this->filesystem->mkdir($dir);
Expand Down
4 changes: 2 additions & 2 deletions Imagine/Cache/Resolver/CacheResolver.php
Expand Up @@ -212,7 +212,7 @@ protected function saveToCache($cacheKey, $content)
if ($this->cache->contains($indexKey)) {
$index = (array) $this->cache->fetch($indexKey);

if (!in_array($cacheKey, $index, true)) {
if (!\in_array($cacheKey, $index, true)) {
$index[] = $cacheKey;
}
} else {
Expand All @@ -236,7 +236,7 @@ protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'global_prefix' => 'liip_imagine.resolver_cache',
'prefix' => get_class($this->resolver),
'prefix' => \get_class($this->resolver),
'index_key' => 'index',
]);

Expand Down
2 changes: 1 addition & 1 deletion Imagine/Filter/FilterConfiguration.php
Expand Up @@ -39,7 +39,7 @@ public function __construct(array $filters = [])
*/
public function get($filter)
{
if (false === array_key_exists($filter, $this->filters)) {
if (false === \array_key_exists($filter, $this->filters)) {
throw new NonExistingFilterException(sprintf('Could not find configuration for a filter: %s', $filter));
}

Expand Down
4 changes: 2 additions & 2 deletions Imagine/Filter/FilterManager.php
Expand Up @@ -221,7 +221,7 @@ private function sanitizeFilters(array $filters): array
return isset($this->loaders[$name]);
}, ARRAY_FILTER_USE_KEY);

if (count($filters) !== count($sanitized)) {
if (\count($filters) !== \count($sanitized)) {
throw new \InvalidArgumentException(sprintf('Could not find filter(s): %s', implode(', ', array_map(function (string $name): string {
return sprintf('"%s"', $name);
}, array_diff(array_keys($filters), array_keys($sanitized))))));
Expand All @@ -241,7 +241,7 @@ private function sanitizePostProcessors(array $processors): array
return isset($this->postProcessors[$name]);
}, ARRAY_FILTER_USE_KEY);

if (count($processors) !== count($sanitized)) {
if (\count($processors) !== \count($sanitized)) {
throw new \InvalidArgumentException(sprintf('Could not find post processor(s): %s', implode(', ', array_map(function (string $name): string {
return sprintf('"%s"', $name);
}, array_diff(array_keys($processors), array_keys($sanitized))))));
Expand Down
2 changes: 1 addition & 1 deletion Imagine/Filter/Loader/AutoRotateFilterLoader.php
Expand Up @@ -104,6 +104,6 @@ private function getOrientation(ImageInterface $image)
*/
private function isFlipped($orientation)
{
return in_array((int) $orientation, [2, 4, 5, 7], true);
return \in_array((int) $orientation, [2, 4, 5, 7], true);
}
}
8 changes: 8 additions & 0 deletions Imagine/Filter/Loader/BackgroundFilterLoader.php
Expand Up @@ -62,10 +62,18 @@ public function load(ImageInterface $image, array $options = [])
$x = 0;
$y = ($height - $image->getSize()->getHeight()) / 2;
break;
case 'centerright':
$x = $width - $image->getSize()->getWidth();
$y = ($height - $image->getSize()->getHeight()) / 2;
break;
case 'center':
$x = ($width - $image->getSize()->getWidth()) / 2;
$y = ($height - $image->getSize()->getHeight()) / 2;
break;
case 'centerleft':
$x = 0;
$y = ($height - $image->getSize()->getHeight()) / 2;
break;
case 'right':
$x = $width - $image->getSize()->getWidth();
$y = ($height - $image->getSize()->getHeight()) / 2;
Expand Down
65 changes: 65 additions & 0 deletions Imagine/Filter/Loader/FixedFilterLoader.php
@@ -0,0 +1,65 @@
<?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\Imagine\Filter\Loader;

use Imagine\Filter\Basic\Crop;
use Imagine\Filter\Basic\Resize;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Fixed size filter.
*
* @author Robbe Clerckx <https://github.com/robbeman>
*/
class FixedFilterLoader implements LoaderInterface
{
/**
* @param ImageInterface $image
* @param array $options
*
* @return ImageInterface
*/
public function load(ImageInterface $image, array $options = [])
{
$optionsResolver = new OptionsResolver();
$optionsResolver->setRequired(['width', 'height']);
$options = $optionsResolver->resolve($options);

// get the original image size and create a crop box
$size = $image->getSize();
$box = new Box($options['width'], $options['height']);

// determine scale
if ($size->getWidth() / $size->getHeight() > $box->getWidth() / $box->getHeight()) {
$size = $size->heighten($box->getHeight());
} else {
$size = $size->widen($box->getWidth());
}

// define filters
$resize = new Resize($size);
$origin = new Point(
floor(($size->getWidth() - $box->getWidth()) / 2),
floor(($size->getHeight() - $box->getHeight()) / 2)
);
$crop = new Crop($origin, $box);

// apply filters to image
$image = $resize->apply($image);
$image = $crop->apply($image);

return $image;
}
}

0 comments on commit 715e567

Please sign in to comment.