-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathgd.c
4183 lines (3456 loc) · 98.4 KB
/
gd.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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| 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: |
| https://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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@php.net> |
| Stig Bakken <ssb@php.net> |
| Jim Winstead <jimw@php.net> |
+----------------------------------------------------------------------+
*/
/* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center,
Cold Spring Harbor Labs. */
/* Note that there is no code from the gd package in this file */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/head.h"
#include <math.h>
#include "SAPI.h"
#include "php_gd.h"
#include "ext/standard/php_image.h"
#include "ext/standard/info.h"
#include "php_open_temporary_file.h"
#include "php_memory_streams.h"
#include "zend_object_handlers.h"
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef PHP_WIN32
# include <io.h>
# include <fcntl.h>
# include <windows.h>
# include <Winuser.h>
# include <Wingdi.h>
#endif
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
# include <X11/xpm.h>
#endif
#include "gd_compat.h"
#ifdef HAVE_GD_BUNDLED
# include "libgd/gd.h"
# include "libgd/gd_errors.h"
# include "libgd/gdfontt.h" /* 1 Tiny font */
# include "libgd/gdfonts.h" /* 2 Small font */
# include "libgd/gdfontmb.h" /* 3 Medium bold font */
# include "libgd/gdfontl.h" /* 4 Large font */
# include "libgd/gdfontg.h" /* 5 Giant font */
#else
# include <gd.h>
# include <gd_errors.h>
# include <gdfontt.h> /* 1 Tiny font */
# include <gdfonts.h> /* 2 Small font */
# include <gdfontmb.h> /* 3 Medium bold font */
# include <gdfontl.h> /* 4 Large font */
# include <gdfontg.h> /* 5 Giant font */
#endif
#if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
# include <ft2build.h>
# include FT_FREETYPE_H
#endif
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
# include "X11/xpm.h"
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* don't used libgd constants, not used, so going to be removed */
#define PHP_GD_FLIP_HORIZONTAL 1
#define PHP_GD_FLIP_VERTICAL 2
#define PHP_GD_FLIP_BOTH 3
#ifdef HAVE_GD_FREETYPE
static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int);
#endif
#include "gd_arginfo.h"
/* as it is not really public, duplicate declaration here to avoid
pointless warnings */
int overflow2(int a, int b);
static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS);
static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS);
/* End Section filters declarations */
static gdImagePtr _php_image_create_from_string(zend_string *Data, char *tn, gdImagePtr (*ioctx_func_p)(gdIOCtxPtr));
static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(FILE *), gdImagePtr (*ioctx_func_p)(gdIOCtxPtr));
static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn);
static gdIOCtx *create_stream_context_from_zval(zval *to_zval);
static gdIOCtx *create_stream_context(php_stream *stream, int close_stream);
static gdIOCtx *create_output_context(void);
static int _php_image_type(zend_string *data);
/* output streaming (formerly gd_ctx.c) */
static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn);
/*********************************************************
*
* GD Object Representation
*
********************************************************/
zend_class_entry *gd_image_ce;
typedef struct _gd_ext_image_object {
gdImagePtr image;
zend_object std;
} php_gd_image_object;
static zend_object_handlers php_gd_image_object_handlers;
static zend_function *php_gd_image_object_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "You cannot initialize a GdImage object except through helper functions");
return NULL;
}
/**
* Returns the underlying php_gd_image_object from a zend_object
*/
static zend_always_inline php_gd_image_object* php_gd_exgdimage_from_zobj_p(zend_object* obj)
{
return (php_gd_image_object *) ((char *) (obj) - XtOffsetOf(php_gd_image_object, std));
}
/**
* Converts an extension GdImage instance contained within a zval into the gdImagePtr
* for use with library APIs
*/
PHP_GD_API gdImagePtr php_gd_libgdimageptr_from_zval_p(zval* zp)
{
return php_gd_exgdimage_from_zobj_p(Z_OBJ_P(zp))->image;
}
zend_object *php_gd_image_object_create(zend_class_entry *class_type)
{
size_t block_len = sizeof(php_gd_image_object) + zend_object_properties_size(class_type);
php_gd_image_object *intern = emalloc(block_len);
memset(intern, 0, block_len);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static void php_gd_image_object_free(zend_object *intern)
{
php_gd_image_object *img_obj_ptr = php_gd_exgdimage_from_zobj_p(intern);
if (img_obj_ptr->image) {
gdImageDestroy(img_obj_ptr->image);
}
zend_object_std_dtor(intern);
}
/**
* Creates a new GdImage object wrapping the gdImagePtr and attaches it
* to the zval (usually return_value).
*
* This function must only be called once per valid gdImagePtr
*/
void php_gd_assign_libgdimageptr_as_extgdimage(zval *val, gdImagePtr image)
{
object_init_ex(val, gd_image_ce);
php_gd_exgdimage_from_zobj_p(Z_OBJ_P(val))->image = image;
}
static void php_gd_object_minit_helper(void)
{
gd_image_ce = register_class_GdImage();
gd_image_ce->create_object = php_gd_image_object_create;
gd_image_ce->default_object_handlers = &php_gd_image_object_handlers;
/* setting up the object handlers for the GdImage class */
memcpy(&php_gd_image_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
php_gd_image_object_handlers.clone_obj = NULL;
php_gd_image_object_handlers.free_obj = php_gd_image_object_free;
php_gd_image_object_handlers.get_constructor = php_gd_image_object_get_constructor;
php_gd_image_object_handlers.compare = zend_objects_not_comparable;
php_gd_image_object_handlers.offset = XtOffsetOf(php_gd_image_object, std);
}
static zend_class_entry *gd_font_ce = NULL;
static zend_object_handlers php_gd_font_object_handlers;
typedef struct _php_gd_font_object {
gdFontPtr font;
zend_object std;
} php_gd_font_object;
static php_gd_font_object *php_gd_font_object_from_zend_object(zend_object *zobj)
{
return ((php_gd_font_object*)(zobj + 1)) - 1;
}
static zend_object *php_gd_font_object_to_zend_object(php_gd_font_object *obj)
{
return ((zend_object*)(obj + 1)) - 1;
}
static zend_object *php_gd_font_object_create(zend_class_entry *ce)
{
php_gd_font_object *obj = zend_object_alloc(sizeof(php_gd_font_object), ce);
zend_object *zobj = php_gd_font_object_to_zend_object(obj);
obj->font = NULL;
zend_object_std_init(zobj, ce);
object_properties_init(zobj, ce);
zobj->handlers = &php_gd_font_object_handlers;
return zobj;
}
static void php_gd_font_object_free(zend_object *zobj)
{
php_gd_font_object *obj = php_gd_font_object_from_zend_object(zobj);
if (obj->font) {
if (obj->font->data) {
efree(obj->font->data);
}
efree(obj->font);
obj->font = NULL;
}
zend_object_std_dtor(zobj);
}
static zend_function *php_gd_font_object_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "You cannot initialize a GdFont object except through helper functions");
return NULL;
}
static void php_gd_font_minit_helper(void)
{
gd_font_ce = register_class_GdFont();
gd_font_ce->create_object = php_gd_font_object_create;
/* setting up the object handlers for the GdFont class */
memcpy(&php_gd_font_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
php_gd_font_object_handlers.clone_obj = NULL;
php_gd_font_object_handlers.free_obj = php_gd_font_object_free;
php_gd_font_object_handlers.get_constructor = php_gd_font_object_get_constructor;
php_gd_font_object_handlers.offset = XtOffsetOf(php_gd_font_object, std);
}
/*********************************************************
*
* Extension Implementation
*
********************************************************/
zend_module_entry gd_module_entry = {
STANDARD_MODULE_HEADER,
"gd",
ext_functions,
PHP_MINIT(gd),
PHP_MSHUTDOWN(gd),
NULL,
PHP_RSHUTDOWN(gd),
PHP_MINFO(gd),
PHP_GD_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_GD
ZEND_GET_MODULE(gd)
#endif
/* {{{ PHP_INI_BEGIN */
PHP_INI_BEGIN()
PHP_INI_ENTRY_EX("gd.jpeg_ignore_warning", "1", PHP_INI_ALL, NULL, zend_ini_boolean_displayer_cb)
PHP_INI_END()
/* }}} */
/* {{{ php_gd_error_method */
void php_gd_error_method(int type, const char *format, va_list args)
{
switch (type) {
#ifndef PHP_WIN32
case GD_DEBUG:
case GD_INFO:
#endif
case GD_NOTICE:
type = E_NOTICE;
break;
case GD_WARNING:
type = E_WARNING;
break;
default:
type = E_ERROR;
}
php_verror(NULL, "", type, format, args);
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(gd)
{
php_gd_object_minit_helper();
php_gd_font_minit_helper();
#if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
gdFontCacheMutexSetup();
#endif
gdSetErrorMethod(php_gd_error_method);
REGISTER_INI_ENTRIES();
register_gd_symbols(module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(gd)
{
#if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
gdFontCacheMutexShutdown();
#endif
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(gd)
{
#ifdef HAVE_GD_FREETYPE
gdFontCacheShutdown();
#endif
return SUCCESS;
}
/* }}} */
#ifdef HAVE_GD_BUNDLED
#define PHP_GD_VERSION_STRING "bundled (2.1.0 compatible)"
#else
# define PHP_GD_VERSION_STRING GD_VERSION_STRING
#endif
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(gd)
{
php_info_print_table_start();
php_info_print_table_row(2, "GD Support", "enabled");
/* need to use a PHPAPI function here because it is external module in windows */
#ifdef HAVE_GD_BUNDLED
php_info_print_table_row(2, "GD Version", PHP_GD_VERSION_STRING);
#else
php_info_print_table_row(2, "GD headers Version", PHP_GD_VERSION_STRING);
#ifdef HAVE_GD_LIBVERSION
php_info_print_table_row(2, "GD library Version", gdVersionString());
#endif
#endif
#ifdef HAVE_GD_FREETYPE
php_info_print_table_row(2, "FreeType Support", "enabled");
php_info_print_table_row(2, "FreeType Linkage", "with freetype");
#ifdef HAVE_GD_BUNDLED
{
char tmp[256];
#ifdef FREETYPE_PATCH
snprintf(tmp, sizeof(tmp), "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
#elif defined(FREETYPE_MAJOR)
snprintf(tmp, sizeof(tmp), "%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR);
#else
snprintf(tmp, sizeof(tmp), "1.x");
#endif
php_info_print_table_row(2, "FreeType Version", tmp);
}
#endif
#endif
php_info_print_table_row(2, "GIF Read Support", "enabled");
php_info_print_table_row(2, "GIF Create Support", "enabled");
#ifdef HAVE_GD_JPG
{
php_info_print_table_row(2, "JPEG Support", "enabled");
#ifdef HAVE_GD_BUNDLED
php_info_print_table_row(2, "libJPEG Version", gdJpegGetVersionString());
#endif
}
#endif
#ifdef HAVE_GD_PNG
php_info_print_table_row(2, "PNG Support", "enabled");
#ifdef HAVE_GD_BUNDLED
php_info_print_table_row(2, "libPNG Version", gdPngGetVersionString());
#endif
#endif
php_info_print_table_row(2, "WBMP Support", "enabled");
#ifdef HAVE_GD_XPM
php_info_print_table_row(2, "XPM Support", "enabled");
#ifdef HAVE_GD_BUNDLED
{
char tmp[12];
snprintf(tmp, sizeof(tmp), "%d", XpmLibraryVersion());
php_info_print_table_row(2, "libXpm Version", tmp);
}
#endif
#endif
php_info_print_table_row(2, "XBM Support", "enabled");
#ifdef USE_GD_JISX0208
php_info_print_table_row(2, "JIS-mapped Japanese Font Support", "enabled");
#endif
#ifdef HAVE_GD_WEBP
php_info_print_table_row(2, "WebP Support", "enabled");
#endif
#ifdef HAVE_GD_BMP
php_info_print_table_row(2, "BMP Support", "enabled");
#endif
#ifdef HAVE_GD_AVIF
php_info_print_table_row(2, "AVIF Support", "enabled");
#endif
#ifdef HAVE_GD_TGA
php_info_print_table_row(2, "TGA Read Support", "enabled");
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ */
PHP_FUNCTION(gd_info)
{
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
array_init(return_value);
add_assoc_string(return_value, "GD Version", PHP_GD_VERSION_STRING);
#ifdef HAVE_GD_FREETYPE
add_assoc_bool(return_value, "FreeType Support", 1);
add_assoc_string(return_value, "FreeType Linkage", "with freetype");
#else
add_assoc_bool(return_value, "FreeType Support", 0);
#endif
add_assoc_bool(return_value, "GIF Read Support", 1);
add_assoc_bool(return_value, "GIF Create Support", 1);
#ifdef HAVE_GD_JPG
add_assoc_bool(return_value, "JPEG Support", 1);
#else
add_assoc_bool(return_value, "JPEG Support", 0);
#endif
#ifdef HAVE_GD_PNG
add_assoc_bool(return_value, "PNG Support", 1);
#else
add_assoc_bool(return_value, "PNG Support", 0);
#endif
add_assoc_bool(return_value, "WBMP Support", 1);
#ifdef HAVE_GD_XPM
add_assoc_bool(return_value, "XPM Support", 1);
#else
add_assoc_bool(return_value, "XPM Support", 0);
#endif
add_assoc_bool(return_value, "XBM Support", 1);
#ifdef HAVE_GD_WEBP
add_assoc_bool(return_value, "WebP Support", 1);
#else
add_assoc_bool(return_value, "WebP Support", 0);
#endif
#ifdef HAVE_GD_BMP
add_assoc_bool(return_value, "BMP Support", 1);
#else
add_assoc_bool(return_value, "BMP Support", 0);
#endif
#ifdef HAVE_GD_AVIF
add_assoc_bool(return_value, "AVIF Support", 1);
#else
add_assoc_bool(return_value, "AVIF Support", 0);
#endif
#ifdef HAVE_GD_TGA
add_assoc_bool(return_value, "TGA Read Support", 1);
#else
add_assoc_bool(return_value, "TGA Read Support", 0);
#endif
#ifdef USE_GD_JISX0208
add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);
#else
add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);
#endif
}
/* }}} */
#define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))
/* {{{ Load a new font */
PHP_FUNCTION(imageloadfont)
{
zend_string *file;
int hdr_size = sizeof(gdFont) - sizeof(char *);
int body_size, n = 0, b, i, body_size_check;
gdFontPtr font;
php_stream *stream;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file) == FAILURE) {
RETURN_THROWS();
}
stream = php_stream_open_wrapper(ZSTR_VAL(file), "rb", IGNORE_PATH | REPORT_ERRORS, NULL);
if (stream == NULL) {
RETURN_FALSE;
}
/* Only supports a architecture-dependent binary dump format
* at the moment.
* The file format is like this on machines with 32-byte integers:
*
* byte 0-3: (int) number of characters in the font
* byte 4-7: (int) value of first character in the font (often 32, space)
* byte 8-11: (int) pixel width of each character
* byte 12-15: (int) pixel height of each character
* bytes 16-: (char) array with character data, one byte per pixel
* in each character, for a total of
* (nchars*width*height) bytes.
*/
font = (gdFontPtr) emalloc(sizeof(gdFont));
b = 0;
while (b < hdr_size && (n = php_stream_read(stream, (char*)&font[b], hdr_size - b)) > 0) {
b += n;
}
if (n <= 0) {
efree(font);
if (php_stream_eof(stream)) {
php_error_docref(NULL, E_WARNING, "End of file while reading header");
} else {
php_error_docref(NULL, E_WARNING, "Error while reading header");
}
php_stream_close(stream);
RETURN_FALSE;
}
i = php_stream_tell(stream);
php_stream_seek(stream, 0, SEEK_END);
body_size_check = php_stream_tell(stream) - hdr_size;
php_stream_seek(stream, i, SEEK_SET);
if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
php_error_docref(NULL, E_WARNING, "Error reading font, invalid font header");
efree(font);
php_stream_close(stream);
RETURN_FALSE;
}
body_size = font->w * font->h * font->nchars;
if (body_size != body_size_check) {
font->w = FLIPWORD(font->w);
font->h = FLIPWORD(font->h);
font->nchars = FLIPWORD(font->nchars);
if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
php_error_docref(NULL, E_WARNING, "Error reading font, invalid font header");
efree(font);
php_stream_close(stream);
RETURN_FALSE;
}
body_size = font->w * font->h * font->nchars;
}
if (body_size != body_size_check) {
php_error_docref(NULL, E_WARNING, "Error reading font");
efree(font);
php_stream_close(stream);
RETURN_FALSE;
}
ZEND_ASSERT(body_size > 0);
font->data = emalloc(body_size);
b = 0;
while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b)) > 0) {
b += n;
}
if (n <= 0) {
efree(font->data);
efree(font);
if (php_stream_eof(stream)) {
php_error_docref(NULL, E_WARNING, "End of file while reading body");
} else {
php_error_docref(NULL, E_WARNING, "Error while reading body");
}
php_stream_close(stream);
RETURN_FALSE;
}
php_stream_close(stream);
object_init_ex(return_value, gd_font_ce);
php_gd_font_object_from_zend_object(Z_OBJ_P(return_value))->font = font;
}
/* }}} */
/* {{{ Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */
PHP_FUNCTION(imagesetstyle)
{
zval *IM, *styles, *item;
gdImagePtr im;
int *stylearr;
int index = 0;
uint32_t num_styles;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &IM, gd_image_ce, &styles) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
num_styles = zend_hash_num_elements(Z_ARRVAL_P(styles));
if (num_styles == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}
/* copy the style values in the stylearr */
stylearr = safe_emalloc(sizeof(int), num_styles, 0);
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(styles), item) {
stylearr[index++] = zval_get_long(item);
} ZEND_HASH_FOREACH_END();
gdImageSetStyle(im, stylearr, index);
efree(stylearr);
RETURN_TRUE;
}
/* }}} */
/* {{{ Create a new true color image */
PHP_FUNCTION(imagecreatetruecolor)
{
zend_long x_size, y_size;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
RETURN_THROWS();
}
if (x_size <= 0 || x_size >= INT_MAX) {
zend_argument_value_error(1, "must be greater than 0");
RETURN_THROWS();
}
if (y_size <= 0 || y_size >= INT_MAX) {
zend_argument_value_error(2, "must be greater than 0");
RETURN_THROWS();
}
im = gdImageCreateTrueColor(x_size, y_size);
if (!im) {
RETURN_FALSE;
}
php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
}
/* }}} */
/* {{{ return true if the image uses truecolor */
PHP_FUNCTION(imageistruecolor)
{
zval *IM;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
RETURN_BOOL(im->trueColor);
}
/* }}} */
/* {{{ Convert a true color image to a palette based image with a number of colors, optionally using dithering. */
PHP_FUNCTION(imagetruecolortopalette)
{
zval *IM;
bool dither;
zend_long ncolors;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Obl", &IM, gd_image_ce, &dither, &ncolors) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
if (ncolors <= 0 || ZEND_LONG_INT_OVFL(ncolors)) {
zend_argument_value_error(3, "must be greater than 0 and less than %d", INT_MAX);
RETURN_THROWS();
}
if (gdImageTrueColorToPalette(im, dither, (int)ncolors)) {
RETURN_TRUE;
} else {
php_error_docref(NULL, E_WARNING, "Couldn't convert to palette");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Convert a palette based image to a true color image. */
PHP_FUNCTION(imagepalettetotruecolor)
{
zval *IM;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
if (gdImagePaletteToTrueColor(im) == 0) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ Makes the colors of the palette version of an image more closely match the true color version */
PHP_FUNCTION(imagecolormatch)
{
zval *IM1, *IM2;
gdImagePtr im1, im2;
int result;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM1, gd_image_ce, &IM2, gd_image_ce) == FAILURE) {
RETURN_THROWS();
}
im1 = php_gd_libgdimageptr_from_zval_p(IM1);
im2 = php_gd_libgdimageptr_from_zval_p(IM2);
result = gdImageColorMatch(im1, im2);
switch (result) {
case -1:
zend_argument_value_error(1, "must be TrueColor");
RETURN_THROWS();
break;
case -2:
zend_argument_value_error(2, "must be Palette");
RETURN_THROWS();
break;
case -3:
zend_argument_value_error(2, "must be the same size as argument #1 ($im1)");
RETURN_THROWS();
break;
case -4:
zend_argument_value_error(2, "must have at least one color");
RETURN_THROWS();
break;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */
PHP_FUNCTION(imagesetthickness)
{
zval *IM;
zend_long thick;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &thick) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
gdImageSetThickness(im, thick);
RETURN_TRUE;
}
/* }}} */
/* {{{ Draw an ellipse */
PHP_FUNCTION(imagefilledellipse)
{
zval *IM;
zend_long cx, cy, w, h, color;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &color) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
gdImageFilledEllipse(im, cx, cy, w, h, color);
RETURN_TRUE;
}
/* }}} */
/* {{{ Draw a filled partial ellipse */
PHP_FUNCTION(imagefilledarc)
{
zval *IM;
zend_long cx, cy, w, h, ST, E, col, style;
gdImagePtr im;
int e, st;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollllllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
e = E;
if (e < 0) {
e %= 360;
}
st = ST;
if (st < 0) {
st %= 360;
}
gdImageFilledArc(im, cx, cy, w, h, st, e, col, style);
RETURN_TRUE;
}
/* }}} */
/* {{{ Turn alpha blending mode on or off for the given image */
PHP_FUNCTION(imagealphablending)
{
zval *IM;
bool blend;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &blend) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
gdImageAlphaBlending(im, blend);
RETURN_TRUE;
}
/* }}} */
/* {{{ Include alpha channel to a saved image */
PHP_FUNCTION(imagesavealpha)
{
zval *IM;
bool save;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &save) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
gdImageSaveAlpha(im, save);
RETURN_TRUE;
}
/* }}} */
/* {{{ Set the alpha blending flag to use the bundled libgd layering effects */
PHP_FUNCTION(imagelayereffect)
{
zval *IM;
zend_long effect;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &effect) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
gdImageAlphaBlending(im, effect);
RETURN_TRUE;
}
/* }}} */
#define CHECK_RGBA_RANGE(component, name, argument_number) \
if (component < 0 || component > gd##name##Max) { \
zend_argument_value_error(argument_number, "must be between 0 and %d (inclusive)", gd##name##Max); \
RETURN_THROWS(); \
}
/* {{{ Allocate a color with an alpha level. Works for true color and palette based images */
PHP_FUNCTION(imagecolorallocatealpha)
{
zval *IM;
zend_long red, green, blue, alpha;
gdImagePtr im;
int ct = (-1);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
CHECK_RGBA_RANGE(red, Red, 2);
CHECK_RGBA_RANGE(green, Green, 3);
CHECK_RGBA_RANGE(blue, Blue, 4);
CHECK_RGBA_RANGE(alpha, Alpha, 5);
ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha);
if (ct < 0) {
RETURN_FALSE;
}
RETURN_LONG((zend_long)ct);
}
/* }}} */
/* {{{ Resolve/Allocate a colour with an alpha level. Works for true colour and palette based images */
PHP_FUNCTION(imagecolorresolvealpha)
{
zval *IM;
zend_long red, green, blue, alpha;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
CHECK_RGBA_RANGE(red, Red, 2);
CHECK_RGBA_RANGE(green, Green, 3);
CHECK_RGBA_RANGE(blue, Blue, 4);
CHECK_RGBA_RANGE(alpha, Alpha, 5);
RETURN_LONG(gdImageColorResolveAlpha(im, red, green, blue, alpha));
}
/* }}} */
/* {{{ Find the closest matching colour with alpha transparency */
PHP_FUNCTION(imagecolorclosestalpha)
{
zval *IM;
zend_long red, green, blue, alpha;
gdImagePtr im;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
RETURN_THROWS();
}
im = php_gd_libgdimageptr_from_zval_p(IM);
CHECK_RGBA_RANGE(red, Red, 2);
CHECK_RGBA_RANGE(green, Green, 3);
CHECK_RGBA_RANGE(blue, Blue, 4);
CHECK_RGBA_RANGE(alpha, Alpha, 5);