Skip to content

Commit 05bb051

Browse files
committed
Expose gfxinfo as php_gfxinfo
This is necessary for future commits, when we extend the image handling to support extensions adding their own handlers. Also extend the struct with fields for when the width and height are not numbers but strings (e.g. for SVG).
1 parent 03a9f03 commit 05bb051

File tree

2 files changed

+60
-59
lines changed

2 files changed

+60
-59
lines changed

ext/standard/image.c

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,11 @@ PHPAPI const char php_sig_heix[4] = {'h', 'e', 'i', 'x'};
5959
/* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
6060
/* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
6161

62-
/* return info as a struct, to make expansion easier */
63-
64-
struct gfxinfo {
65-
unsigned int width;
66-
unsigned int height;
67-
unsigned int bits;
68-
unsigned int channels;
69-
};
70-
7162
/* {{{ php_handle_gif
7263
* routine to handle GIF files. If only everything were that easy... ;} */
73-
static struct gfxinfo *php_handle_gif (php_stream * stream)
64+
static struct php_gfxinfo *php_handle_gif (php_stream * stream)
7465
{
75-
struct gfxinfo *result = NULL;
66+
struct php_gfxinfo *result = NULL;
7667
unsigned char dim[5];
7768

7869
if (php_stream_seek(stream, 3, SEEK_CUR))
@@ -81,7 +72,7 @@ static struct gfxinfo *php_handle_gif (php_stream * stream)
8172
if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
8273
return NULL;
8374

84-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
75+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
8576
result->width = (unsigned int)dim[0] | (((unsigned int)dim[1])<<8);
8677
result->height = (unsigned int)dim[2] | (((unsigned int)dim[3])<<8);
8778
result->bits = dim[4]&0x80 ? ((((unsigned int)dim[4])&0x07) + 1) : 0;
@@ -92,9 +83,9 @@ static struct gfxinfo *php_handle_gif (php_stream * stream)
9283
/* }}} */
9384

9485
/* {{{ php_handle_psd */
95-
static struct gfxinfo *php_handle_psd (php_stream * stream)
86+
static struct php_gfxinfo *php_handle_psd (php_stream * stream)
9687
{
97-
struct gfxinfo *result = NULL;
88+
struct php_gfxinfo *result = NULL;
9889
unsigned char dim[8];
9990

10091
if (php_stream_seek(stream, 11, SEEK_CUR))
@@ -103,7 +94,7 @@ static struct gfxinfo *php_handle_psd (php_stream * stream)
10394
if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
10495
return NULL;
10596

106-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
97+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
10798
result->height = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
10899
result->width = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
109100

@@ -112,9 +103,9 @@ static struct gfxinfo *php_handle_psd (php_stream * stream)
112103
/* }}} */
113104

114105
/* {{{ php_handle_bmp */
115-
static struct gfxinfo *php_handle_bmp (php_stream * stream)
106+
static struct php_gfxinfo *php_handle_bmp (php_stream * stream)
116107
{
117-
struct gfxinfo *result = NULL;
108+
struct php_gfxinfo *result = NULL;
118109
unsigned char dim[16];
119110
int size;
120111

@@ -126,12 +117,12 @@ static struct gfxinfo *php_handle_bmp (php_stream * stream)
126117

127118
size = (((unsigned int)dim[ 3]) << 24) + (((unsigned int)dim[ 2]) << 16) + (((unsigned int)dim[ 1]) << 8) + ((unsigned int) dim[ 0]);
128119
if (size == 12) {
129-
result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
120+
result = (struct php_gfxinfo *) ecalloc (1, sizeof(struct php_gfxinfo));
130121
result->width = (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
131122
result->height = (((unsigned int)dim[ 7]) << 8) + ((unsigned int) dim[ 6]);
132123
result->bits = ((unsigned int)dim[11]);
133124
} else if (size > 12 && (size <= 64 || size == 108 || size == 124)) {
134-
result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
125+
result = (struct php_gfxinfo *) ecalloc (1, sizeof(struct php_gfxinfo));
135126
result->width = (((unsigned int)dim[ 7]) << 24) + (((unsigned int)dim[ 6]) << 16) + (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
136127
result->height = (((unsigned int)dim[11]) << 24) + (((unsigned int)dim[10]) << 16) + (((unsigned int)dim[ 9]) << 8) + ((unsigned int) dim[ 8]);
137128
result->height = abs((int32_t)result->height);
@@ -162,9 +153,9 @@ static unsigned long int php_swf_get_bits (unsigned char* buffer, unsigned int p
162153

163154
#if defined(HAVE_ZLIB) && !defined(COMPILE_DL_ZLIB)
164155
/* {{{ php_handle_swc */
165-
static struct gfxinfo *php_handle_swc(php_stream * stream)
156+
static struct php_gfxinfo *php_handle_swc(php_stream * stream)
166157
{
167-
struct gfxinfo *result = NULL;
158+
struct php_gfxinfo *result = NULL;
168159

169160
long bits;
170161
unsigned char a[64];
@@ -231,7 +222,7 @@ static struct gfxinfo *php_handle_swc(php_stream * stream)
231222
}
232223

233224
if (!status) {
234-
result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
225+
result = (struct php_gfxinfo *) ecalloc (1, sizeof (struct php_gfxinfo));
235226
bits = php_swf_get_bits (b, 0, 5);
236227
result->width = (php_swf_get_bits (b, 5 + bits, bits) -
237228
php_swf_get_bits (b, 5, bits)) / 20;
@@ -248,9 +239,9 @@ static struct gfxinfo *php_handle_swc(php_stream * stream)
248239
#endif
249240

250241
/* {{{ php_handle_swf */
251-
static struct gfxinfo *php_handle_swf (php_stream * stream)
242+
static struct php_gfxinfo *php_handle_swf (php_stream * stream)
252243
{
253-
struct gfxinfo *result = NULL;
244+
struct php_gfxinfo *result = NULL;
254245
long bits;
255246
unsigned char a[32];
256247

@@ -260,7 +251,7 @@ static struct gfxinfo *php_handle_swf (php_stream * stream)
260251
if (php_stream_read(stream, (char*)a, sizeof(a)) != sizeof(a))
261252
return NULL;
262253

263-
result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
254+
result = (struct php_gfxinfo *) ecalloc (1, sizeof (struct php_gfxinfo));
264255
bits = php_swf_get_bits (a, 0, 5);
265256
result->width = (php_swf_get_bits (a, 5 + bits, bits) -
266257
php_swf_get_bits (a, 5, bits)) / 20;
@@ -274,9 +265,9 @@ static struct gfxinfo *php_handle_swf (php_stream * stream)
274265

275266
/* {{{ php_handle_png
276267
* routine to handle PNG files */
277-
static struct gfxinfo *php_handle_png (php_stream * stream)
268+
static struct php_gfxinfo *php_handle_png (php_stream * stream)
278269
{
279-
struct gfxinfo *result = NULL;
270+
struct php_gfxinfo *result = NULL;
280271
unsigned char dim[9];
281272
/* Width: 4 bytes
282273
* Height: 4 bytes
@@ -293,7 +284,7 @@ static struct gfxinfo *php_handle_png (php_stream * stream)
293284
if((php_stream_read(stream, (char*)dim, sizeof(dim))) < sizeof(dim))
294285
return NULL;
295286

296-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
287+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
297288
result->width = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
298289
result->height = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
299290
result->bits = (unsigned int)dim[8];
@@ -452,9 +443,9 @@ static int php_read_APP(php_stream * stream, unsigned int marker, zval *info)
452443

453444
/* {{{ php_handle_jpeg
454445
main loop to parse JPEG structure */
455-
static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
446+
static struct php_gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
456447
{
457-
struct gfxinfo *result = NULL;
448+
struct php_gfxinfo *result = NULL;
458449
unsigned int marker = M_PSEUDO;
459450
unsigned short length, ff_read=1;
460451

@@ -477,7 +468,7 @@ static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
477468
case M_SOF15:
478469
if (result == NULL) {
479470
/* handle SOFn block */
480-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
471+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
481472
length = php_read2(stream);
482473
result->bits = php_stream_getc(stream);
483474
result->height = php_read2(stream);
@@ -580,9 +571,9 @@ static unsigned int php_read4(php_stream * stream)
580571

581572
/* {{{ php_handle_jpc
582573
Main loop to parse JPEG2000 raw codestream structure */
583-
static struct gfxinfo *php_handle_jpc(php_stream * stream)
574+
static struct php_gfxinfo *php_handle_jpc(php_stream * stream)
584575
{
585-
struct gfxinfo *result = NULL;
576+
struct php_gfxinfo *result = NULL;
586577
int highest_bit_depth, bit_depth;
587578
unsigned char first_marker_id;
588579
unsigned int i;
@@ -603,7 +594,7 @@ static struct gfxinfo *php_handle_jpc(php_stream * stream)
603594
return NULL;
604595
}
605596

606-
result = (struct gfxinfo *)ecalloc(1, sizeof(struct gfxinfo));
597+
result = (struct php_gfxinfo *)ecalloc(1, sizeof(struct php_gfxinfo));
607598

608599
php_read2(stream); /* Lsiz */
609600
php_read2(stream); /* Rsiz */
@@ -651,9 +642,9 @@ static struct gfxinfo *php_handle_jpc(php_stream * stream)
651642

652643
/* {{{ php_handle_jp2
653644
main loop to parse JPEG 2000 JP2 wrapper format structure */
654-
static struct gfxinfo *php_handle_jp2(php_stream *stream)
645+
static struct php_gfxinfo *php_handle_jp2(php_stream *stream)
655646
{
656-
struct gfxinfo *result = NULL;
647+
struct php_gfxinfo *result = NULL;
657648
unsigned int box_length;
658649
unsigned int box_type;
659650
char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
@@ -777,9 +768,9 @@ static unsigned php_ifd_get32u(void *Long, int motorola_intel)
777768

778769
/* {{{ php_handle_tiff
779770
main loop to parse TIFF structure */
780-
static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
771+
static struct php_gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
781772
{
782-
struct gfxinfo *result = NULL;
773+
struct php_gfxinfo *result = NULL;
783774
int i, num_entries;
784775
unsigned char *dir_entry;
785776
size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
@@ -845,7 +836,7 @@ static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int mot
845836
efree(ifd_data);
846837
if ( width && height) {
847838
/* not the same when in for-loop */
848-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
839+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
849840
result->height = height;
850841
result->width = width;
851842
result->bits = 0;
@@ -857,9 +848,9 @@ static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int mot
857848
/* }}} */
858849

859850
/* {{{ php_handle_psd */
860-
static struct gfxinfo *php_handle_iff(php_stream * stream)
851+
static struct php_gfxinfo *php_handle_iff(php_stream * stream)
861852
{
862-
struct gfxinfo * result;
853+
struct php_gfxinfo * result;
863854
unsigned char a[10];
864855
int chunkId;
865856
int size;
@@ -893,7 +884,7 @@ static struct gfxinfo *php_handle_iff(php_stream * stream)
893884
height = php_ifd_get16s(a+2, 1);
894885
bits = a[8] & 0xff;
895886
if (width > 0 && height > 0 && bits > 0 && bits < 33) {
896-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
887+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
897888
result->width = width;
898889
result->height = height;
899890
result->bits = bits;
@@ -918,7 +909,7 @@ static struct gfxinfo *php_handle_iff(php_stream * stream)
918909
* int Number of columns
919910
* int Number of rows
920911
*/
921-
static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
912+
static int php_get_wbmp(php_stream *stream, struct php_gfxinfo **result, int check)
922913
{
923914
int i, width = 0, height = 0;
924915

@@ -979,9 +970,9 @@ static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
979970
/* }}} */
980971

981972
/* {{{ php_handle_wbmp */
982-
static struct gfxinfo *php_handle_wbmp(php_stream * stream)
973+
static struct php_gfxinfo *php_handle_wbmp(php_stream * stream)
983974
{
984-
struct gfxinfo *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
975+
struct php_gfxinfo *result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
985976

986977
if (!php_get_wbmp(stream, &result, 0)) {
987978
efree(result);
@@ -993,7 +984,7 @@ static struct gfxinfo *php_handle_wbmp(php_stream * stream)
993984
/* }}} */
994985

995986
/* {{{ php_get_xbm */
996-
static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
987+
static int php_get_xbm(php_stream *stream, struct php_gfxinfo **result)
997988
{
998989
char *fline;
999990
char *iname;
@@ -1040,7 +1031,7 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
10401031

10411032
if (width && height) {
10421033
if (result) {
1043-
*result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
1034+
*result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
10441035
(*result)->width = width;
10451036
(*result)->height = height;
10461037
}
@@ -1052,18 +1043,18 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
10521043
/* }}} */
10531044

10541045
/* {{{ php_handle_xbm */
1055-
static struct gfxinfo *php_handle_xbm(php_stream * stream)
1046+
static struct php_gfxinfo *php_handle_xbm(php_stream * stream)
10561047
{
1057-
struct gfxinfo *result;
1048+
struct php_gfxinfo *result;
10581049
php_get_xbm(stream, &result);
10591050
return result;
10601051
}
10611052
/* }}} */
10621053

10631054
/* {{{ php_handle_ico */
1064-
static struct gfxinfo *php_handle_ico(php_stream * stream)
1055+
static struct php_gfxinfo *php_handle_ico(php_stream * stream)
10651056
{
1066-
struct gfxinfo *result = NULL;
1057+
struct php_gfxinfo *result = NULL;
10671058
unsigned char dim[16];
10681059
int num_icons = 0;
10691060

@@ -1075,7 +1066,7 @@ static struct gfxinfo *php_handle_ico(php_stream * stream)
10751066
if (num_icons < 1 || num_icons > 255)
10761067
return NULL;
10771068

1078-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
1069+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
10791070

10801071
while (num_icons > 0)
10811072
{
@@ -1102,9 +1093,9 @@ static struct gfxinfo *php_handle_ico(php_stream * stream)
11021093
/* }}} */
11031094

11041095
/* {{{ php_handle_webp */
1105-
static struct gfxinfo *php_handle_webp(php_stream * stream)
1096+
static struct php_gfxinfo *php_handle_webp(php_stream * stream)
11061097
{
1107-
struct gfxinfo *result = NULL;
1098+
struct php_gfxinfo *result = NULL;
11081099
const char sig[3] = {'V', 'P', '8'};
11091100
unsigned char buf[18];
11101101
char format;
@@ -1125,7 +1116,7 @@ static struct gfxinfo *php_handle_webp(php_stream * stream)
11251116
return NULL;
11261117
}
11271118

1128-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
1119+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
11291120

11301121
switch (format) {
11311122
case ' ':
@@ -1187,14 +1178,14 @@ static void php_avif_stream_skip(void* stream, size_t num_bytes) {
11871178
* declared as invalid. Around 450 bytes are usually enough.
11881179
* Transforms such as mirror and rotation are not applied on width and height.
11891180
*/
1190-
static struct gfxinfo *php_handle_avif(php_stream * stream) {
1191-
struct gfxinfo* result = NULL;
1181+
static struct php_gfxinfo *php_handle_avif(php_stream * stream) {
1182+
struct php_gfxinfo* result = NULL;
11921183
AvifInfoFeatures features;
11931184
struct php_avif_stream avif_stream;
11941185
avif_stream.stream = stream;
11951186

11961187
if (AvifInfoGetFeaturesStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip, &features) == kAvifInfoOk) {
1197-
result = (struct gfxinfo*)ecalloc(1, sizeof(struct gfxinfo));
1188+
result = (struct php_gfxinfo*)ecalloc(1, sizeof(struct php_gfxinfo));
11981189
result->width = features.width;
11991190
result->height = features.height;
12001191
result->bits = features.bit_depth;
@@ -1463,7 +1454,7 @@ PHPAPI int php_getimagetype(php_stream *stream, const char *input, char *filetyp
14631454
static void php_getimagesize_from_stream(php_stream *stream, char *input, zval *info, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
14641455
{
14651456
int itype = 0;
1466-
struct gfxinfo *result = NULL;
1457+
struct php_gfxinfo *result = NULL;
14671458

14681459
if (!stream) {
14691460
RETURN_FALSE;

ext/standard/php_image.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ PHPAPI char * php_image_type_to_mime_type(int image_type);
5656

5757
PHPAPI bool php_is_image_avif(php_stream *stream);
5858

59+
/* return info as a struct, to make expansion easier */
60+
struct php_gfxinfo {
61+
unsigned int width;
62+
unsigned int height;
63+
zend_string *width_str;
64+
zend_string *height_str;
65+
unsigned int bits;
66+
unsigned int channels;
67+
};
68+
5969
#endif /* PHP_IMAGE_H */

0 commit comments

Comments
 (0)