forked from backdrop-contrib/graphicsmagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicsmagick.module
1782 lines (1625 loc) · 52.2 KB
/
graphicsmagick.module
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
g<?php
/**
* @file
* Hooks implementations for the GraphicsMagick module.
*/
use graphicsmagick\Utility\Color;
/**
* Implements hook_autoload_info().
*/
function graphicsmagick_autoload_info(): array {
return array(
'graphicsmagick\Utility\Color' => 'includes/color.class.inc',
);
}
/**
* Implements hook_image_effect_info().
*/
function graphicsmagick_image_effect_info(): array {
$effects = array();
$effects['graphicsmagick_effects_blur'] = array(
'label' => t('Blur'),
'help' => t('This effect will make the image details less distinct using a Gaussian operator.'),
'dimensions passthrough' => TRUE,
'form callback' => 'graphicsmagick_effects_blur_form',
'effect callback' => 'graphicsmagick_effects_blur',
'summary theme' => 'graphicsmagick_effects_blur_summary',
);
$effects['graphicsmagick_effects_despeckle'] = array(
'label' => t('Despeckle'),
'help' => t('This effect will reduce the speckle noise in an image while preserving the original image edges.'),
'dimensions passthrough' => TRUE,
'effect callback' => 'graphicsmagick_effects_despeckle',
);
$effects['graphicsmagick_effects_edge'] = array(
'label' => t('Edge enhance'),
'help' => t('This effect will enhance edges within the image with a convolution filter.'),
'dimensions passthrough' => TRUE,
'form callback' => 'graphicsmagick_effects_edge_form',
'effect callback' => 'graphicsmagick_effects_edge',
'summary theme' => 'graphicsmagick_effects_oil_paint_summary',
);
$effects['graphicsmagick_effects_emboss'] = array(
'label' => t('Emboss'),
'help' => t('This effect will convolve the image using a Gaussian operator, giving the image a three-dimensional effect.'),
'dimensions passthrough' => TRUE,
'form callback' => 'graphicsmagick_effects_emboss_form',
'effect callback' => 'graphicsmagick_effects_emboss',
'summary theme' => 'graphicsmagick_effects_blur_summary',
);
$effects['graphicsmagick_effects_enhance'] = array(
'label' => t('Enhance'),
'help' => t('This effect will improve the quality of a noisy image.'),
'dimensions passthrough' => TRUE,
'effect callback' => 'graphicsmagick_effects_enhance',
);
$effects['graphicsmagick_effects_equalize'] = array(
'label' => t('Equalize'),
'help' => t('This effect will equalize the image histogram.'),
'dimensions passthrough' => TRUE,
'effect callback' => 'graphicsmagick_effects_equalize',
);
$effects['graphicsmagick_effects_flip'] = array(
'label' => t('Vertical mirror'),
'help' => t('This effect will create a vertical mirror image.'),
'dimensions passthrough' => TRUE,
'effect callback' => 'graphicsmagick_effects_flip',
);
$effects['graphicsmagick_effects_flop'] = array(
'help' => t('This effect will create a horizontal mirror image.'),
'dimensions passthrough' => TRUE,
'effect callback' => 'graphicsmagick_effects_flop',
);
$effects['graphicsmagick_effects_gamma'] = array(
'label' => t('Gamma'),
'help' => t("This effect will change the gamma values."),
'dimensions passthrough' => TRUE,
'form callback' => 'graphicsmagick_effects_gamma_form',
'effect callback' => 'graphicsmagick_effects_gamma',
'summary theme' => 'graphicsmagick_effects_gamma_summary',
);
$effects['graphicsmagick_effects_noise'] = array(
'label' => t('Add noise'),
'help' => t("This effect will add random noise to the image."),
'dimensions passthrough' => TRUE,
'form callback' => 'graphicsmagick_effects_noise_form',
'effect callback' => 'graphicsmagick_effects_noise',
'summary theme' => 'graphicsmagick_effects_noise_summary',
);
$effects['graphicsmagick_effects_oil_paint'] = array(
'label' => t('Oil paint'),
'help' => t('This effect will simulate an oil painting.'),
'dimensions passthrough' => TRUE,
'form callback' => 'graphicsmagick_effects_oil_paint_form',
'effect callback' => 'graphicsmagick_effects_oil_paint',
'summary theme' => 'graphicsmagick_effects_oil_paint_summary',
);
$effects['graphicsmagick_effects_solarize'] = array(
'label' => t('Solarize'),
'help' => t('Solarize will apply an effect to the image, similar to the one achieved in a photo darkroom by selectively exposing areas of photo sensitive paper to light.'),
'dimensions passthrough' => TRUE,
'form callback' => 'graphicsmagick_effects_solarize_form',
'effect callback' => 'graphicsmagick_effects_solarize',
'summary theme' => 'graphicsmagick_effects_solarize_summary',
);
$effects['graphicsmagick_effects_spread'] = array(
'label' => t('Spread'),
'help' => t('This effect will randomly displace each pixel in a block defined by the radius.'),
'form callback' => 'graphicsmagick_effects_spread_form',
'effect callback' => 'graphicsmagick_effects_spread',
'summary theme' => 'graphicsmagick_effects_oil_paint_summary',
);
$effects['graphicsmagick_effects_swirl'] = array(
'label' => t('Swirl'),
'help' => t('This effect will swirl the pixels about the center of the image.'),
'form callback' => 'graphicsmagick_effects_swirl_form',
'effect callback' => 'graphicsmagick_effects_swirl',
'summary theme' => 'graphicsmagick_effects_swirl_summary',
);
return $effects;
}
/**
* Implements hook_image_toolkits().
*/
function graphicsmagick_image_toolkits(): array {
return array(
'graphicsmagick_toolkit' => array(
'title' => t('GraphicsMagick toolkit'),
'available' => graphicsmagick_available(),
),
);
}
/**
* Implements hook_theme().
*/
function graphicsmagick_theme(): array {
$summary_base = array(
'variables' => array( 'data' => NULL),
'file' => 'includes/graphicsmagick.theme.inc',
);
return array(
'graphicsmagick_effects_blur_summary' => $summary_base,
'graphicsmagick_effects_edge_summary' => $summary_base,
'graphicsmagick_effects_emboss_summary' => $summary_base,
'graphicsmagick_effects_gamma_summary' => $summary_base,
'graphicsmagick_effects_noise_summary' => $summary_base,
'graphicsmagick_effects_oil_paint_summary' => $summary_base,
'graphicsmagick_effects_solarize_summary' => $summary_base,
'graphicsmagick_effects_spread_summary' => $summary_base,
'graphicsmagick_effects_swirl_summary' => $summary_base,
);
}
/**
* Retrieves settings for the GraphicsMagick toolkit.
*
* @return array
* The form array.
*/
function image_graphicsmagick_toolkit_settings(): array {
$form['graphicsmagick_toolkit_jpeg_quality'] = array(
'#type' => 'number',
'#title' => t('JPEG quality'),
'#description' => t('The image quality for JPEG images (from 0 to 100). Higher values mean better image quality but bigger files.'),
'#default_value' => graphicsmagick_toolkit_get_jpeg_quality(),
'#min' => 0,
'#max' => 100,
'#step' => 1,
);
$form['graphicsmagick_toolkit_png_compression'] = array(
'#type' => 'number',
'#title' => t('PNG compression'),
'#description' => t('The PNG compression (from 0 to 99). Higher values do not mean better compression, although 99 usually compresses a PNG image more than other values. Verify the file size once the image has been saved.'),
'#default_value' => graphicsmagick_toolkit_get_png_compression(),
'#min' => 0,
'#max' => 100,
'#step' => 1,
);
return $form;
}
/**
* Retrieves the supported extensions for the GraphicsMagick toolkit.
*
* @return array
* The supported extensions.
*/
function image_graphicsmagick_toolkit_supported_extensions(): array {
// The following list doesn't include all the file types recognized by the
// GraphicsMagick library; it includes most of the image types, excluding
// the raw images and those images the Gmagick class is not able to write.
$extensions = array(
'BMP' => 'bmp',
'CIN' => 'cin',
'DCX' => 'dcx',
'DPX' => 'dpx',
'EPI' => 'epi',
'EPSF' => 'eps',
'EPSI' => 'eps',
'EPT' => 'eps',
'EPT2' => 'eps',
'EPT3' => 'eps',
'EPS' => 'eps',
'FITS' => 'fits',
'GIF87' => 'gif',
'GIF' => 'gif',
'ICB' => 'icb',
'JPEG' => 'jpeg',
'JPG' => 'jpg',
'PAM' => 'pam',
'PBM' => 'pbm',
'EPDF' => 'pdf',
'PDF' => 'pdf',
'PGM' => 'pgm',
'PNG00' => 'png',
'PNG24' => 'png',
'PNG32' => 'png',
'PNG48' => 'png',
'PNG' => 'png',
'PNM' => 'pnm',
'PPM' => 'ppm',
'PS' => 'ps',
'SGI' => 'sgi',
'TGA' => 'tga',
'PTIF' => 'tif',
'BIGTIFF' => 'tif',
'TIFF' => 'tiff',
'VDA' => 'vda',
'VST' => 'vst',
'WBMP' => 'wbmp',
'WEBP' => 'webp',
'XBM' => 'xbm',
'PICON' => 'xpm',
);
$supported_extensions = array();
try {
$handler = new Gmagick();
foreach ($handler->queryFormats() as $format) {
if (isset($extensions[$format])) {
$supported_extensions[] = $extensions[$format];
}
}
}
catch (GmagickException) {
// Ignore the thrown exception.
}
// The returned extensions exclude raw images.
return array_unique($supported_extensions);
}
/**
* Loads an image from a file.
*
* @param object $image
* An image object. The $image->handler and $image->format values will be
* populated by this call.
*
* @return bool
* TRUE or FALSE, based on success.
*
* @see image_load()
* @see graphicsmagick_image_load()
*/
function image_graphicsmagick_toolkit_load(object $image): bool {
return graphicsmagick_image_load($image);
}
/**
* Writes an image to a destination file.
*
* @param object $image
* An image object. The $image->handler value will be changed by this call.
* @param string $destination
* A URI or file path where the image should be saved.
*
* @return bool
* TRUE or FALSE, based on success.
*
* @see image_save()
* @see graphicsmagick_image_save()
*/
function image_graphicsmagick_toolkit_save(object $image, string $destination): bool {
return graphicsmagick_image_save($image, $destination);
}
/**
* Gets details about an image.
*
* @param object $image
* An image object.
*
* @return false|array
* FALSE, if the file could not be found or is not an image. Otherwise, a
* keyed array containing information about the image:
* - "width": Width, in pixels.
* - "height": Height, in pixels.
*
* @see image_get_info()
*/
function image_graphicsmagick_toolkit_get_info(object $image): false|array {
$details = array(
'width' => 0,
'height' => 0,
);
try {
$handler = new \Gmagick(backdrop_realpath($image->source));
$details['width'] = $handler->getImageWidth();
$details['height'] = $handler->getImageHeight();
}
catch (\GmagickException) {
// An exception has been thrown when trying to access the image information,
// which could also mean the image file is corrupted. Return FALSE to signal
// an error.
return FALSE;
}
return $details;
}
/**
* Scales an image to the specified size using the GraphicsMagick library.
*
* @param object $image
* An image object. The $image->info['width'] and $image->info['height']
* values will be modified by this call.
* @param int $width
* The new width of the resized image, in pixels.
* @param int $height
* The new height of the resized image, in pixels.
*
* @return bool
* TRUE or FALSE, based on success.
*
* @see image_resize()
*/
function image_graphicsmagick_toolkit_resize(object $image, int $width, int $height): bool {
$effect = (new class {
public function __invoke($handler, $width, $height) {
// $background, $filter, and $blur are not passed by image_resize() Use
// default values for the last three parameters necessary for
// Gmagick::resizeImage().
return $handler->resizeImage(
$width,
$height,
'transparent',
\Gmagick::FILTER_UNDEFINED,
0.88
);
}
});
$result = graphicsmagick_image_effect($image, $effect, $width, $height);
try {
// The following methods could throw an exception. In the case they do, use
// the width and height values passed to the method.
$new_width = $image->handler->getImageWidth();
$new_height = $image->handler->getImageHeight();
}
catch (\GmagickException|\Error) {
$new_width = $width;
$new_height = $height;
}
$image->info['width'] = $new_width;
$image->info['height'] = $new_height;
return $result;
}
/**
* Rotates an image the given number of degrees.
*
* @param object $image
* An image object. The $image->info['width'] and $image->info['height']
* values will be modified by this call.
* @param int $degrees
* The number of (clockwise) degrees to rotate the image.
* @param int|string|null $background
* A hexadecimal value specifying the background color to use for the
* uncovered area of the image after the rotation, for example 0x000000 for
* black or 0xff00ff for magenta. The default value is #FFFFFF.
*
* @return bool
* TRUE or FALSE, based on success.
*
* @see image_rotate()
*/
function image_graphicsmagick_toolkit_rotate(object $image, int $degrees, int|string|null $background): bool {
try {
$color = Color::toHex($background);
}
catch (\InvalidArgumentException $e) {
watchdog_exception('graphicsmagick', $e);
return FALSE;
}
$effect = (new class {
public function __invoke($handler, $degrees, $background) {
return $handler->rotateImage($degrees, $background);
}
});
return graphicsmagick_image_effect($image, $effect, $degrees, $color);
}
/**
* Crops an image using the GraphicsMagick library.
*
* @param object $image
* An image object. The $image->info['width'] and $image->info['height']
* values will be modified by this call.
* @param int $x
* The starting x offset at which to start the crop, in pixels.
* @param int $y
* The starting y offset at which to start the crop, in pixels.
* @param int $width
* The width of the cropped area, in pixels.
* @param int $height
* The height of the cropped area, in pixels.
*
* @return bool
* TRUE or FALSE, based on success.
*
* @see image_crop()
*/
function image_graphicsmagick_toolkit_crop(object $image, int $x, int $y, int $width, int $height): bool {
$effect = (new class {
public function __invoke($handler, $x, $y, $width, $height) {
$handler = $handler->cropImage($width, $height, $x, $y);
// This method is not documented on
// https://www.php.net/manual/en/class.gmagick.php.
// Verify it is implemented before using it.
if (method_exists($handler, 'setImagePage')) {
$handler = $handler->setImagePage($width, $height, $x, $y);
}
return $handler;
}
});
$result = graphicsmagick_image_effect($image, $effect, $x, $y, $width, $height);
try {
// The following methods could throw an exception. In the case they do, use
// the width and height values passed to the method.
$new_width = $image->handler->getImageWidth();
$new_height = $image->handler->getImageHeight();
}
catch (\GmagickException) {
$new_width = $width;
$new_height = $height;
}
$image->info['width'] = $new_width;
$image->info['height'] = $new_height;
return $result;
}
/**
* Converts an image to grayscale.
*
* @param object $image
* An image object.
*
* @return bool
* TRUE or FALSE, based on success.
*
* @see image_desaturate()
*/
function image_graphicsmagick_toolkit_desaturate(object $image): bool {
$effect = (new class {
public function __invoke($handler) {
return $handler->setImageType(Gmagick::IMGTYPE_GRAYSCALE);
}
});
return graphicsmagick_image_effect($image, $effect);
}
/**
* Blurs an image.
*
* @param object $image
* An image object.
* @param float $radius
* The Gaussian radius.
* @param float $sigma
* The Gaussian sigma.
*
* @return bool
* TRUE or FALSE, based on success.
*/
function image_graphicsmagick_toolkit_blur(object $image, float $radius, float $sigma): bool {
$effect = (new class {
public function __invoke($handler, $radius, $sigma) {
return $handler->blurImage($radius, $sigma);
}
});
return graphicsmagick_image_effect($image, $effect, $radius, $sigma);
}
/**
* Reduces the speckle noise in an image.
*
* @param object $image
* An image object.
*
* @return bool
* TRUE or FALSE, based on success.
*/
function image_graphicsmagick_toolkit_despeckle(object $image): bool {
$effect = (new class {
public function __invoke($handler) {
return $handler->despeckleImage();
}
});
return graphicsmagick_image_effect($image, $effect);
}
/**
* Enhances the image edges.
*
* @param object $image
* An image object.
* @param float $radius
* The operation radius.
*
* @return bool
* TRUE or FALSE, based on success.
*/
function image_graphicsmagick_toolkit_edge(object $image, float $radius): bool {
$effect = (new class {
public function __invoke($handler, $radius) {
return $handler->edgeImage($radius);
}
});
return graphicsmagick_image_effect($image, $effect, $radius);
}
/**
* Embosses an image.
*
* @param object $image
* An image object.
* @param float $radius
* The Gaussian radius.
* @param float $sigma
* The Gaussian sigma.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_emboss(object $image, float $radius, float $sigma): bool {
$effect = (new class {
public function __invoke($handler, $radius, $sigma) {
return $handler->embossImage($radius, $sigma);
}
});
return graphicsmagick_image_effect($image, $effect, $radius, $sigma);
}
/**
* Enhances an image.
*
* @param object $image
* An image object.
*
* @return bool
* TRUE on success, FALSE on failure to blur the image.
*/
function image_graphicsmagick_toolkit_enhance(object $image): bool {
$effect = (new class {
public function __invoke($handler) {
return $handler->enhanceImage();
}
});
return graphicsmagick_image_effect($image, $effect);
}
/**
* Equalizes an image.
*
* @param object $image
* An image object.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_equalize(object $image): bool {
$effect = (new class {
public function __invoke($handler) {
return $handler->equalizeImage();
}
});
return graphicsmagick_image_effect($image, $effect);
}
/**
* Creates a vertical mirror image.
*
* @param object $image
* An image object.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_flip(object $image): bool {
$effect = (new class {
public function __invoke($handler) {
return $handler->flipImage();
}
});
return graphicsmagick_image_effect($image, $effect);
}
/**
* Creates a horizontal mirror image.
*
* @param object $image
* An image object.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_flop(object $image): bool {
$effect = (new class {
public function __invoke($handler) {
return $handler->flopImage();
}
});
return graphicsmagick_image_effect($image, $effect);
}
/**
* Changes the image gamma value.
*
* @param object $image
* An image object.
* @param float $gamma
* The new gamma value.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_gamma(object $image, float $gamma): bool {
$effect = (new class {
public function __invoke($handler, $gamma) {
return $handler->gammaImage($gamma);
}
});
return graphicsmagick_image_effect($image, $effect, $gamma);
}
/**
* Adds random noise.
*
* @param object $image
* An image object.
* @param int $noise
* The noise type.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_noise(object $image, int $noise): bool {
$effect = (new class {
public function __invoke($handler, $noise) {
return $handler->addNoiseImage($noise);
}
});
return graphicsmagick_image_effect($image, $effect, $noise);
}
/**
* Image effect callback; simulate an oil painting.
*
* @param object $image
* An image object.
* @param float $radius
* The radius of the circular region used to calculate the most frequently
* used color.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_oil_paint(object $image, float $radius): bool {
$effect = (new class {
public function __invoke($handler, $radius) {
return $handler->oilPaintImage($radius);
}
});
return graphicsmagick_image_effect($image, $effect, $radius);
}
/**
* Simulates a solarize effect.
*
* @param object $image
* An image object.
* @param int $level
* An integer representing the level of the effect.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_solarize(object $image, int $level): bool {
$effect = (new class {
public function __invoke($handler, $level) {
return $handler->solarizeImage($level);
}
});
return graphicsmagick_image_effect($image, $effect, $level);
}
/**
* Randomly displaces each pixel in a circular region.
*
* @param object $image
* An image object.
*
* @param float $radius
* The radius of the circular region where the pixels are randomly displaced.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_spread(object $image, float $radius): bool {
$effect = (new class {
public function __invoke($handler, $radius) {
return $handler->spreadImage($radius);
}
});
return graphicsmagick_image_effect($image, $effect, $radius);
}
/**
* Swirls the pixels about the center of the image.
*
* @param object $image
* An image object.
* @param float $degrees
* The sweep of the arc through which each pixel is moved.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function image_graphicsmagick_toolkit_swirl(object $image, float $degrees): bool {
$effect = (new class {
public function __invoke($handler, $degrees) {
return $handler->swirlImage($degrees);
}
});
return graphicsmagick_image_effect($image, $effect, $degrees);
}
/**
* Form structure for the blur effect.
*
* Note that this is not a complete form; it only contains the portion of the
* form for configuring the effect options. Therefore, it does not need to
* include metadata about the effect, nor a submit button.
*
* @param array $data
* The current configuration for this effect.
*/
function graphicsmagick_effects_blur_form(array $data): array {
$form['radius'] = array(
'#type' => 'number',
'#title' => t('Radius'),
'#description' => t('This value should typically be either 0, to let the effect choose the best value, or twice the sigma value.'),
'#default_value' => $data['radius'] ?? 0.0,
'#required' => TRUE,
'#min' => 0.0,
'#max' => 65355.0,
'#step' => 0.5,
);
$form['sigma'] = array(
'#type' => 'number',
'#title' => t('Sigma'),
'#description' => t('A large sigma value and a smallish radius value will introduce artifacts in the resulting image; small sigma values are typically only used to fuzz lines and smooth edges on images for which no anti-aliasing was used.'),
'#default_value' => $data['sigma'] ?? 1.0,
'#required' => TRUE,
'#min' => 0.0,
'#max' => 65355.0,
'#step' => 0.5,
);
return $form;
}
/**
* Image effect callback; blur an image.
*
* @param object $image
* An image object.
* @param array $data
* An array of attributes to use when performing the blur effect with the
* following items:
* - "radius": The Gaussian radius.
* - "sigma": The Gaussian sigma.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function graphicsmagick_effects_blur(object $image, array $data): bool {
return image_toolkit_invoke('blur', $image, array_values($data));
}
/**
* Image effect callback; reduce the speckle noise in an image.
*
* @param object $image
* An image object.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function graphicsmagick_effects_despeckle(object $image): bool {
return image_toolkit_invoke('despeckle', $image);
}
/**
* Form structure for the edge effect.
*
* Note that this is not a complete form; it only contains the portion of the
* form for configuring the effect options. Therefore, it does not need to
* include metadata about the effect, nor a submit button.
*
* @param array $data
* An array of attributes to use when performing the effect with the
* following items:
* - "radius": the operation radius.
*/
function graphicsmagick_effects_edge_form(array $data): array {
$form['radius'] = array(
'#type' => 'number',
'#title' => t('Radius'),
'#description' => t('Use a value of zero to let the effect choose the best value.'),
'#default_value' => $data['radius'] ?? 0.0,
'#required' => TRUE,
'#min' => 0.0,
'#max' => 65355.0,
'#step' => 0.5,
);
return $form;
}
/**
* Image effect callback; enhance the image edges.
*
* @param object $image
* An image object.
* @param $data
* An array of attributes to use when performing the effect with the
* following items:
* - "radius": the operation radius.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function graphicsmagick_effects_edge(object $image, array $data): bool {
return image_toolkit_invoke('edge', $image, array_values($data));
}
/**
* Form structure for the emboss effect.
*
* Note that this is not a complete form; it only contains the portion of the
* form for configuring the effect options. Therefore, it does not need to
* include metadata about the effect, nor a submit button.
*
* @param array $data
* The current configuration for this effect.
*/
function graphicsmagick_effects_emboss_form(array $data): array {
$form['radius'] = array(
'#type' => 'number',
'#title' => t('Radius'),
'#description' => t('For reasonable results, the radius value should be higher than the sigma value. Use a value of zero to let the effect choose the best value.'),
'#default_value' => $data['radius'] ?? 0.0,
'#required' => TRUE,
'#min' => 0.0,
'#max' => 65355.0,
'#step' => 0.5,
);
$form['sigma'] = array(
'#type' => 'number',
'#title' => t('Sigma'),
'#description' => t('A large sigma value works well also with color image. It can remove color dominance, but it could also introduce artifacts, especially in JPEG images.'),
'#default_value' => $data['sigma'] ?? 1.0,
'#required' => TRUE,
'#min' => 0.0,
'#max' => 65355.0,
'#step' => 0.5,
);
return $form;
}
/**
* Image effect callback; emboss an image.
*
* @param object $image
* An image object.
* @param array $data
* An array of attributes to use when performing the emboss effect with the
* following items:
* - "radius": The Gaussian radius.
* - "sigma": The Gaussian sigma.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function graphicsmagick_effects_emboss(object $image, array $data): bool {
return image_toolkit_invoke('emboss', $image, array_values($data));
}
/**
* Image effect callback; enhance an image.
*
* @param object $image
* An image object.
* @param array $data
* An array of attributes to use when enhancing an image. No attributes are
* currently used for this effect.
*
* @return bool
* TRUE on success, FALSE on failure to blur the image.
*/
function graphicsmagick_effects_enhance(object $image, array $data): bool {
return image_toolkit_invoke('enhance', $image, array_values($data));
}
/**
* Image effect callback; equalize an image.
*
* @param object $image
* An image object.
* @param array $data
* An array of attributes to use when equalizing an image. No attributes are
* currently used for this effect.
*
* @return bool
* TRUE on success, FALSE on failure to apply the effect.
*/
function graphicsmagick_effects_equalize(object $image, array $data): bool {
return image_toolkit_invoke('equalize', $image, array_values($data));
}
/**
* Image effect callback; create a vertical mirror image.
*
* @param object $image
* An image object.
* @param array $data