1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Imagine\Filter\Loader; |
13
|
|
|
|
14
|
|
|
use Imagine\Filter\Basic\Thumbnail; |
15
|
|
|
use Imagine\Image\Box; |
16
|
|
|
use Imagine\Image\ImageInterface; |
17
|
|
|
|
18
|
|
|
class ThumbnailFilterLoader implements LoaderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function load(ImageInterface $image, array $options = []) |
24
|
|
|
{ |
25
|
|
|
$mode = ImageInterface::THUMBNAIL_OUTBOUND; |
26
|
|
|
if (!empty($options['mode']) && 'inset' === $options['mode']) { |
27
|
|
|
$mode = ImageInterface::THUMBNAIL_INSET; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!empty($options['filter'])) { |
31
|
|
|
$filter = \constant('Imagine\Image\ImageInterface::FILTER_'.mb_strtoupper($options['filter'])); |
32
|
|
|
} |
33
|
|
|
if (empty($filter)) { |
34
|
|
|
$filter = ImageInterface::FILTER_UNDEFINED; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$width = isset($options['size'][0]) ? $options['size'][0] : null; |
38
|
|
|
$height = isset($options['size'][1]) ? $options['size'][1] : null; |
39
|
|
|
|
40
|
|
|
$size = $image->getSize(); |
41
|
|
|
$origWidth = $size->getWidth(); |
42
|
|
|
$origHeight = $size->getHeight(); |
43
|
|
|
|
44
|
|
|
if (null === $width || null === $height) { |
45
|
|
|
if (null === $height) { |
46
|
|
|
$height = (int) (($width / $origWidth) * $origHeight); |
47
|
|
|
} elseif (null === $width) { |
48
|
|
|
$width = (int) (($height / $origHeight) * $origWidth); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (($origWidth > $width || $origHeight > $height) |
53
|
|
|
|| (!empty($options['allow_upscale']) && ($origWidth !== $width || $origHeight !== $height)) |
54
|
|
|
) { |
55
|
|
|
$filter = new Thumbnail(new Box($width, $height), $mode, $filter); |
56
|
|
|
$image = $filter->apply($image); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $image; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|