|
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
|
|
|
* Scale filter. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Devi Prasad <https://github.com/deviprsd21> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ScaleFilterLoader implements LoaderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $dimensionKey; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $ratioKey; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $absoluteRatio; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($dimensionKey = 'dim', $ratioKey = 'to', $absoluteRatio = true) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->dimensionKey = $dimensionKey; |
|
34
|
|
|
$this->ratioKey = $ratioKey; |
|
35
|
|
|
$this->absoluteRatio = $absoluteRatio; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function load(ImageInterface $image, array $options = array()) |
|
42
|
|
|
{ |
|
43
|
|
|
if (!isset($options[$this->dimensionKey]) && !isset($options[$this->ratioKey])) { |
|
44
|
|
|
throw new \InvalidArgumentException("Missing $this->dimensionKey or $this->ratioKey option."); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$size = $image->getSize(); |
|
48
|
|
|
$origWidth = $size->getWidth(); |
|
49
|
|
|
$origHeight = $size->getHeight(); |
|
50
|
|
|
|
|
51
|
|
|
if (isset($options[$this->ratioKey])) { |
|
52
|
|
|
$ratio = $this->absoluteRatio ? $options[$this->ratioKey] : $this->calcAbsoluteRatio($options[$this->ratioKey]); |
|
53
|
|
|
} elseif (isset($options[$this->dimensionKey])) { |
|
54
|
|
|
$size = $options[$this->dimensionKey]; |
|
55
|
|
|
$width = isset($size[0]) ? $size[0] : null; |
|
56
|
|
|
$height = isset($size[1]) ? $size[1] : null; |
|
57
|
|
|
|
|
58
|
|
|
$widthRatio = $width / $origWidth; |
|
59
|
|
|
$heightRatio = $height / $origHeight; |
|
60
|
|
|
|
|
61
|
|
|
if (null == $width || null == $height) { |
|
62
|
|
|
$ratio = max($widthRatio, $heightRatio); |
|
63
|
|
|
} else { |
|
64
|
|
|
$ratio = min($widthRatio, $heightRatio); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($this->isImageProcessable($ratio)) { |
|
69
|
|
|
$filter = new Resize(new Box(round($origWidth * $ratio), round($origHeight * $ratio))); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $filter->apply($image); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $image; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function calcAbsoluteRatio($ratio) |
|
78
|
|
|
{ |
|
79
|
|
|
return $ratio; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function isImageProcessable($ratio) |
|
83
|
|
|
{ |
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: