forked from Imagick/imagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagick_helpers.c
2018 lines (1800 loc) Β· 79.8 KB
/
imagick_helpers.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 / Imagick |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2013 Mikko Koppanen, Scott MacVicar |
| ImageMagick (c) ImageMagick Studio LLC |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Mikko Kopppanen <[email protected]> |
| Scott MacVicar <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_imagick.h"
#include "php_imagick_defs.h"
#include "php_imagick_macros.h"
#include "php_imagick_helpers.h"
MagickBooleanType php_imagick_progress_monitor(const char *text, const MagickOffsetType offset, const MagickSizeType span, void *client_data)
{
FILE *fp;
php_imagick_object *intern = (php_imagick_object *)client_data;
if (!intern) {
return MagickFalse;
}
if (!intern->progress_monitor_name) {
return MagickFalse;
}
fp = fopen(intern->progress_monitor_name, "a+");
if (!fp) {
return MagickFalse;
}
fprintf(fp, "text: %s, offset: %lld, span: %lld\n", text, offset, span);
fclose(fp);
return MagickTrue;
}
void php_imagick_cleanup_progress_callback(php_imagick_callback* progress_callback TSRMLS_DC) {
if (progress_callback) {
if (progress_callback->previous_callback) {
php_imagick_cleanup_progress_callback(progress_callback->previous_callback TSRMLS_CC);
efree(progress_callback->previous_callback);
}
#if PHP_VERSION_ID >= 70000
zval_ptr_dtor(&progress_callback->user_callback);
#else
zval_ptr_dtor(&progress_callback->user_callback);
#endif
}
}
MagickBooleanType php_imagick_progress_monitor_callable(const char *text, const MagickOffsetType offset, const MagickSizeType span, void *userData)
{
int error;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
#if PHP_VERSION_ID >= 70000
zval zargs[2];
zval retval;
#else
zval **zargs[2];
zval *retval_ptr;
#endif
//We can get the data both via the passed param and via
//IMAGICK_G(progress_callback) - this should be quicker
php_imagick_callback *callback = (php_imagick_callback*)userData;
// This suppresses an 'unused parameter' warning.
// We could maybe use text.
(void)text;
#if PHP_VERSION_ID >= 70000 && defined(ZTS)
if( NULL == tsrm_get_ls_cache() ) {
return MagickTrue;
}
#endif
#if PHP_VERSION_ID < 70000
TSRMLS_FETCH_FROM_CTX(callback->thread_ctx);
#endif
fci_cache = empty_fcall_info_cache;
fci = empty_fcall_info;
fci.size = sizeof(fci);
#if PHP_VERSION_ID < 70100
fci.function_table = EG(function_table);
#endif
#if PHP_VERSION_ID >= 70000
//fci.function_name = *callback->user_callback;
ZVAL_COPY_VALUE(&fci.function_name, &callback->user_callback);
fci.retval = &retval;
#else
retval_ptr = NULL;
fci.function_name = callback->user_callback;
fci.retval_ptr_ptr = &retval_ptr;
#endif
fci.param_count = 2;
fci.params = zargs;
#if PHP_VERSION_ID >= 70000
ZVAL_LONG(&zargs[0], offset);
ZVAL_LONG(&zargs[1], span);
#else
zargs[0] = emalloc(sizeof(zval *));
MAKE_STD_ZVAL(*zargs[0]);
ZVAL_LONG(*zargs[0], offset);
zargs[1] = emalloc(sizeof(zval *));
MAKE_STD_ZVAL(*zargs[1]);
ZVAL_LONG(*zargs[1], span);
#endif
error = zend_call_function(&fci, &fci_cache TSRMLS_CC);
if (error == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the callback");
//Abort processing as user callback is no longer callable
return MagickFalse;
}
#if PHP_VERSION_ID >= 70000
if (Z_TYPE(retval) == IS_FALSE) {
//User returned false - tell Imagick to abort processing.
return MagickFalse;
}
#else
zval_ptr_dtor(zargs[0]);
zval_ptr_dtor(zargs[1]);
if (retval_ptr) {
if (Z_TYPE_P(retval_ptr) == IS_BOOL) {
if (Z_LVAL_P(retval_ptr) == 0) {
//User returned false - tell Imagick to abort processing.
return MagickFalse;
}
}
}
#endif
return MagickTrue;
}
/* This is not universally safe to use, but is safe enough for values that will
be encountered for image dimensions.
*/
static inline double im_round_helper(double value) {
if (value >= 0.0) {
// Prevent zero width/height images
if (value < 1) {
return 1;
}
return floor(value + 0.5);
} else {
return ceil(value - 0.5);
}
}
zend_bool php_imagick_thumbnail_dimensions(MagickWand *magick_wand, zend_bool bestfit, im_long desired_width, im_long desired_height, im_long *new_width, im_long *new_height, zend_bool legacy)
{
im_long orig_width, orig_height;
orig_width = MagickGetImageWidth(magick_wand);
orig_height = MagickGetImageHeight(magick_wand);
if ((orig_width == desired_width) && (orig_height == desired_height)) {
*new_width = desired_width;
*new_height = desired_height;
return 1;
}
if (bestfit) {
double ratio_x, ratio_y;
if (desired_width <= 0 || desired_height <= 0) {
return 0;
}
ratio_x = (double) desired_width / (double) orig_width;
ratio_y = (double) desired_height / (double) orig_height;
//in the case of square images there should be no rounding error
if (ratio_x == ratio_y) {
*new_width = desired_width;
*new_height = desired_height;
} else if (ratio_x < ratio_y) {
*new_width = desired_width;
if (legacy) {
*new_height = ratio_x * ((double) orig_height);
}
else {
*new_height = im_round_helper(ratio_x * ((double) orig_height));
}
} else {
*new_height = desired_height;
if (legacy) {
*new_width = ratio_y * ((double) orig_width);
}
else {
*new_width = im_round_helper(ratio_y * ((double) orig_width));
}
}
*new_width = (*new_width < 1) ? 1 : *new_width;
*new_height = (*new_height < 1) ? 1 : *new_height;
} else {
double ratio;
if (desired_width <= 0 && desired_height <= 0) {
return 0;
}
if (desired_width <= 0 || desired_height <= 0) {
if (desired_width <= 0) {
ratio = (double) orig_height / (double) desired_height;
if (legacy) {
*new_width = ((double) orig_width) / ratio;
}
else {
*new_width = im_round_helper(((double) orig_width) / ratio);
}
*new_height = desired_height;
} else {
ratio = (double) orig_width / (double) desired_width;
if (legacy) {
*new_height = ((double) orig_height) / ratio;
}
else {
*new_height = im_round_helper(((double) orig_height) / ratio);
}
*new_width = desired_width;
}
} else {
*new_width = desired_width;
*new_height = desired_height;
}
}
return 1;
}
zend_bool php_imagick_validate_map(const char *map TSRMLS_DC)
{
zend_bool match;
const char *p = map;
char allow_map[] = { 'R', 'G', 'B',
'A', 'O', 'C',
'Y', 'M', 'K',
'I', 'P' };
while (*p != '\0') {
char *it = allow_map;
match = 0;
while(*it != '\0') {
if (*(it++) == *p) {
match = 1;
break;
}
}
if (!match) {
return 0;
}
p++;
}
return 1;
}
double *php_imagick_zval_to_double_array(zval *param_array, im_long *num_elements TSRMLS_DC)
{
double *double_array;
long i = 0;
#if PHP_VERSION_ID >= 70000
zval *pzvalue;
#else
zval **ppzval;
#endif
*num_elements = zend_hash_num_elements(Z_ARRVAL_P(param_array));
if (*num_elements == 0) {
return NULL;
}
double_array = ecalloc(*num_elements, sizeof(double));
#if PHP_VERSION_ID >= 70000
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(param_array), pzvalue) {
ZVAL_DEREF(pzvalue);
double_array[i] = zval_get_double(pzvalue);
i++;
} ZEND_HASH_FOREACH_END();
#else
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(param_array));
zend_hash_get_current_data(Z_ARRVAL_P(param_array), (void **) &ppzval) == SUCCESS;
zend_hash_move_forward(Z_ARRVAL_P(param_array)), i++)
{
zval tmp_zval, *tmp_pzval;
double value = 0.0;
if (Z_TYPE_PP(ppzval) == IS_DOUBLE) {
value = Z_DVAL_PP(ppzval);
}
else {
tmp_zval = **ppzval;
zval_copy_ctor(&tmp_zval);
tmp_pzval = &tmp_zval;
convert_to_double(tmp_pzval);
value = Z_DVAL_P(tmp_pzval);
zval_dtor (tmp_pzval);
}
double_array[i] = value;
}
#endif
return double_array;
}
im_long *php_imagick_zval_to_long_array(zval *param_array, im_long *num_elements TSRMLS_DC)
{
im_long *long_array;
im_long i = 0;
#if PHP_VERSION_ID >= 70000
zval *pzvalue;
#else
zval **ppzval;
#endif
*num_elements = zend_hash_num_elements(Z_ARRVAL_P(param_array));
if (*num_elements == 0) {
return NULL;
}
long_array = ecalloc(*num_elements, sizeof(im_long));
#if PHP_VERSION_ID >= 70000
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(param_array), pzvalue) {
ZVAL_DEREF(pzvalue);
long_array[i] = zval_get_long(pzvalue);
i++;
} ZEND_HASH_FOREACH_END();
#else
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(param_array));
zend_hash_get_current_data(Z_ARRVAL_P(param_array), (void **) &ppzval) == SUCCESS;
zend_hash_move_forward(Z_ARRVAL_P(param_array)), i++)
{
zval tmp_zval, *tmp_pzval;
im_long value = 0;
if (Z_TYPE_PP(ppzval) == IS_DOUBLE) {
value = Z_LVAL_PP(ppzval);
}
else {
tmp_zval = **ppzval;
zval_copy_ctor(&tmp_zval);
tmp_pzval = &tmp_zval;
convert_to_long(tmp_pzval);
value = Z_LVAL_P(tmp_pzval);
zval_dtor (tmp_pzval);
}
long_array[i] = value;
}
#endif
return long_array;
}
unsigned char *php_imagick_zval_to_char_array(zval *param_array, im_long *num_elements TSRMLS_DC)
{
unsigned char *char_array;
im_long i = 0;
#if PHP_VERSION_ID >= 70000
zval *pzvalue;
#else
zval **ppzval;
#endif
*num_elements = zend_hash_num_elements(Z_ARRVAL_P(param_array));
if (*num_elements == 0) {
return NULL;
}
char_array = ecalloc(*num_elements, sizeof(unsigned char));
#if PHP_VERSION_ID >= 70000
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(param_array), pzvalue) {
ZVAL_DEREF(pzvalue);
char_array[i] = zval_get_long(pzvalue);
i++;
} ZEND_HASH_FOREACH_END();
#else
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(param_array));
zend_hash_get_current_data(Z_ARRVAL_P(param_array), (void **) &ppzval) == SUCCESS;
zend_hash_move_forward(Z_ARRVAL_P(param_array)), i++)
{
zval tmp_zval, *tmp_pzval;
im_long value = 0;
if (Z_TYPE_PP(ppzval) == IS_DOUBLE) {
value = Z_LVAL_PP(ppzval);
}
else {
tmp_zval = **ppzval;
zval_copy_ctor(&tmp_zval);
tmp_pzval = &tmp_zval;
convert_to_long(tmp_pzval);
value = Z_LVAL_P(tmp_pzval);
zval_dtor (tmp_pzval);
}
char_array[i] = value;
}
#endif
return char_array;
}
zend_bool php_imagick_check_font(char *font, int font_len TSRMLS_DC)
{
zend_bool retval = 0;
char **fonts;
unsigned long i = 0;
size_t num_fonts = 0;
/* Check that user is only able to set a proper font */
fonts = MagickQueryFonts("*", &num_fonts);
for(i = 0 ; i < num_fonts ; i++) {
/* Let's see if the font is among configured fonts */
if (strncasecmp(fonts[i], font, font_len) == 0) {
retval = 1;
break;
}
}
IMAGICK_FREE_MAGICK_MEMORY(fonts);
return retval;
}
php_imagick_rw_result_t php_imagick_file_access_check (const char *filename TSRMLS_DC)
{
if (strlen(filename) >= MAXPATHLEN)
return IMAGICK_RW_FILENAME_TOO_LONG;
#if defined(CHECKUID_CHECK_FILE_AND_DIR)
if (PG(safe_mode) && (!php_checkuid_ex(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR, CHECKUID_NO_ERRORS)))
return IMAGICK_RW_SAFE_MODE_ERROR;
#endif
if (php_check_open_basedir_ex(filename, 0 TSRMLS_CC))
return IMAGICK_RW_OPEN_BASEDIR_ERROR;
if (VCWD_ACCESS(filename, F_OK) != 0)
return IMAGICK_RW_PATH_DOES_NOT_EXIST;
if (VCWD_ACCESS(filename, R_OK) != 0)
return IMAGICK_RW_PERMISSION_DENIED;
return IMAGICK_RW_OK;
}
static
void s_rw_fail_to_exception (php_imagick_rw_result_t rc, const char *filename TSRMLS_DC)
{
switch (rc) {
case IMAGICK_RW_SAFE_MODE_ERROR:
zend_throw_exception_ex(php_imagick_exception_class_entry, 1 TSRMLS_CC, "Safe mode restricts user to read the file: %s", filename);
break;
case IMAGICK_RW_OPEN_BASEDIR_ERROR:
zend_throw_exception_ex(php_imagick_exception_class_entry, 1 TSRMLS_CC, "open_basedir restriction in effect. File(%s) is not within the allowed path(s)", filename);
break;
case IMAGICK_RW_PERMISSION_DENIED:
zend_throw_exception_ex(php_imagick_exception_class_entry, 1 TSRMLS_CC, "Permission denied to: %s", filename);
break;
case IMAGICK_RW_FILENAME_TOO_LONG:
zend_throw_exception_ex(php_imagick_exception_class_entry, 1 TSRMLS_CC, "Filename too long: %s", filename);
break;
case IMAGICK_RW_PATH_DOES_NOT_EXIST:
zend_throw_exception_ex(php_imagick_exception_class_entry, 1 TSRMLS_CC, "The path does not exist: %s", filename);
break;
case IMAGICK_RW_PATH_IS_DIR:
zend_throw_exception_ex(php_imagick_exception_class_entry, 1 TSRMLS_CC, "The path is a directory: %s", filename);
break;
default:
zend_throw_exception_ex(php_imagick_exception_class_entry, 1 TSRMLS_CC, "Unknown error");
break;
}
}
void php_imagick_rw_fail_to_exception (MagickWand *magick_wand, php_imagick_rw_result_t rc, const char *filename TSRMLS_DC)
{
if (rc == IMAGICK_RW_UNDERLYING_LIBRARY) {
php_imagick_convert_imagick_exception (magick_wand, "Failed to read the file" TSRMLS_CC);
return;
}
s_rw_fail_to_exception (rc, filename TSRMLS_CC);
}
void php_imagick_imagickdraw_rw_fail_to_exception (DrawingWand *drawing_wand, php_imagick_rw_result_t rc, const char *filename TSRMLS_DC)
{
if (rc == IMAGICK_RW_UNDERLYING_LIBRARY) {
php_imagick_convert_imagickdraw_exception (drawing_wand, "Failed to read the file" TSRMLS_CC);
return;
}
s_rw_fail_to_exception (rc, filename TSRMLS_CC);
}
PointInfo *php_imagick_zval_to_pointinfo_array(zval *coordinate_array, int *num_elements TSRMLS_DC)
{
PointInfo *coordinates;
long elements, sub_elements, i;
HashTable *sub_array;
#if PHP_VERSION_ID >= 70000
zval *pzvalue;
#else
HashTable *coords;
zval **ppzval;
#endif
i = 0;
elements = zend_hash_num_elements(Z_ARRVAL_P(coordinate_array));
if (elements < 1) {
coordinates = (PointInfo *)NULL;
*num_elements = 0;
return coordinates;
}
*num_elements = elements;
coordinates = emalloc(sizeof(PointInfo) * elements);
#if PHP_VERSION_ID >= 70000
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(coordinate_array), pzvalue) {
zval *pz_x, *pz_y;
ZVAL_DEREF(pzvalue);
/* If its something than array lets error here */
if(Z_TYPE_P(pzvalue) != IS_ARRAY) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
/* Subarray should have two elements. X and Y */
sub_elements = zend_hash_num_elements(Z_ARRVAL_P(pzvalue));
/* Exactly two elements */
if (sub_elements != 2) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
/* Subarray values */
sub_array = Z_ARRVAL_P(pzvalue);
/* Get X */
if ((pz_x = zend_hash_str_find(sub_array, "x", sizeof("x")-1)) == NULL) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
/* Get Y */
if ((pz_y = zend_hash_str_find(sub_array, "y", sizeof("y")-1)) == NULL) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
/* Assign X and Y */
coordinates[i].x = zval_get_double(pz_x);
coordinates[i].y = zval_get_double(pz_y);
i++;
} ZEND_HASH_FOREACH_END();
#else
coords = Z_ARRVAL_P(coordinate_array);
zend_hash_internal_pointer_reset_ex(coords, (HashPosition *) 0);
for (i = 0, zend_hash_internal_pointer_reset(coords);
zend_hash_get_current_data(coords, (void **) &ppzval) == SUCCESS;
zend_hash_move_forward(coords), i++
) {
zval **ppz_x, **ppz_y;
zval tmp_zx, *tmp_pzx, tmp_zy, *tmp_pzy;
/* If its something than array lets error here */
if(Z_TYPE_PP(ppzval) != IS_ARRAY) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
/* Subarray should have two elements. X and Y */
sub_elements = zend_hash_num_elements(Z_ARRVAL_PP(ppzval));
/* Exactly two elements */
if (sub_elements != 2) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
/* Subarray values */
sub_array = Z_ARRVAL_PP(ppzval);
/* Get X */
if (zend_hash_find(sub_array, "x", sizeof("x"), (void**)&ppz_x) == FAILURE) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
tmp_zx = **ppz_x;
zval_copy_ctor(&tmp_zx);
tmp_pzx = &tmp_zx;
convert_to_double(tmp_pzx);
/* Get Y */
if (zend_hash_find(sub_array, "y", sizeof("y"), (void**)&ppz_y) == FAILURE) {
efree(coordinates);
*num_elements = 0;
return NULL;
}
tmp_zy = **ppz_y;
zval_copy_ctor(&tmp_zy);
tmp_pzy = &tmp_zy;
convert_to_double(tmp_pzy);
/* Assign X and Y */
coordinates[i].x = Z_DVAL(tmp_zx);
coordinates[i].y = Z_DVAL(tmp_zy);
}
#endif
return coordinates;
}
void php_imagick_throw_exception (php_imagick_class_type_t type, const char *description TSRMLS_DC)
{
int code;
zend_class_entry *ce = NULL;
switch (type) {
case IMAGICK_CLASS:
default:
ce = php_imagick_exception_class_entry;
code = 1;
break;
case IMAGICKDRAW_CLASS:
ce = php_imagickdraw_exception_class_entry;
code = 2;
break;
case IMAGICKPIXELITERATOR_CLASS:
ce = php_imagickpixeliterator_exception_class_entry;
code = 3;
break;
case IMAGICKPIXEL_CLASS:
ce = php_imagickpixel_exception_class_entry;
code = 4;
break;
#ifdef IMAGICK_WITH_KERNEL
case IMAGICKKERNEL_CLASS:
ce = php_imagickkernel_exception_class_entry;
code = 5;
break;
#endif
}
zend_throw_exception(ce, description, code TSRMLS_CC);
}
static
void s_convert_exception (char *description, const char *default_message, long severity, int code TSRMLS_DC)
{
// No description provided or empty one
if (!description || (strlen (description) == 0)) {
if (description) {
description = MagickRelinquishMemory (description);
}
zend_throw_exception(php_imagick_exception_class_entry, default_message, code TSRMLS_CC);
return;
}
zend_throw_exception(php_imagick_exception_class_entry, description, severity TSRMLS_CC);
MagickRelinquishMemory (description);
}
/**
Convert ImageMagick MagickWand exception to PHP exception
*/
void php_imagick_convert_imagick_exception (MagickWand *magick_wand, const char *default_message TSRMLS_DC)
{
ExceptionType severity;
char *description;
description = MagickGetException(magick_wand, &severity);
MagickClearException (magick_wand);
s_convert_exception (description, default_message, severity, 1 TSRMLS_CC);
}
void php_imagick_convert_imagickdraw_exception (DrawingWand *drawing_wand, const char *default_message TSRMLS_DC)
{
ExceptionType severity;
char *description;
description = DrawGetException(drawing_wand, &severity);
DrawClearException (drawing_wand);
s_convert_exception (description, default_message, severity, 2 TSRMLS_CC);
}
void php_imagick_convert_imagickpixeliterator_exception (PixelIterator *pixel_iterator, const char *default_message TSRMLS_DC)
{
ExceptionType severity;
char *description;
description = PixelGetIteratorException(pixel_iterator, &severity);
PixelClearIteratorException (pixel_iterator);
s_convert_exception (description, default_message, severity, 3 TSRMLS_CC);
}
void php_imagick_convert_imagickpixel_exception (PixelWand *pixel_wand, const char *default_message TSRMLS_DC)
{
ExceptionType severity;
char *description;
description = PixelGetException(pixel_wand, &severity);
PixelClearException (pixel_wand);
s_convert_exception (description, default_message, severity, 4 TSRMLS_CC);
}
PixelWand *php_imagick_zval_to_pixelwand (zval *param, php_imagick_class_type_t caller, zend_bool *allocated TSRMLS_DC)
{
PixelWand *pixel_wand = NULL;
*allocated = 0;
#if PHP_VERSION_ID >= 70000
ZVAL_DEREF(param);
#endif
if (Z_TYPE_P (param) == IS_LONG || Z_TYPE_P (param) == IS_DOUBLE) {
zval var;
var = *param;
zval_copy_ctor(&var);
convert_to_string(&var);
param = &var;
}
switch (Z_TYPE_P(param)) {
case IS_STRING:
{
pixel_wand = NewPixelWand();
if (!pixel_wand) {
zend_error(E_ERROR, "Failed to allocate PixelWand structure");
}
*allocated = 1;
if (PixelSetColor (pixel_wand, Z_STRVAL_P(param)) == MagickFalse) {
pixel_wand = DestroyPixelWand(pixel_wand);
php_imagick_throw_exception (caller, "Unrecognized color string" TSRMLS_CC);
return NULL;
}
}
break;
case IS_OBJECT:
if (instanceof_function(Z_OBJCE_P(param), php_imagickpixel_sc_entry TSRMLS_CC)) {
php_imagickpixel_object *intern = Z_IMAGICKPIXEL_P(param);
pixel_wand = intern->pixel_wand;
} else
php_imagick_throw_exception(caller, "The parameter must be an instance of ImagickPixel or a string" TSRMLS_CC);
break;
default:
php_imagick_throw_exception(caller, "Invalid color parameter provided" TSRMLS_CC);
}
return pixel_wand;
}
PixelWand *php_imagick_zval_to_opacity (zval *param, php_imagick_class_type_t caller, zend_bool *allocated TSRMLS_DC)
{
PixelWand *pixel_wand = NULL;
*allocated = 0;
#if PHP_VERSION_ID >= 70000
ZVAL_DEREF(param);
#endif
if (Z_TYPE_P (param) == IS_STRING) {
zval var;
var = *param;
zval_copy_ctor(&var);
convert_to_double(&var);
param = &var;
}
switch (Z_TYPE_P(param)) {
case IS_LONG:
case IS_DOUBLE:
{
pixel_wand = NewPixelWand();
if (!pixel_wand) {
zend_error(E_ERROR, "Failed to allocate PixelWand structure");
}
#if MagickLibVersion >= 0x700
//TOOD - this should be one minus? Or should we just make a BC break
// and make users do it in user land?
PixelSetAlpha(pixel_wand, Z_DVAL_P(param));
#else
PixelSetOpacity(pixel_wand, Z_DVAL_P(param));
#endif
*allocated = 1;
}
break;
case IS_OBJECT:
if (instanceof_function(Z_OBJCE_P(param), php_imagickpixel_sc_entry TSRMLS_CC)) {
php_imagickpixel_object *intern = Z_IMAGICKPIXEL_P(param);
pixel_wand = intern->pixel_wand;
} else
php_imagick_throw_exception(caller, "The parameter must be an instance of ImagickPixel or a string" TSRMLS_CC);
break;
default:
php_imagick_throw_exception(caller, "Invalid color parameter provided" TSRMLS_CC);
}
return pixel_wand;
}
/**
* Changes the locale to IMAGICK_LC_NUMERIC_LOCALE if imagick.locale_fix is on
* and returns the locale set before calling this function.
* If locale is not changed, NULL is returned
*
*/
char *php_imagick_set_locale (TSRMLS_D)
{
char *current_locale;
if (!IMAGICK_G(locale_fix))
return NULL;
current_locale = setlocale(LC_NUMERIC, NULL);
if (current_locale != NULL) {
if (strcmp (current_locale, IMAGICK_LC_NUMERIC_LOCALE) != 0) {
setlocale (LC_NUMERIC, IMAGICK_LC_NUMERIC_LOCALE);
return estrdup (current_locale);
}
}
return NULL;
}
void php_imagick_restore_locale (const char *old_locale)
{
if (!old_locale)
return;
if (strcmp (old_locale, IMAGICK_LC_NUMERIC_LOCALE) != 0)
setlocale (LC_NUMERIC, old_locale);
}
PixelWand *php_imagick_clone_pixelwand (PixelWand *source)
{
#if MagickLibVersion >= 0x635
return ClonePixelWand(source);
#else
PixelWand *target = NewPixelWand ();
if (!target)
return NULL;
PixelSetColorCount (target, PixelGetColorCount (source));
PixelSetRed (target, PixelGetRed (source));
PixelSetGreen (target, PixelGetGreen (source));
PixelSetBlue (target, PixelGetBlue (source));
PixelSetOpacity (target, PixelGetOpacity (source));
PixelSetAlpha (target, PixelGetAlpha (source));
return target;
#endif
}
void php_imagick_replace_magickwand (php_imagick_object *obj, MagickWand *new_wand)
{
if (!obj->magick_wand)
obj->magick_wand = new_wand;
else {
obj->magick_wand = DestroyMagickWand(obj->magick_wand);
obj->magick_wand = new_wand;
}
}
void php_imagick_replace_drawingwand (php_imagickdraw_object *obj, DrawingWand *new_wand)
{
if (!obj->drawing_wand)
obj->drawing_wand = new_wand;
else {
obj->drawing_wand = DestroyDrawingWand(obj->drawing_wand);
obj->drawing_wand = new_wand;
}
}
void php_imagick_replace_pixelwand (php_imagickpixel_object *obj, PixelWand *new_wand)
{
if (obj->pixel_wand && obj->initialized_via_iterator != 1) {
obj->pixel_wand = DestroyPixelWand(obj->pixel_wand);
obj->pixel_wand = new_wand;
} else
obj->pixel_wand = new_wand;
}
zend_bool php_imagick_ensure_not_empty (MagickWand *magick_wand)
{
if (MagickGetNumberImages(magick_wand) == 0) {
TSRMLS_FETCH ();
php_imagick_throw_exception (IMAGICK_CLASS, "Can not process empty Imagick object" TSRMLS_CC);
return 0;
}
return 1;
}
zend_bool php_imagickpixel_ensure_not_null(PixelWand *pixel_wand)
{
if (pixel_wand == NULL) {
TSRMLS_FETCH ();
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Can not process empty ImagickPixel object" TSRMLS_CC);
return 0;
}
return 1;
}
void php_imagick_initialize_constants(TSRMLS_D)
{
#define IMAGICK_REGISTER_CONST_LONG(const_name, value)\
zend_declare_class_constant_long(php_imagick_sc_entry, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
#define IMAGICK_REGISTER_CONST_STRING(const_name, value)\
zend_declare_class_constant_string(php_imagick_sc_entry, const_name, sizeof(const_name)-1, value TSRMLS_CC);
/* Constants defined in php_imagick.h */
IMAGICK_REGISTER_CONST_LONG("COLOR_BLACK", PHP_IMAGICK_COLOR_BLACK);
IMAGICK_REGISTER_CONST_LONG("COLOR_BLUE", PHP_IMAGICK_COLOR_BLUE);
IMAGICK_REGISTER_CONST_LONG("COLOR_CYAN", PHP_IMAGICK_COLOR_CYAN);
IMAGICK_REGISTER_CONST_LONG("COLOR_GREEN", PHP_IMAGICK_COLOR_GREEN);
IMAGICK_REGISTER_CONST_LONG("COLOR_RED", PHP_IMAGICK_COLOR_RED);
IMAGICK_REGISTER_CONST_LONG("COLOR_YELLOW", PHP_IMAGICK_COLOR_YELLOW);
IMAGICK_REGISTER_CONST_LONG("COLOR_MAGENTA", PHP_IMAGICK_COLOR_MAGENTA);
#if MagickLibVersion < 0x700
IMAGICK_REGISTER_CONST_LONG("COLOR_OPACITY", PHP_IMAGICK_COLOR_OPACITY);
#endif
IMAGICK_REGISTER_CONST_LONG("COLOR_ALPHA", PHP_IMAGICK_COLOR_ALPHA);
IMAGICK_REGISTER_CONST_LONG("COLOR_FUZZ", PHP_IMAGICK_COLOR_FUZZ);
/* Returning the version as a constant string */
IMAGICK_REGISTER_CONST_LONG("IMAGICK_EXTNUM", PHP_IMAGICK_EXTNUM);
IMAGICK_REGISTER_CONST_STRING("IMAGICK_EXTVER", PHP_IMAGICK_VERSION);
#if defined(MagickQuantumRange)
IMAGICK_REGISTER_CONST_LONG("QUANTUM_RANGE", atoi (MagickQuantumRange));
#endif
/* Are we using PHP allocations */
#ifdef PHP_IMAGICK_ZEND_MM
IMAGICK_REGISTER_CONST_LONG("USE_ZEND_MM", 1);
#else