Grafika.php
13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
namespace Grafika;
use Grafika\Gd\DrawingObject\CubicBezier as GdCubicBezier;
use Grafika\Gd\DrawingObject\Ellipse as GdEllipse;
use Grafika\Gd\DrawingObject\Line as GdLine;
use Grafika\Gd\DrawingObject\Polygon as GdPolygon;
use Grafika\Gd\DrawingObject\QuadraticBezier as GdQuadraticBezier;
use Grafika\Gd\DrawingObject\Rectangle as GdRectangle;
use Grafika\Gd\Editor as GdEditor;
use Grafika\Gd\Filter\Dither as GdDither;
use Grafika\Gd\Filter\Blur as GdBlur;
use Grafika\Gd\Filter\Brightness as GdBrightness;
use Grafika\Gd\Filter\Colorize as GdColorize;
use Grafika\Gd\Filter\Contrast as GdContrast;
use Grafika\Gd\Filter\Gamma as GdGamma;
use Grafika\Gd\Filter\Grayscale as GdGrayscale;
use Grafika\Gd\Filter\Invert as GdInvert;
use Grafika\Gd\Filter\Pixelate as GdPixelate;
use Grafika\Gd\Filter\Sharpen as GdSharpen;
use Grafika\Gd\Filter\Sobel as GdSobel;
use Grafika\Gd\Image as GdImage;
use Grafika\Imagick\DrawingObject\CubicBezier as ImagickCubicBezier;
use Grafika\Imagick\DrawingObject\Ellipse as ImagickEllipse;
use Grafika\Imagick\DrawingObject\Line as ImagickLine;
use Grafika\Imagick\DrawingObject\Polygon as ImagickPolygon;
use Grafika\Imagick\DrawingObject\QuadraticBezier as ImagickQuadraticBezier;
use Grafika\Imagick\DrawingObject\Rectangle as ImagickRectangle;
use Grafika\Imagick\Editor as ImagickEditor;
use Grafika\Imagick\Filter\Blur as ImagickBlur;
use Grafika\Imagick\Filter\Brightness as ImagickBrightness;
use Grafika\Imagick\Filter\Colorize as ImagickColorize;
use Grafika\Imagick\Filter\Contrast as ImagickContrast;
use Grafika\Imagick\Filter\Gamma as ImagickGamma;
use Grafika\Imagick\Filter\Dither as ImagickDither;
use Grafika\Imagick\Filter\Grayscale as ImagickGrayscale;
use Grafika\Imagick\Filter\Invert as ImagickInvert;
use Grafika\Imagick\Filter\Pixelate as ImagickPixelate;
use Grafika\Imagick\Filter\Sharpen as ImagickSharpen;
use Grafika\Imagick\Filter\Sobel as ImagickSobel;
use Grafika\Imagick\Image as ImagickImage;
/**
* Contains factory methods for detecting editors, creating editors and images.
* @package Grafika
*/
class Grafika
{
/**
* Grafika root directory
*/
const DIR = __DIR__;
/**
* @var array $editorList List of editors to evaluate.
*/
private static $editorList = array('Imagick', 'Gd');
/**
* Return path to directory containing fonts used in text operations.
*
* @return string
*/
public static function fontsDir()
{
$ds = DIRECTORY_SEPARATOR;
return realpath(self::DIR . $ds . '..' . $ds . '..') . $ds . 'fonts';
}
/**
* Change the editor list order of evaluation globally.
*
* @param array $editorList
*
* @throws \Exception
*/
public static function setEditorList($editorList){
if(!is_array($editorList)){
throw new \Exception('$editorList must be an array.');
}
self::$editorList = $editorList;
}
/**
* Detects and return the name of the first supported editor which can either be "Imagick" or "Gd".
*
* @param array $editorList Array of editor list names. Use this to change the order of evaluation for editors for this function call only. Default order of evaluation is Imagick then GD.
*
* @return string Name of available editor.
* @throws \Exception Throws exception if there are no supported editors.
*/
public static function detectAvailableEditor($editorList = null)
{
if(null === $editorList){
$editorList = self::$editorList;
}
/* Get first supported editor instance. Order of editorList matter. */
foreach ($editorList as $editorName) {
if ('Imagick' === $editorName) {
$editorInstance = new ImagickEditor();
} else {
$editorInstance = new GdEditor();
}
/** @var EditorInterface $editorInstance */
if (true === $editorInstance->isAvailable()) {
return $editorName;
}
}
throw new \Exception('No supported editor.');
}
/**
* Creates the first available editor.
*
* @param array $editorList Array of editor list names. Use this to change the order of evaluation for editors. Default order of evaluation is Imagick then GD.
*
* @return EditorInterface
* @throws \Exception
*/
public static function createEditor($editorList = array('Imagick', 'Gd'))
{
$editorName = self::detectAvailableEditor($editorList);
if ('Imagick' === $editorName) {
return new ImagickEditor();
} else {
return new GdEditor();
}
}
/**
* Create an image.
* @param string $imageFile Path to image file.
*
* @return ImageInterface
* @throws \Exception
*/
public static function createImage($imageFile)
{
$editorName = self::detectAvailableEditor();
if ('Imagick' === $editorName) {
return ImagickImage::createFromFile($imageFile);
} else {
return GdImage::createFromFile($imageFile);
}
}
/**
* Create a blank image.
*
* @param int $width Width of image in pixels.
* @param int $height Height of image in pixels.
*
* @return ImageInterface
* @throws \Exception
*/
public static function createBlankImage($width = 1, $height = 1)
{
$editorName = self::detectAvailableEditor();
if ('Imagick' === $editorName) {
return ImagickImage::createBlank($width, $height);
} else {
return GdImage::createBlank($width, $height);
}
}
/**
* Create a filter. Detects available editor to use.
*
* @param string $filterName The name of the filter.
*
* @return FilterInterface
* @throws \Exception
*/
public static function createFilter($filterName)
{
$editorName = self::detectAvailableEditor();
$p = func_get_args();
if ('Imagick' === $editorName) {
switch ($filterName){
case 'Blur':
return new ImagickBlur(
(array_key_exists(1,$p) ? $p[1] : 1)
);
case 'Brightness':
return new ImagickBrightness(
$p[1]
);
case 'Colorize':
return new ImagickColorize(
$p[1], $p[2], $p[3]
);
case 'Contrast':
return new ImagickContrast(
$p[1]
);
case 'Dither':
return new ImagickDither(
$p[1]
);
case 'Gamma':
return new ImagickGamma(
$p[1]
);
case 'Grayscale':
return new ImagickGrayscale();
case 'Invert':
return new ImagickInvert();
case 'Pixelate':
return new ImagickPixelate(
$p[1]
);
case 'Sharpen':
return new ImagickSharpen(
$p[1]
);
case 'Sobel':
return new ImagickSobel();
}
throw new \Exception('Invalid filter name.');
} else {
switch ($filterName){
case 'Blur':
return new GdBlur(
(array_key_exists(1,$p) ? $p[1] : 1)
);
case 'Brightness':
return new GdBrightness(
$p[1]
);
case 'Colorize':
return new GdColorize(
$p[1], $p[2], $p[3]
);
case 'Contrast':
return new GdContrast(
$p[1]
);
case 'Dither':
return new GdDither(
$p[1]
);
case 'Gamma':
return new GdGamma(
$p[1]
);
case 'Grayscale':
return new GdGrayscale();
case 'Invert':
return new GdInvert();
case 'Pixelate':
return new GdPixelate(
$p[1]
);
case 'Sharpen':
return new GdSharpen(
$p[1]
);
case 'Sobel':
return new GdSobel();
}
throw new \Exception('Invalid filter name.');
}
}
/**
* Draws an object. Detects available editor to use.
*
* @param string $drawingObjectName The name of the DrawingObject.
*
* @return DrawingObjectInterface
* @throws \Exception
*
* We use array_key_exist() instead of isset() to be able to detect a parameter with a NULL value.
*/
public static function createDrawingObject($drawingObjectName)
{
$editorName = self::detectAvailableEditor();
$p = func_get_args();
if ('Imagick' === $editorName) {
switch ($drawingObjectName){
case 'CubicBezier':
return new ImagickCubicBezier(
$p[1],
$p[2],
$p[3],
$p[4],
(array_key_exists(5,$p) ? $p[5] : '#000000')
);
case 'Ellipse':
return new ImagickEllipse(
$p[1],
$p[2],
(array_key_exists(3,$p) ? $p[3] : array(0,0)),
(array_key_exists(4,$p) ? $p[4] : 1),
(array_key_exists(5,$p) ? $p[5] : '#000000'),
(array_key_exists(6,$p) ? $p[6] : '#FFFFFF')
);
case 'Line':
return new ImagickLine(
$p[1],
$p[2],
(array_key_exists(3,$p) ? $p[3] : 1),
(array_key_exists(4,$p) ? $p[4] : '#000000')
);
case 'Polygon':
return new ImagickPolygon(
$p[1],
(array_key_exists(2,$p) ? $p[2] : 1),
(array_key_exists(3,$p) ? $p[3] : '#000000'),
(array_key_exists(4,$p) ? $p[4] : '#FFFFFF')
);
case 'Rectangle':
return new ImagickRectangle(
$p[1],
$p[2],
(array_key_exists(3,$p) ? $p[3] : array(0,0)),
(array_key_exists(4,$p) ? $p[4] : 1),
(array_key_exists(5,$p) ? $p[5] : '#000000'),
(array_key_exists(6,$p) ? $p[6] : '#FFFFFF')
);
case 'QuadraticBezier':
return new ImagickQuadraticBezier(
$p[1],
$p[2],
$p[3],
(array_key_exists(4,$p) ? $p[4] : '#000000')
);
}
throw new \Exception('Invalid drawing object name.');
} else {
switch ($drawingObjectName) {
case 'CubicBezier':
return new GdCubicBezier(
$p[1],
$p[2],
$p[3],
$p[4],
(array_key_exists(5,$p) ? $p[5] : '#000000')
);
case 'Ellipse':
return new GdEllipse(
$p[1],
$p[2],
(array_key_exists(3,$p) ? $p[3] : array(0,0)),
(array_key_exists(4,$p) ? $p[4] : 1),
(array_key_exists(5,$p) ? $p[5] : '#000000'),
(array_key_exists(6,$p) ? $p[6] : '#FFFFFF')
);
case 'Line':
return new GdLine(
$p[1],
$p[2],
(array_key_exists(3,$p) ? $p[3] : 1),
(array_key_exists(4,$p) ? $p[4] : '#000000')
);
case 'Polygon':
return new GdPolygon(
$p[1],
(array_key_exists(2,$p) ? $p[2] : 1),
(array_key_exists(3,$p) ? $p[3] : '#000000'),
(array_key_exists(4,$p) ? $p[4] : '#FFFFFF')
);
case 'Rectangle':
return new GdRectangle(
$p[1],
$p[2],
(array_key_exists(3,$p) ? $p[3] : array(0,0)),
(array_key_exists(4,$p) ? $p[4] : 1),
(array_key_exists(5,$p) ? $p[5] : '#000000'),
(array_key_exists(6,$p) ? $p[6] : '#FFFFFF')
);
case 'QuadraticBezier':
return new GdQuadraticBezier(
$p[1],
$p[2],
$p[3],
(array_key_exists(4,$p) ? $p[4] : '#000000')
);
}
throw new \Exception('Invalid drawing object name.');
}
}
}