ScaleFilterLoader   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C load() 0 36 12
A calcAbsoluteRatio() 0 4 1
A isImageProcessable() 0 4 1
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\Resize;
15
use Imagine\Image\Box;
16
use Imagine\Image\ImageInterface;
17
18
/**
19
 * Scale filter.
20
 *
21
 * @author Devi Prasad <https://github.com/deviprsd21>
22
 */
23
class ScaleFilterLoader implements LoaderInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $dimensionKey;
29
30
    /**
31
     * @var string
32
     */
33
    protected $ratioKey;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $absoluteRatio;
39
40
    public function __construct($dimensionKey = 'dim', $ratioKey = 'to', $absoluteRatio = true)
41
    {
42
        $this->dimensionKey = $dimensionKey;
43
        $this->ratioKey = $ratioKey;
44
        $this->absoluteRatio = $absoluteRatio;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function load(ImageInterface $image, array $options = [])
51
    {
52
        if (!isset($options[$this->dimensionKey]) && !isset($options[$this->ratioKey])) {
53
            throw new \InvalidArgumentException("Missing $this->dimensionKey or $this->ratioKey option.");
54
        }
55
56
        $size = $image->getSize();
57
        $origWidth = $size->getWidth();
58
        $origHeight = $size->getHeight();
59
        $ratio = 1;
60
61
        if (isset($options[$this->ratioKey])) {
62
            $ratio = $this->absoluteRatio ? $options[$this->ratioKey] : $this->calcAbsoluteRatio($options[$this->ratioKey]);
63
        } elseif (isset($options[$this->dimensionKey])) {
64
            $size = $options[$this->dimensionKey];
65
            $width = isset($size[0]) ? $size[0] : null;
66
            $height = isset($size[1]) ? $size[1] : null;
67
68
            $widthRatio = $width / $origWidth;
69
            $heightRatio = $height / $origHeight;
70
71
            if (null === $width || null === $height) {
72
                $ratio = max($widthRatio, $heightRatio);
73
            } else {
74
                $ratio = ('min' === $this->dimensionKey) ? max($widthRatio, $heightRatio) : min($widthRatio, $heightRatio);
75
            }
76
        }
77
78
        if ($this->isImageProcessable($ratio)) {
79
            $filter = new Resize(new Box(round($origWidth * $ratio), round($origHeight * $ratio)));
80
81
            return $filter->apply($image);
82
        }
83
84
        return $image;
85
    }
86
87
    protected function calcAbsoluteRatio($ratio)
88
    {
89
        return $ratio;
90
    }
91
92
    protected function isImageProcessable($ratio)
93
    {
94
        return true;
95
    }
96
}
97