1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rostenkowski\Resize\Requests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Nette\Utils\Image; |
7
|
|
|
use Rostenkowski\Resize\Entity\EmptyImage; |
8
|
|
|
use Rostenkowski\Resize\Meta; |
9
|
|
|
use Rostenkowski\Resize\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Image request encapsulation |
13
|
|
|
*/ |
14
|
|
|
class ImageRequest implements Request |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The requested image meta information. |
19
|
|
|
* |
20
|
|
|
* @var Meta |
21
|
|
|
*/ |
22
|
|
|
private $meta; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The requested image thumbnail dimensions |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $dimensions; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The requested image thumbnail flags. |
33
|
|
|
* |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
private $flags; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The requested image thumbnail cropping flag. |
40
|
|
|
* |
41
|
|
|
* @var boolean |
42
|
|
|
*/ |
43
|
|
|
private $crop = false; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructs the image request from the given meta information, requested dimensions and flags. |
48
|
|
|
* |
49
|
|
|
* @param Meta $meta |
50
|
|
|
* @param integer $dimensions |
51
|
|
|
* @param integer $flags |
52
|
|
|
* @param boolean $crop |
53
|
|
|
*/ |
54
|
|
|
public function __construct(Meta $meta, $dimensions = Request::ORIGINAL, $flags = Request::ORIGINAL, $crop = false) |
55
|
|
|
{ |
56
|
|
|
$this->meta = $meta; |
57
|
|
|
$this->dimensions = $dimensions; |
58
|
|
|
$this->flags = $flags; |
59
|
|
|
$this->crop = $crop; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Creates the image request from the crop macro arguments. |
65
|
|
|
* |
66
|
|
|
* @param Meta $image |
67
|
|
|
* @param array $args |
68
|
|
|
* @return ImageRequest |
69
|
|
|
*/ |
70
|
|
|
public static function crop(Meta $image = NULL, $args = array()) |
71
|
|
|
{ |
72
|
|
|
if ($image === NULL) { |
73
|
|
|
$image = new EmptyImage(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$dimensions = isset($args[0]) ? $args[0] : Request::ORIGINAL; |
77
|
|
|
$flags = Image::FIT; |
78
|
|
|
|
79
|
|
|
$request = new ImageRequest($image, $dimensions, $flags, true); |
80
|
|
|
|
81
|
|
|
return $request; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates the image request from the image macro arguments. |
87
|
|
|
* |
88
|
|
|
* @param Meta $image |
89
|
|
|
* @param array $args |
90
|
|
|
* @return ImageRequest |
91
|
|
|
*/ |
92
|
|
|
public static function fromMacro(Meta $image = NULL, $args = array()) |
93
|
|
|
{ |
94
|
|
|
if ($image === NULL) { |
95
|
|
|
$image = new EmptyImage(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new ImageRequest($image, isset($args[0]) ? $args[0] : Request::ORIGINAL, isset($args[1]) ? $args[1] : Request::ORIGINAL); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function getCrop() |
106
|
|
|
{ |
107
|
|
|
return $this->crop; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Meta |
113
|
|
|
*/ |
114
|
|
|
public function getMeta() |
115
|
|
|
{ |
116
|
|
|
return $this->meta; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Meta $meta |
122
|
|
|
* @return Request |
123
|
|
|
*/ |
124
|
|
|
public function setMeta(Meta $meta) |
125
|
|
|
{ |
126
|
|
|
$this->meta = $meta; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getDimensions() |
136
|
|
|
{ |
137
|
|
|
return $this->dimensions; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return integer |
143
|
|
|
*/ |
144
|
|
|
public function getFlags() |
145
|
|
|
{ |
146
|
|
|
return $this->flags; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: