Completed
Push — master ( c2562a...15ecc0 )
by Lukas Kahwe
09:33 queued 06:51
created

ThumbnailFilterLoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 4
dl 0
loc 44
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F load() 0 38 16
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
        $width = isset($options['size'][0]) ? $options['size'][0] : null;
29
        $height = isset($options['size'][1]) ? $options['size'][1] : null;
30
31
        $size = $image->getSize();
32
        $origWidth = $size->getWidth();
33
        $origHeight = $size->getHeight();
34
35
        if (null === $width || null === $height) {
36
            if (null === $height) {
37
                $height = (int) (($width / $origWidth) * $origHeight);
38
            } elseif (null === $width) {
39
                $width = (int) (($height / $origHeight) * $origWidth);
40
            }
41
        }
42
43
        if (($origWidth > $width || $origHeight > $height)
44
            || (!empty($options['allow_upscale']) && ($origWidth !== $width || $origHeight !== $height))
45
        ) {
46
            $filter = new Thumbnail(new Box($width, $height), $mode, $filter);
47
            $image = $filter->apply($image);
48
        }
49
50
        return $image;
51
    }
52
}
53