Completed
Push — master ( 261a19...1c518e )
by Iurii
01:23
created

Action   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 317
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A autoOrient() 0 6 1
A bestFit() 0 7 1
A flip() 0 7 1
A overlay() 0 7 1
A rotate() 0 7 1
A border() 0 7 1
A fill() 0 7 1
A blur() 0 7 1
A brighten() 0 7 1
A colorize() 0 7 1
A contrast() 0 7 1
A darken() 0 7 1
A desaturate() 0 6 1
A duotone() 0 7 1
A edgeDetect() 0 6 1
A emboss() 0 6 1
A invert() 0 6 1
A opacity() 0 7 1
A pixelate() 0 7 1
A sepia() 0 6 1
A sharpen() 0 6 1
A sketch() 0 6 1
1
<?php
2
3
/**
4
 * @package Image
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\image\handlers;
11
12
use claviska\SimpleImage;
13
use gplcart\core\Library;
14
use LogicException;
15
16
/**
17
 * Image action methods for Image module
18
 */
19
class Action
20
{
21
22
    /**
23
     * SimpleImage class instance
24
     * @var \claviska\SimpleImage $lib
25
     */
26
    protected $image;
27
28
    /**
29
     * @param Library $library
30
     * @throws LogicException
31
     */
32
    public function __construct(Library $library)
33
    {
34
        $library->load('simpleimage');
35
36
        if (!class_exists('claviska\\SimpleImage')) {
37
            throw new LogicException('Class claviska\\SimpleImage does not exist');
38
        }
39
40
        $this->image = new SimpleImage;
41
    }
42
43
    /**
44
     * @param string $source
45
     * @param string $target
46
     * @return bool
47
     */
48
    public function autoOrient(&$source, $target)
49
    {
50
        $this->image->fromFile($source)->autoOrient()->toFile($target);
51
        $source = $target;
52
        return true;
53
    }
54
55
    /**
56
     * @param string $source
57
     * @param string $target
58
     * @param array $action
59
     * @return bool
60
     */
61
    public function bestFit(&$source, $target, array $action)
62
    {
63
        list($width, $height) = $action['value'];
64
        $this->image->fromFile($source)->bestFit($width, $height)->toFile($target);
65
        $source = $target;
66
        return true;
67
    }
68
69
    /**
70
     * @param string $source
71
     * @param string $target
72
     * @param array $action
73
     * @return bool
74
     */
75
    public function flip(&$source, $target, array $action)
76
    {
77
        $direction = reset($action['value']);
78
        $this->image->fromFile($source)->flip($direction)->toFile($target);
79
        $source = $target;
80
        return true;
81
    }
82
83
    /**
84
     * @param string $source
85
     * @param string $target
86
     * @param array $action
87
     * @return bool
88
     */
89
    public function overlay(&$source, $target, array $action)
90
    {
91
        list($overlay, $anchor, $opacity, $xOffset, $yOffset) = $action['value'];
92
        $this->image->fromFile($source)->overlay($overlay, $anchor, $opacity, $xOffset, $yOffset)->toFile($target);
93
        $source = $target;
94
        return true;
95
    }
96
97
    /**
98
     * @param string $source
99
     * @param string $target
100
     * @param array $action
101
     * @return bool
102
     */
103
    public function rotate(&$source, $target, array $action)
104
    {
105
        list($angle, $backgroundColor) = $action['value'];
106
        $this->image->fromFile($source)->rotate($angle, $backgroundColor)->toFile($target);
107
        $source = $target;
108
        return true;
109
    }
110
111
    /**
112
     * @param string $source
113
     * @param string $target
114
     * @param array $action
115
     * @return bool
116
     */
117
    public function border(&$source, $target, array $action)
118
    {
119
        list($color, $thickness) = $action['value'];
120
        $this->image->fromFile($source)->border($color, $thickness)->toFile($target);
121
        $source = $target;
122
        return true;
123
    }
124
125
    /**
126
     * @param string $source
127
     * @param string $target
128
     * @param array $action
129
     * @return bool
130
     */
131
    public function fill(&$source, $target, array $action)
132
    {
133
        list($color) = $action['value'];
134
        $this->image->fromFile($source)->fill($color)->toFile($target);
135
        $source = $target;
136
        return true;
137
    }
138
139
    /**
140
     * @param string $source
141
     * @param string $target
142
     * @param array $action
143
     * @return bool
144
     */
145
    public function blur(&$source, $target, array $action)
146
    {
147
        list($type, $passes) = $action['value'];
148
        $this->image->fromFile($source)->blur($type, $passes)->toFile($target);
149
        $source = $target;
150
        return true;
151
    }
152
153
    /**
154
     * @param string $source
155
     * @param string $target
156
     * @param array $action
157
     * @return bool
158
     */
159
    public function brighten(&$source, $target, array $action)
160
    {
161
        list($percentage) = $action['value'];
162
        $this->image->fromFile($source)->brighten($percentage)->toFile($target);
163
        $source = $target;
164
        return true;
165
    }
166
167
    /**
168
     * @param string $source
169
     * @param string $target
170
     * @param array $action
171
     * @return bool
172
     */
173
    public function colorize(&$source, $target, array $action)
174
    {
175
        list($color) = $action['value'];
176
        $this->image->fromFile($source)->colorize($color)->toFile($target);
177
        $source = $target;
178
        return true;
179
    }
180
181
    /**
182
     * @param string $source
183
     * @param string $target
184
     * @param array $action
185
     * @return bool
186
     */
187
    public function contrast(&$source, $target, array $action)
188
    {
189
        list($percentage) = $action['value'];
190
        $this->image->fromFile($source)->contrast($percentage)->toFile($target);
191
        $source = $target;
192
        return true;
193
    }
194
195
    /**
196
     * @param string $source
197
     * @param string $target
198
     * @param array $action
199
     * @return bool
200
     */
201
    public function darken(&$source, $target, array $action)
202
    {
203
        list($percentage) = $action['value'];
204
        $this->image->fromFile($source)->darken($percentage)->toFile($target);
205
        $source = $target;
206
        return true;
207
    }
208
209
    /**
210
     * @param string $source
211
     * @param string $target
212
     * @return bool
213
     */
214
    public function desaturate(&$source, $target)
215
    {
216
        $this->image->fromFile($source)->desaturate()->toFile($target);
217
        $source = $target;
218
        return true;
219
    }
220
221
    /**
222
     * @param string $source
223
     * @param string $target
224
     * @param array $action
225
     * @return bool
226
     */
227
    public function duotone(&$source, $target, array $action)
228
    {
229
        list($lightColor, $darkColor) = $action['value'];
230
        $this->image->fromFile($source)->duotone($lightColor, $darkColor)->toFile($target);
231
        $source = $target;
232
        return true;
233
    }
234
235
    /**
236
     * @param string $source
237
     * @param string $target
238
     * @return bool
239
     */
240
    public function edgeDetect(&$source, $target)
241
    {
242
        $this->image->fromFile($source)->edgeDetect()->toFile($target);
243
        $source = $target;
244
        return true;
245
    }
246
247
    /**
248
     * @param string $source
249
     * @param string $target
250
     * @return bool
251
     */
252
    public function emboss(&$source, $target)
253
    {
254
        $this->image->fromFile($source)->emboss()->toFile($target);
255
        $source = $target;
256
        return true;
257
    }
258
259
    /**
260
     * @param string $source
261
     * @param string $target
262
     * @return bool
263
     */
264
    public function invert(&$source, $target)
265
    {
266
        $this->image->fromFile($source)->invert()->toFile($target);
267
        $source = $target;
268
        return true;
269
    }
270
271
    /**
272
     * @param string $source
273
     * @param string $target
274
     * @param array $action
275
     * @return bool
276
     */
277
    public function opacity(&$source, $target, array $action)
278
    {
279
        list($opacity) = $action['value'];
280
        $this->image->fromFile($source)->opacity($opacity)->toFile($target);
281
        $source = $target;
282
        return true;
283
    }
284
285
    /**
286
     * @param string $source
287
     * @param string $target
288
     * @param array $action
289
     * @return bool
290
     */
291
    public function pixelate(&$source, $target, array $action)
292
    {
293
        list($size) = $action['value'];
294
        $this->image->fromFile($source)->pixelate($size)->toFile($target);
295
        $source = $target;
296
        return true;
297
    }
298
299
    /**
300
     * @param string $source
301
     * @param string $target
302
     * @return bool
303
     */
304
    public function sepia(&$source, $target)
305
    {
306
        $this->image->fromFile($source)->sepia()->toFile($target);
307
        $source = $target;
308
        return true;
309
    }
310
311
    /**
312
     * @param string $source
313
     * @param string $target
314
     * @return bool
315
     */
316
    public function sharpen(&$source, $target)
317
    {
318
        $this->image->fromFile($source)->sharpen()->toFile($target);
319
        $source = $target;
320
        return true;
321
    }
322
323
    /**
324
     * @param string $source
325
     * @param string $target
326
     * @return bool
327
     */
328
    public function sketch(&$source, $target)
329
    {
330
        $this->image->fromFile($source)->sketch()->toFile($target);
331
        $source = $target;
332
        return true;
333
    }
334
335
}
336