forked from liip/LiipImagineBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResampleFilterLoader.php
More file actions
140 lines (119 loc) · 4.33 KB
/
ResampleFilterLoader.php
File metadata and controls
140 lines (119 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Liip\ImagineBundle\Exception\Imagine\Filter\LoadFilterException;
use Liip\ImagineBundle\Exception\InvalidArgumentException;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ResampleFilterLoader implements LoaderInterface
{
/**
* @var ImagineInterface
*/
private $imagine;
public function __construct(ImagineInterface $imagine)
{
$this->imagine = $imagine;
}
/**
* @throws LoadFilterException
*
* @return ImageInterface
*/
public function load(ImageInterface $image, array $options = [])
{
$options = $this->resolveOptions($options);
$tmpFile = $this->getTemporaryFile($options['temp_dir']);
try {
$image->save($tmpFile, $this->getImagineSaveOptions($options));
$image = $this->imagine->open($tmpFile);
$this->delTemporaryFile($tmpFile);
} catch (\Exception $exception) {
$this->delTemporaryFile($tmpFile);
throw new LoadFilterException('Unable to save/open file in resample filter loader.', $exception->getCode(), $exception);
}
return $image;
}
/**
* @param string $path
*
* @throws \RuntimeException
*
* @return string
*/
private function getTemporaryFile($path)
{
if (!is_dir($path) || false === $file = tempnam($path, 'liip-imagine-bundle')) {
throw new \RuntimeException(sprintf('Unable to create temporary file in "%s" base path.', $path));
}
return $file;
}
/**
* @param $file
*
* @throws \RuntimeException
*/
private function delTemporaryFile($file)
{
if (file_exists($file)) {
unlink($file);
}
}
/**
* @return array
*/
private function getImagineSaveOptions(array $options)
{
$saveOptions = [
'resolution-units' => $options['unit'],
'resolution-x' => $options['x'],
'resolution-y' => $options['y'],
];
if (isset($options['filter'])) {
$saveOptions['resampling-filter'] = $options['filter'];
}
return $saveOptions;
}
/**
* @return array
*/
private function resolveOptions(array $options)
{
$resolver = new OptionsResolver();
$resolver->setRequired(['x', 'y', 'unit', 'temp_dir']);
$resolver->setDefined(['filter']);
$resolver->setDefault('temp_dir', sys_get_temp_dir());
$resolver->setDefault('filter', 'UNDEFINED');
$resolver->setAllowedTypes('x', ['int', 'float']);
$resolver->setAllowedTypes('y', ['int', 'float']);
$resolver->setAllowedTypes('temp_dir', ['string']);
$resolver->setAllowedTypes('filter', ['string']);
$resolver->setAllowedValues('unit', [
ImageInterface::RESOLUTION_PIXELSPERINCH,
ImageInterface::RESOLUTION_PIXELSPERCENTIMETER,
]);
$resolver->setNormalizer('filter', function (Options $options, $value) {
foreach (['\Imagine\Image\ImageInterface::FILTER_%s', '\Imagine\Image\ImageInterface::%s', '%s'] as $format) {
if (\defined($constant = sprintf($format, mb_strtoupper($value))) || \defined($constant = sprintf($format, $value))) {
return \constant($constant);
}
}
throw new InvalidArgumentException('Invalid value for "filter" option: must be a valid constant resolvable using one of formats '.'"\Imagine\Image\ImageInterface::FILTER_%s", "\Imagine\Image\ImageInterface::%s", or "%s".');
});
try {
return $resolver->resolve($options);
} catch (ExceptionInterface $exception) {
throw new InvalidArgumentException(sprintf('Invalid option(s) passed to %s::load().', __CLASS__), $exception->getCode(), $exception);
}
}
}