Completed
Pull Request — master (#732)
by 12345
03:41
created

ThumbnailFilterLoader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 43
wmc 14
c 1
b 0
f 0
lcom 0
cbo 4
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C load() 0 37 14
1
<?php
2
3
namespace Liip\ImagineBundle\Imagine\Filter\Loader;
4
5
use Imagine\Filter\Basic\Thumbnail;
6
use Imagine\Image\Box;
7
use Imagine\Image\ImageInterface;
8
9
class ThumbnailFilterLoader implements LoaderInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function load(ImageInterface $image, array $options = array())
15
    {
16
        $mode = ImageInterface::THUMBNAIL_OUTBOUND;
17
        if (!empty($options['mode']) && 'inset' === $options['mode']) {
18
            $mode = ImageInterface::THUMBNAIL_INSET;
19
        }
20
21
        if (!empty($options['filter'])) {
22
            $filter = constant('Imagine\Image\ImageInterface::FILTER_'.strtoupper($options['filter']));
23
        }
24
        if (empty($filter)) {
25
            $filter = ImageInterface::FILTER_UNDEFINED;
26
        }
27
28
        list($width, $height) = $options['size'];
29
30
        $size = $image->getSize();
31
        $origWidth = $size->getWidth();
32
        $origHeight = $size->getHeight();
33
34
        if (null === $width || null === $height) {
35
            if (null === $height) {
36
                $height = (int) (($width / $origWidth) * $origHeight);
37
            } elseif (null === $width) {
38
                $width = (int) (($height / $origHeight) * $origWidth);
39
            }
40
        }
41
42
        if (($origWidth > $width || $origHeight > $height)
43
            || (!empty($options['allow_upscale']) && ($origWidth !== $width || $origHeight !== $height))
44
        ) {
45
            $filter = new Thumbnail(new Box($width, $height), $mode, $filter);
46
            $image = $filter->apply($image);
47
        }
48
49
        return $image;
50
    }
51
}
52