Completed
Pull Request — master (#770)
by
unknown
03:04
created

DownscaleFilterLoader::load()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 34
Code Lines 19

Duplication

Lines 5
Ratio 14.71 %

Importance

Changes 5
Bugs 4 Features 0
Metric Value
c 5
b 4
f 0
dl 5
loc 34
rs 6.7272
cc 7
eloc 19
nc 6
nop 2
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
 * downscale filter.
11
 */
12
class DownscaleFilterLoader implements LoaderInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function load(ImageInterface $image, array $options = array())
18
    {
19
        if (!isset($options['max'])) {
20
            throw new \InvalidArgumentException('Missing max option.');
21
        }
22
23
        list($width, $height) = $options['max'];
24
25
        $size = $image->getSize();
26
        $origWidth = $size->getWidth();
27
        $origHeight = $size->getHeight();
28
29
        if ($origWidth > $width || $origHeight > $height) {
30
            $widthRatio = $width / $origWidth;
31
            $heightRatio = $height / $origHeight;
32
33
            // faster check than is_null
34 View Code Duplication
            if (null === $width || null === $height) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
                $ratio = max($widthRatio, $heightRatio);
36
            } else {
37
                $ratio = min($widthRatio, $heightRatio);
38
            }
39
40
            if ($ratio > 1) {
41
                return $image;
42
            }
43
44
            $filter = new Resize(new Box($origWidth * $ratio, $origHeight * $ratio));
45
46
            return $filter->apply($image);
47
        }
48
49
        return $image;
50
    }
51
}
52