1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Image\Effect; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
use \InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
use \Charcoal\Image\AbstractEffect; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Resize an image to given dimensions |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractCropEffect extends AbstractEffect |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var integer $x |
17
|
|
|
*/ |
18
|
|
|
private $x = 0; |
19
|
|
|
/** |
20
|
|
|
* @var integer $y |
21
|
|
|
*/ |
22
|
|
|
private $y = 0; |
23
|
|
|
/** |
24
|
|
|
* @var integer $width |
25
|
|
|
*/ |
26
|
|
|
private $width = 0; |
27
|
|
|
/** |
28
|
|
|
* @var integer $height |
29
|
|
|
*/ |
30
|
|
|
private $height = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $width |
34
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
* @return Rotate Chainable |
36
|
|
|
*/ |
37
|
|
|
public function setWidth($width) |
38
|
|
|
{ |
39
|
|
|
if (!is_int($width) || ($width < 0)) { |
40
|
|
|
throw new InvalidArgumentException( |
41
|
|
|
'Width must be a a positive integer' |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
$this->width = $width; |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return float |
50
|
|
|
*/ |
51
|
|
|
public function width() |
52
|
|
|
{ |
53
|
|
|
return $this->width; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param integer $height |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
* @return $this Chainable |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function setHeight($height) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if (!is_int($height) || ($height < 0)) { |
64
|
|
|
throw new InvalidArgumentException( |
65
|
|
|
'Height must be a positive integer' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
$this->height = $height; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return float |
74
|
|
|
*/ |
75
|
|
|
public function height() |
76
|
|
|
{ |
77
|
|
|
return $this->height; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The X coordinate of the cropped region's top left corner |
82
|
|
|
* |
83
|
|
|
* @param integer $x |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
* @return $this Chainable |
86
|
|
|
*/ |
87
|
|
|
public function setX($x) |
88
|
|
|
{ |
89
|
|
|
if (!is_int($x) || ($x < 0)) { |
90
|
|
|
throw new InvalidArgumentException( |
91
|
|
|
'Height must be a positive integer' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
$this->x = $x; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return float |
100
|
|
|
*/ |
101
|
|
|
public function x() |
102
|
|
|
{ |
103
|
|
|
return $this->x; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* The Y coordinate of the cropped region's top left corner |
108
|
|
|
* |
109
|
|
|
* @param integer $y |
110
|
|
|
* @throws InvalidArgumentException |
111
|
|
|
* @return $this Chainable |
112
|
|
|
*/ |
113
|
|
|
public function setY($y) |
114
|
|
|
{ |
115
|
|
|
if (!is_int($y) || ($y < 0)) { |
116
|
|
|
throw new InvalidArgumentException( |
117
|
|
|
'Height must be a positive integer' |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
$this->y = $y; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return float |
126
|
|
|
*/ |
127
|
|
|
public function y() |
128
|
|
|
{ |
129
|
|
|
return $this->y; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param array $data |
134
|
|
|
* @throws Exception |
135
|
|
|
* @return AbstractResizeEffect Chainable |
136
|
|
|
*/ |
137
|
|
|
public function process(array $data = null) |
138
|
|
|
{ |
139
|
|
|
if ($data !== null) { |
140
|
|
|
$this->setData($data); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$y = $this->y(); |
144
|
|
|
$x = $this->x(); |
145
|
|
|
$width = $this->width(); |
146
|
|
|
$height = $this->height(); |
147
|
|
|
|
148
|
|
|
$this->doCrop($width, $height, $x, $y); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param integer $width |
155
|
|
|
* @param integer $height |
156
|
|
|
* @param boolean $best_fit |
|
|
|
|
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
abstract protected function doCrop($width, $height, $x, $y); |
160
|
|
|
} |
161
|
|
|
|
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.