Skip to content

Commit 3706484

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 063de1f commit 3706484

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
@@ -55,20 +55,11 @@ PHPAPI const char php_sig_webp[4] = {'W', 'E', 'B', 'P'};
5555
/* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
5656
/* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
5757

58-
/* return info as a struct, to make expansion easier */
59-
60-
struct gfxinfo {
61-
unsigned int width;
62-
unsigned int height;
63-
unsigned int bits;
64-
unsigned int channels;
65-
};
66-
6758
/* {{{ php_handle_gif
6859
* routine to handle GIF files. If only everything were that easy... ;} */
69-
static struct gfxinfo *php_handle_gif (php_stream * stream)
60+
static struct php_gfxinfo *php_handle_gif (php_stream * stream)
7061
{
71-
struct gfxinfo *result = NULL;
62+
struct php_gfxinfo *result = NULL;
7263
unsigned char dim[5];
7364

7465
if (php_stream_seek(stream, 3, SEEK_CUR))
@@ -77,7 +68,7 @@ static struct gfxinfo *php_handle_gif (php_stream * stream)
7768
if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
7869
return NULL;
7970

80-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
71+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
8172
result->width = (unsigned int)dim[0] | (((unsigned int)dim[1])<<8);
8273
result->height = (unsigned int)dim[2] | (((unsigned int)dim[3])<<8);
8374
result->bits = dim[4]&0x80 ? ((((unsigned int)dim[4])&0x07) + 1) : 0;
@@ -88,9 +79,9 @@ static struct gfxinfo *php_handle_gif (php_stream * stream)
8879
/* }}} */
8980

9081
/* {{{ php_handle_psd */
91-
static struct gfxinfo *php_handle_psd (php_stream * stream)
82+
static struct php_gfxinfo *php_handle_psd (php_stream * stream)
9283
{
93-
struct gfxinfo *result = NULL;
84+
struct php_gfxinfo *result = NULL;
9485
unsigned char dim[8];
9586

9687
if (php_stream_seek(stream, 11, SEEK_CUR))
@@ -99,7 +90,7 @@ static struct gfxinfo *php_handle_psd (php_stream * stream)
9990
if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
10091
return NULL;
10192

102-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
93+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
10394
result->height = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
10495
result->width = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
10596

@@ -108,9 +99,9 @@ static struct gfxinfo *php_handle_psd (php_stream * stream)
10899
/* }}} */
109100

110101
/* {{{ php_handle_bmp */
111-
static struct gfxinfo *php_handle_bmp (php_stream * stream)
102+
static struct php_gfxinfo *php_handle_bmp (php_stream * stream)
112103
{
113-
struct gfxinfo *result = NULL;
104+
struct php_gfxinfo *result = NULL;
114105
unsigned char dim[16];
115106
int size;
116107

@@ -122,12 +113,12 @@ static struct gfxinfo *php_handle_bmp (php_stream * stream)
122113

123114
size = (((unsigned int)dim[ 3]) << 24) + (((unsigned int)dim[ 2]) << 16) + (((unsigned int)dim[ 1]) << 8) + ((unsigned int) dim[ 0]);
124115
if (size == 12) {
125-
result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
116+
result = (struct php_gfxinfo *) ecalloc (1, sizeof(struct php_gfxinfo));
126117
result->width = (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
127118
result->height = (((unsigned int)dim[ 7]) << 8) + ((unsigned int) dim[ 6]);
128119
result->bits = ((unsigned int)dim[11]);
129120
} else if (size > 12 && (size <= 64 || size == 108 || size == 124)) {
130-
result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
121+
result = (struct php_gfxinfo *) ecalloc (1, sizeof(struct php_gfxinfo));
131122
result->width = (((unsigned int)dim[ 7]) << 24) + (((unsigned int)dim[ 6]) << 16) + (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
132123
result->height = (((unsigned int)dim[11]) << 24) + (((unsigned int)dim[10]) << 16) + (((unsigned int)dim[ 9]) << 8) + ((unsigned int) dim[ 8]);
133124
result->height = abs((int32_t)result->height);
@@ -158,9 +149,9 @@ static unsigned long int php_swf_get_bits (unsigned char* buffer, unsigned int p
158149

159150
#if defined(HAVE_ZLIB) && !defined(COMPILE_DL_ZLIB)
160151
/* {{{ php_handle_swc */
161-
static struct gfxinfo *php_handle_swc(php_stream * stream)
152+
static struct php_gfxinfo *php_handle_swc(php_stream * stream)
162153
{
163-
struct gfxinfo *result = NULL;
154+
struct php_gfxinfo *result = NULL;
164155

165156
long bits;
166157
unsigned char a[64];
@@ -222,7 +213,7 @@ static struct gfxinfo *php_handle_swc(php_stream * stream)
222213
}
223214

224215
if (!status) {
225-
result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
216+
result = (struct php_gfxinfo *) ecalloc (1, sizeof (struct php_gfxinfo));
226217
bits = php_swf_get_bits (b, 0, 5);
227218
result->width = (php_swf_get_bits (b, 5 + bits, bits) -
228219
php_swf_get_bits (b, 5, bits)) / 20;
@@ -239,9 +230,9 @@ static struct gfxinfo *php_handle_swc(php_stream * stream)
239230
#endif
240231

241232
/* {{{ php_handle_swf */
242-
static struct gfxinfo *php_handle_swf (php_stream * stream)
233+
static struct php_gfxinfo *php_handle_swf (php_stream * stream)
243234
{
244-
struct gfxinfo *result = NULL;
235+
struct php_gfxinfo *result = NULL;
245236
long bits;
246237
unsigned char a[32];
247238

@@ -251,7 +242,7 @@ static struct gfxinfo *php_handle_swf (php_stream * stream)
251242
if (php_stream_read(stream, (char*)a, sizeof(a)) != sizeof(a))
252243
return NULL;
253244

254-
result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
245+
result = (struct php_gfxinfo *) ecalloc (1, sizeof (struct php_gfxinfo));
255246
bits = php_swf_get_bits (a, 0, 5);
256247
result->width = (php_swf_get_bits (a, 5 + bits, bits) -
257248
php_swf_get_bits (a, 5, bits)) / 20;
@@ -265,9 +256,9 @@ static struct gfxinfo *php_handle_swf (php_stream * stream)
265256

266257
/* {{{ php_handle_png
267258
* routine to handle PNG files */
268-
static struct gfxinfo *php_handle_png (php_stream * stream)
259+
static struct php_gfxinfo *php_handle_png (php_stream * stream)
269260
{
270-
struct gfxinfo *result = NULL;
261+
struct php_gfxinfo *result = NULL;
271262
unsigned char dim[9];
272263
/* Width: 4 bytes
273264
* Height: 4 bytes
@@ -284,7 +275,7 @@ static struct gfxinfo *php_handle_png (php_stream * stream)
284275
if((php_stream_read(stream, (char*)dim, sizeof(dim))) < sizeof(dim))
285276
return NULL;
286277

287-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
278+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
288279
result->width = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
289280
result->height = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
290281
result->bits = (unsigned int)dim[8];
@@ -443,9 +434,9 @@ static int php_read_APP(php_stream * stream, unsigned int marker, zval *info)
443434

444435
/* {{{ php_handle_jpeg
445436
main loop to parse JPEG structure */
446-
static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
437+
static struct php_gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
447438
{
448-
struct gfxinfo *result = NULL;
439+
struct php_gfxinfo *result = NULL;
449440
unsigned int marker = M_PSEUDO;
450441
unsigned short length, ff_read=1;
451442

@@ -468,7 +459,7 @@ static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
468459
case M_SOF15:
469460
if (result == NULL) {
470461
/* handle SOFn block */
471-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
462+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
472463
length = php_read2(stream);
473464
result->bits = php_stream_getc(stream);
474465
result->height = php_read2(stream);
@@ -571,9 +562,9 @@ static unsigned int php_read4(php_stream * stream)
571562

572563
/* {{{ php_handle_jpc
573564
Main loop to parse JPEG2000 raw codestream structure */
574-
static struct gfxinfo *php_handle_jpc(php_stream * stream)
565+
static struct php_gfxinfo *php_handle_jpc(php_stream * stream)
575566
{
576-
struct gfxinfo *result = NULL;
567+
struct php_gfxinfo *result = NULL;
577568
int highest_bit_depth, bit_depth;
578569
unsigned char first_marker_id;
579570
unsigned int i;
@@ -594,7 +585,7 @@ static struct gfxinfo *php_handle_jpc(php_stream * stream)
594585
return NULL;
595586
}
596587

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

599590
php_read2(stream); /* Lsiz */
600591
php_read2(stream); /* Rsiz */
@@ -642,9 +633,9 @@ static struct gfxinfo *php_handle_jpc(php_stream * stream)
642633

643634
/* {{{ php_handle_jp2
644635
main loop to parse JPEG 2000 JP2 wrapper format structure */
645-
static struct gfxinfo *php_handle_jp2(php_stream *stream)
636+
static struct php_gfxinfo *php_handle_jp2(php_stream *stream)
646637
{
647-
struct gfxinfo *result = NULL;
638+
struct php_gfxinfo *result = NULL;
648639
unsigned int box_length;
649640
unsigned int box_type;
650641
char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
@@ -768,9 +759,9 @@ static unsigned php_ifd_get32u(void *Long, int motorola_intel)
768759

769760
/* {{{ php_handle_tiff
770761
main loop to parse TIFF structure */
771-
static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
762+
static struct php_gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
772763
{
773-
struct gfxinfo *result = NULL;
764+
struct php_gfxinfo *result = NULL;
774765
int i, num_entries;
775766
unsigned char *dir_entry;
776767
size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
@@ -836,7 +827,7 @@ static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int mot
836827
efree(ifd_data);
837828
if ( width && height) {
838829
/* not the same when in for-loop */
839-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
830+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
840831
result->height = height;
841832
result->width = width;
842833
result->bits = 0;
@@ -848,9 +839,9 @@ static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int mot
848839
/* }}} */
849840

850841
/* {{{ php_handle_psd */
851-
static struct gfxinfo *php_handle_iff(php_stream * stream)
842+
static struct php_gfxinfo *php_handle_iff(php_stream * stream)
852843
{
853-
struct gfxinfo * result;
844+
struct php_gfxinfo * result;
854845
unsigned char a[10];
855846
int chunkId;
856847
int size;
@@ -884,7 +875,7 @@ static struct gfxinfo *php_handle_iff(php_stream * stream)
884875
height = php_ifd_get16s(a+2, 1);
885876
bits = a[8] & 0xff;
886877
if (width > 0 && height > 0 && bits > 0 && bits < 33) {
887-
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
878+
result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
888879
result->width = width;
889880
result->height = height;
890881
result->bits = bits;
@@ -909,7 +900,7 @@ static struct gfxinfo *php_handle_iff(php_stream * stream)
909900
* int Number of columns
910901
* int Number of rows
911902
*/
912-
static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
903+
static int php_get_wbmp(php_stream *stream, struct php_gfxinfo **result, int check)
913904
{
914905
int i, width = 0, height = 0;
915906

@@ -970,9 +961,9 @@ static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
970961
/* }}} */
971962

972963
/* {{{ php_handle_wbmp */
973-
static struct gfxinfo *php_handle_wbmp(php_stream * stream)
964+
static struct php_gfxinfo *php_handle_wbmp(php_stream * stream)
974965
{
975-
struct gfxinfo *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
966+
struct php_gfxinfo *result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
976967

977968
if (!php_get_wbmp(stream, &result, 0)) {
978969
efree(result);
@@ -984,7 +975,7 @@ static struct gfxinfo *php_handle_wbmp(php_stream * stream)
984975
/* }}} */
985976

986977
/* {{{ php_get_xbm */
987-
static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
978+
static int php_get_xbm(php_stream *stream, struct php_gfxinfo **result)
988979
{
989980
char *fline;
990981
char *iname;
@@ -1031,7 +1022,7 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
10311022

10321023
if (width && height) {
10331024
if (result) {
1034-
*result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
1025+
*result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
10351026
(*result)->width = width;
10361027
(*result)->height = height;
10371028
}
@@ -1043,18 +1034,18 @@ static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
10431034
/* }}} */
10441035

10451036
/* {{{ php_handle_xbm */
1046-
static struct gfxinfo *php_handle_xbm(php_stream * stream)
1037+
static struct php_gfxinfo *php_handle_xbm(php_stream * stream)
10471038
{
1048-
struct gfxinfo *result;
1039+
struct php_gfxinfo *result;
10491040
php_get_xbm(stream, &result);
10501041
return result;
10511042
}
10521043
/* }}} */
10531044

10541045
/* {{{ php_handle_ico */
1055-
static struct gfxinfo *php_handle_ico(php_stream * stream)
1046+
static struct php_gfxinfo *php_handle_ico(php_stream * stream)
10561047
{
1057-
struct gfxinfo *result = NULL;
1048+
struct php_gfxinfo *result = NULL;
10581049
unsigned char dim[16];
10591050
int num_icons = 0;
10601051

@@ -1066,7 +1057,7 @@ static struct gfxinfo *php_handle_ico(php_stream * stream)
10661057
if (num_icons < 1 || num_icons > 255)
10671058
return NULL;
10681059

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

10711062
while (num_icons > 0)
10721063
{
@@ -1093,9 +1084,9 @@ static struct gfxinfo *php_handle_ico(php_stream * stream)
10931084
/* }}} */
10941085

10951086
/* {{{ php_handle_webp */
1096-
static struct gfxinfo *php_handle_webp(php_stream * stream)
1087+
static struct php_gfxinfo *php_handle_webp(php_stream * stream)
10971088
{
1098-
struct gfxinfo *result = NULL;
1089+
struct php_gfxinfo *result = NULL;
10991090
const char sig[3] = {'V', 'P', '8'};
11001091
unsigned char buf[18];
11011092
char format;
@@ -1116,7 +1107,7 @@ static struct gfxinfo *php_handle_webp(php_stream * stream)
11161107
return NULL;
11171108
}
11181109

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

11211112
switch (format) {
11221113
case ' ':
@@ -1178,14 +1169,14 @@ static void php_avif_stream_skip(void* stream, size_t num_bytes) {
11781169
* declared as invalid. Around 450 bytes are usually enough.
11791170
* Transforms such as mirror and rotation are not applied on width and height.
11801171
*/
1181-
static struct gfxinfo *php_handle_avif(php_stream * stream) {
1182-
struct gfxinfo* result = NULL;
1172+
static struct php_gfxinfo *php_handle_avif(php_stream * stream) {
1173+
struct php_gfxinfo* result = NULL;
11831174
AvifInfoFeatures features;
11841175
struct php_avif_stream avif_stream;
11851176
avif_stream.stream = stream;
11861177

11871178
if (AvifInfoGetFeaturesStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip, &features) == kAvifInfoOk) {
1188-
result = (struct gfxinfo*)ecalloc(1, sizeof(struct gfxinfo));
1179+
result = (struct php_gfxinfo*)ecalloc(1, sizeof(struct php_gfxinfo));
11891180
result->width = features.width;
11901181
result->height = features.height;
11911182
result->bits = features.bit_depth;
@@ -1443,7 +1434,7 @@ PHPAPI int php_getimagetype(php_stream *stream, const char *input, char *filetyp
14431434
static void php_getimagesize_from_stream(php_stream *stream, char *input, zval *info, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
14441435
{
14451436
int itype = 0;
1446-
struct gfxinfo *result = NULL;
1437+
struct php_gfxinfo *result = NULL;
14471438

14481439
if (!stream) {
14491440
RETURN_FALSE;

ext/standard/php_image.h

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

5656
PHPAPI bool php_is_image_avif(php_stream *stream);
5757

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

0 commit comments

Comments
 (0)