Completed
Push — master ( f9114f...683fbb )
by Chauncey
03:31
created

AbstractCropEffect::repage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    /**
21
     * @var integer $y
22
     */
23
    private $y = 0;
24
25
    /**
26
     * @var integer $width
27
     */
28
    private $width = 0;
29
30
    /**
31
     * @var integer $height
32
     */
33
    private $height = 0;
34
35
    /**
36
     * @var mixed $geometry
37
     */
38
    private $geometry;
39
40
    /**
41
     * @var string $gravity
42
     */
43
    private $gravity = 'center';
44
45
    /**
46
     * @var boolean $repage
47
     */
48
    private $repage = false;
49
50
    /**
51
     * @param  integer $width The crop width.
52
     * @throws InvalidArgumentException If the width argument is not valid.
53
     * @return AbstractCropEffect
54
     */
55
    public function setWidth($width)
56
    {
57
        if (!is_int($width) || ($width < 0)) {
58
            throw new InvalidArgumentException(
59
                'Width must be a a positive integer'
60
            );
61
        }
62
        $this->width = $width;
63
        return $this;
64
    }
65
66
    /**
67
     * @return integer
68
     */
69
    public function width()
70
    {
71
        return $this->width;
72
    }
73
74
    /**
75
     * @param  integer $height The crop height.
76
     * @throws InvalidArgumentException If the height argument is not valid.
77
     * @return AbstractCropEffect
78
     */
79 View Code Duplication
    public function setHeight($height)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
80
    {
81
        if (!is_int($height) || ($height < 0)) {
82
            throw new InvalidArgumentException(
83
                'Height must be a positive integer'
84
            );
85
        }
86
        $this->height = $height;
87
        return $this;
88
    }
89
90
    /**
91
     * @return integer
92
     */
93
    public function height()
94
    {
95
        return $this->height;
96
    }
97
98
    /**
99
     * The X coordinate of the cropped region's top left corner
100
     *
101
     * @param  integer $x The x-position (in pixel) of the crop.
102
     * @throws InvalidArgumentException If the x argument is not valid.
103
     * @return AbstractCropEffect
104
     */
105
    public function setX($x)
106
    {
107
        if (!is_int($x) || ($x < 0)) {
108
            throw new InvalidArgumentException(
109
                'Height must be a positive integer'
110
            );
111
        }
112
        $this->x = $x;
113
        return $this;
114
    }
115
116
    /**
117
     * @return integer
118
     */
119
    public function x()
120
    {
121
        return $this->x;
122
    }
123
124
    /**
125
     * The Y coordinate of the cropped region's top left corner
126
     *
127
     * @param  integer $y The y-position (in pixel) of the crop.
128
     * @throws InvalidArgumentException If the y argumnet is not valid.
129
     * @return AbstractCropEffect
130
     */
131
    public function setY($y)
132
    {
133
        if (!is_int($y) || ($y < 0)) {
134
            throw new InvalidArgumentException(
135
                'Height must be a positive integer'
136
            );
137
        }
138
        $this->y = $y;
139
        return $this;
140
    }
141
142
    /**
143
     * @return integer
144
     */
145
    public function y()
146
    {
147
        return $this->y;
148
    }
149
150
    /**
151
     * Set a complex geometry value.
152
     *
153
     * @param  mixed $geometry The image geometry.
154
     * @throws InvalidArgumentException If the geometry argument is not valid.
155
     * @return AbstractCropEffect
156
     */
157 View Code Duplication
    public function setGeometry($geometry)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
158
    {
159
        if ($geometry !== null && !is_string($geometry) && !is_numeric($geometry)) {
160
            throw new InvalidArgumentException(
161
                'Geometry must be a valid crop'
162
            );
163
        }
164
        $this->geometry = $geometry;
165
        return $this;
166
    }
167
168
    /**
169
     * Retrieve the complex geometry value.
170
     *
171
     * @return mixed
172
     */
173
    public function geometry()
174
    {
175
        return $this->geometry;
176
    }
177
178
    /**
179
     * @param  string $gravity The crop gravity.
180
     * @throws InvalidArgumentException If the argument is not a valid gravity name.
181
     * @return AbstractCropEffect
182
     */
183 View Code Duplication
    public function setGravity($gravity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
184
    {
185
        if (!in_array($gravity, $this->image()->availableGravities())) {
186
            throw new InvalidArgumentException(
187
                'Gravity is not valid'
188
            );
189
        }
190
        $this->gravity = $gravity;
191
        return $this;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function gravity()
198
    {
199
        return $this->gravity;
200
    }
201
202
    /**
203
     * @param  boolean $repage The repage image flag.
204
     * @return AbstractCropEffect
205
     */
206
    public function setRepage($repage)
207
    {
208
        $this->repage = !!$repage;
209
        return $this;
210
    }
211
212
    /**
213
     * @return boolean
214
     */
215
    public function repage()
216
    {
217
        return $this->repage;
218
    }
219
220
    /**
221
     * @param  array $data The effect data.
222
     * @return AbstractCropEffect
223
     */
224
    public function process(array $data = null)
225
    {
226
        if ($data !== null) {
227
            $this->setData($data);
228
        }
229
230
        if ($this->geometry()) {
231
            $this->doCrop(0, 0, 0, 0);
232
            return $this;
233
        }
234
235
        $y = $this->y();
236
        $x = $this->x();
237
        $width  = $this->width();
238
        $height = $this->height();
239
240
        $this->doCrop($width, $height, $x, $y);
241
242
        return $this;
243
    }
244
245
    /**
246
     * @param  integer $width  The crop width.
247
     * @param  integer $height The crop height.
248
     * @param  integer $x      The x-position (in pixel) of the crop.
249
     * @param  integer $y      The y-position (in pixel) of the crop.
250
     * @return void
251
     */
252
    abstract protected function doCrop($width, $height, $x, $y);
253
}
254