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

UpscaleFilterLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B load() 0 25 5
1
<?php
2
3
namespace Liip\ImagineBundle\Imagine\Filter\Loader;
4
5
use Imagine\Filter\Basic\Resize;
6
use Imagine\Image\ImageInterface;
7
use Imagine\Image\Box;
8
9
/**
10
 * Upscale filter.
11
 *
12
 * @author Maxime Colin <[email protected]>
13
 */
14
class UpscaleFilterLoader implements LoaderInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function load(ImageInterface $image, array $options = array())
20
    {
21
        if (!isset($options['min'])) {
22
            throw new \InvalidArgumentException('Missing min option.');
23
        }
24
25
        list($width, $height) = $options['min'];
26
27
        $size = $image->getSize();
28
        $origWidth = $size->getWidth();
29
        $origHeight = $size->getHeight();
30
31
        if ($origWidth < $width || $origHeight < $height) {
32
            $widthRatio = $width / $origWidth;
33
            $heightRatio = $height / $origHeight;
34
35
            $ratio = $widthRatio > $heightRatio ? $widthRatio : $heightRatio;
36
37
            $filter = new Resize(new Box(round($origWidth * $ratio), round($origHeight * $ratio)));
38
39
            return $filter->apply($image);
40
        }
41
42
        return $image;
43
    }
44
}
45