This source file includes following definitions.
- PHP_MINIT_FUNCTION
- php_handle_gif
- php_handle_psd
- php_handle_bmp
- php_swf_get_bits
- php_handle_swc
- php_handle_swf
- php_handle_png
- php_read2
- php_next_marker
- php_skip_variable
- php_read_APP
- php_handle_jpeg
- php_read4
- php_handle_jpc
- php_handle_jp2
- php_ifd_get16u
- php_ifd_get16s
- php_ifd_get32s
- php_ifd_get32u
- php_handle_tiff
- php_handle_iff
- php_get_wbmp
- php_handle_wbmp
- php_get_xbm
- php_handle_xbm
- php_handle_ico
- php_image_type_to_mime_type
- PHP_FUNCTION
- PHP_FUNCTION
- php_getimagetype
- php_getimagesize_from_stream
- php_getimagesize_from_any
- PHP_FUNCTION
- PHP_FUNCTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include "php.h"
23 #include <stdio.h>
24 #if HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif
27 #include "fopen_wrappers.h"
28 #include "ext/standard/fsock.h"
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include "php_image.h"
33 #ifdef PHP_WIN32
34 #include "win32/php_stdint.h"
35 #endif
36
37 #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
38 #include "zlib.h"
39 #endif
40
41
42 PHPAPI const char php_sig_gif[3] = {'G', 'I', 'F'};
43 PHPAPI const char php_sig_psd[4] = {'8', 'B', 'P', 'S'};
44 PHPAPI const char php_sig_bmp[2] = {'B', 'M'};
45 PHPAPI const char php_sig_swf[3] = {'F', 'W', 'S'};
46 PHPAPI const char php_sig_swc[3] = {'C', 'W', 'S'};
47 PHPAPI const char php_sig_jpg[3] = {(char) 0xff, (char) 0xd8, (char) 0xff};
48 PHPAPI const char php_sig_png[8] = {(char) 0x89, (char) 0x50, (char) 0x4e, (char) 0x47,
49 (char) 0x0d, (char) 0x0a, (char) 0x1a, (char) 0x0a};
50 PHPAPI const char php_sig_tif_ii[4] = {'I','I', (char)0x2A, (char)0x00};
51 PHPAPI const char php_sig_tif_mm[4] = {'M','M', (char)0x00, (char)0x2A};
52 PHPAPI const char php_sig_jpc[3] = {(char)0xff, (char)0x4f, (char)0xff};
53 PHPAPI const char php_sig_jp2[12] = {(char)0x00, (char)0x00, (char)0x00, (char)0x0c,
54 (char)0x6a, (char)0x50, (char)0x20, (char)0x20,
55 (char)0x0d, (char)0x0a, (char)0x87, (char)0x0a};
56 PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
57 PHPAPI const char php_sig_ico[4] = {(char)0x00, (char)0x00, (char)0x01, (char)0x00};
58
59
60
61
62
63
64 struct gfxinfo {
65 unsigned int width;
66 unsigned int height;
67 unsigned int bits;
68 unsigned int channels;
69 };
70
71
72
73 PHP_MINIT_FUNCTION(imagetypes)
74 {
75 REGISTER_LONG_CONSTANT("IMAGETYPE_GIF", IMAGE_FILETYPE_GIF, CONST_CS | CONST_PERSISTENT);
76 REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG", IMAGE_FILETYPE_JPEG, CONST_CS | CONST_PERSISTENT);
77 REGISTER_LONG_CONSTANT("IMAGETYPE_PNG", IMAGE_FILETYPE_PNG, CONST_CS | CONST_PERSISTENT);
78 REGISTER_LONG_CONSTANT("IMAGETYPE_SWF", IMAGE_FILETYPE_SWF, CONST_CS | CONST_PERSISTENT);
79 REGISTER_LONG_CONSTANT("IMAGETYPE_PSD", IMAGE_FILETYPE_PSD, CONST_CS | CONST_PERSISTENT);
80 REGISTER_LONG_CONSTANT("IMAGETYPE_BMP", IMAGE_FILETYPE_BMP, CONST_CS | CONST_PERSISTENT);
81 REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_II", IMAGE_FILETYPE_TIFF_II, CONST_CS | CONST_PERSISTENT);
82 REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_MM", IMAGE_FILETYPE_TIFF_MM, CONST_CS | CONST_PERSISTENT);
83 REGISTER_LONG_CONSTANT("IMAGETYPE_JPC", IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT);
84 REGISTER_LONG_CONSTANT("IMAGETYPE_JP2", IMAGE_FILETYPE_JP2, CONST_CS | CONST_PERSISTENT);
85 REGISTER_LONG_CONSTANT("IMAGETYPE_JPX", IMAGE_FILETYPE_JPX, CONST_CS | CONST_PERSISTENT);
86 REGISTER_LONG_CONSTANT("IMAGETYPE_JB2", IMAGE_FILETYPE_JB2, CONST_CS | CONST_PERSISTENT);
87 #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
88 REGISTER_LONG_CONSTANT("IMAGETYPE_SWC", IMAGE_FILETYPE_SWC, CONST_CS | CONST_PERSISTENT);
89 #endif
90 REGISTER_LONG_CONSTANT("IMAGETYPE_IFF", IMAGE_FILETYPE_IFF, CONST_CS | CONST_PERSISTENT);
91 REGISTER_LONG_CONSTANT("IMAGETYPE_WBMP", IMAGE_FILETYPE_WBMP, CONST_CS | CONST_PERSISTENT);
92 REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG2000",IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT);
93 REGISTER_LONG_CONSTANT("IMAGETYPE_XBM", IMAGE_FILETYPE_XBM, CONST_CS | CONST_PERSISTENT);
94 REGISTER_LONG_CONSTANT("IMAGETYPE_ICO", IMAGE_FILETYPE_ICO, CONST_CS | CONST_PERSISTENT);
95 REGISTER_LONG_CONSTANT("IMAGETYPE_UNKNOWN", IMAGE_FILETYPE_UNKNOWN, CONST_CS | CONST_PERSISTENT);
96 REGISTER_LONG_CONSTANT("IMAGETYPE_COUNT", IMAGE_FILETYPE_COUNT, CONST_CS | CONST_PERSISTENT);
97 return SUCCESS;
98 }
99
100
101
102
103 static struct gfxinfo *php_handle_gif (php_stream * stream TSRMLS_DC)
104 {
105 struct gfxinfo *result = NULL;
106 unsigned char dim[5];
107
108 if (php_stream_seek(stream, 3, SEEK_CUR))
109 return NULL;
110
111 if (php_stream_read(stream, dim, sizeof(dim)) != sizeof(dim))
112 return NULL;
113
114 result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
115 result->width = (unsigned int)dim[0] | (((unsigned int)dim[1])<<8);
116 result->height = (unsigned int)dim[2] | (((unsigned int)dim[3])<<8);
117 result->bits = dim[4]&0x80 ? ((((unsigned int)dim[4])&0x07) + 1) : 0;
118 result->channels = 3;
119
120 return result;
121 }
122
123
124
125
126 static struct gfxinfo *php_handle_psd (php_stream * stream TSRMLS_DC)
127 {
128 struct gfxinfo *result = NULL;
129 unsigned char dim[8];
130
131 if (php_stream_seek(stream, 11, SEEK_CUR))
132 return NULL;
133
134 if (php_stream_read(stream, dim, sizeof(dim)) != sizeof(dim))
135 return NULL;
136
137 result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
138 result->height = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
139 result->width = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
140
141 return result;
142 }
143
144
145
146
147 static struct gfxinfo *php_handle_bmp (php_stream * stream TSRMLS_DC)
148 {
149 struct gfxinfo *result = NULL;
150 unsigned char dim[16];
151 int size;
152
153 if (php_stream_seek(stream, 11, SEEK_CUR))
154 return NULL;
155
156 if (php_stream_read(stream, dim, sizeof(dim)) != sizeof(dim))
157 return NULL;
158
159 size = (((unsigned int)dim[ 3]) << 24) + (((unsigned int)dim[ 2]) << 16) + (((unsigned int)dim[ 1]) << 8) + ((unsigned int) dim[ 0]);
160 if (size == 12) {
161 result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
162 result->width = (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
163 result->height = (((unsigned int)dim[ 7]) << 8) + ((unsigned int) dim[ 6]);
164 result->bits = ((unsigned int)dim[11]);
165 } else if (size > 12 && (size <= 64 || size == 108 || size == 124)) {
166 result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
167 result->width = (((unsigned int)dim[ 7]) << 24) + (((unsigned int)dim[ 6]) << 16) + (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
168 result->height = (((unsigned int)dim[11]) << 24) + (((unsigned int)dim[10]) << 16) + (((unsigned int)dim[ 9]) << 8) + ((unsigned int) dim[ 8]);
169 result->height = abs((int32_t)result->height);
170 result->bits = (((unsigned int)dim[15]) << 8) + ((unsigned int)dim[14]);
171 } else {
172 return NULL;
173 }
174
175 return result;
176 }
177
178
179
180
181 static unsigned long int php_swf_get_bits (unsigned char* buffer, unsigned int pos, unsigned int count)
182 {
183 unsigned int loop;
184 unsigned long int result = 0;
185
186 for (loop = pos; loop < pos + count; loop++)
187 {
188 result = result +
189 ((((buffer[loop / 8]) >> (7 - (loop % 8))) & 0x01) << (count - (loop - pos) - 1));
190 }
191 return result;
192 }
193
194
195 #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
196
197
198 static struct gfxinfo *php_handle_swc(php_stream * stream TSRMLS_DC)
199 {
200 struct gfxinfo *result = NULL;
201
202 long bits;
203 unsigned char a[64];
204 unsigned long len=64, szlength;
205 int factor=1,maxfactor=16;
206 int slength, status=0;
207 char *b, *buf=NULL, *bufz=NULL;
208
209 b = ecalloc (1, len + 1);
210
211 if (php_stream_seek(stream, 5, SEEK_CUR))
212 return NULL;
213
214 if (php_stream_read(stream, a, sizeof(a)) != sizeof(a))
215 return NULL;
216
217 if (uncompress(b, &len, a, sizeof(a)) != Z_OK) {
218
219 if (php_stream_seek(stream, 8, SEEK_SET))
220 return NULL;
221
222 slength = php_stream_copy_to_mem(stream, &bufz, PHP_STREAM_COPY_ALL, 0);
223
224
225
226
227
228
229
230
231
232 do {
233 szlength=slength*(1<<factor++);
234 buf = (char *) erealloc(buf,szlength);
235 status = uncompress(buf, &szlength, bufz, slength);
236 } while ((status==Z_BUF_ERROR)&&(factor<maxfactor));
237
238 if (bufz) {
239 pefree(bufz, 0);
240 }
241
242 if (status == Z_OK) {
243 memcpy(b, buf, len);
244 }
245
246 if (buf) {
247 efree(buf);
248 }
249 }
250
251 if (!status) {
252 result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
253 bits = php_swf_get_bits (b, 0, 5);
254 result->width = (php_swf_get_bits (b, 5 + bits, bits) -
255 php_swf_get_bits (b, 5, bits)) / 20;
256 result->height = (php_swf_get_bits (b, 5 + (3 * bits), bits) -
257 php_swf_get_bits (b, 5 + (2 * bits), bits)) / 20;
258 } else {
259 result = NULL;
260 }
261
262 efree (b);
263 return result;
264 }
265
266 #endif
267
268
269
270 static struct gfxinfo *php_handle_swf (php_stream * stream TSRMLS_DC)
271 {
272 struct gfxinfo *result = NULL;
273 long bits;
274 unsigned char a[32];
275
276 if (php_stream_seek(stream, 5, SEEK_CUR))
277 return NULL;
278
279 if (php_stream_read(stream, a, sizeof(a)) != sizeof(a))
280 return NULL;
281
282 result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
283 bits = php_swf_get_bits (a, 0, 5);
284 result->width = (php_swf_get_bits (a, 5 + bits, bits) -
285 php_swf_get_bits (a, 5, bits)) / 20;
286 result->height = (php_swf_get_bits (a, 5 + (3 * bits), bits) -
287 php_swf_get_bits (a, 5 + (2 * bits), bits)) / 20;
288 result->bits = 0;
289 result->channels = 0;
290 return result;
291 }
292
293
294
295
296 static struct gfxinfo *php_handle_png (php_stream * stream TSRMLS_DC)
297 {
298 struct gfxinfo *result = NULL;
299 unsigned char dim[9];
300
301
302
303
304
305
306
307
308
309 if (php_stream_seek(stream, 8, SEEK_CUR))
310 return NULL;
311
312 if((php_stream_read(stream, dim, sizeof(dim))) < sizeof(dim))
313 return NULL;
314
315 result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
316 result->width = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
317 result->height = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
318 result->bits = (unsigned int)dim[8];
319 return result;
320 }
321
322
323
324
325
326 #define M_SOF0 0xC0
327 #define M_SOF1 0xC1
328 #define M_SOF2 0xC2
329 #define M_SOF3 0xC3
330 #define M_SOF5 0xC5
331 #define M_SOF6 0xC6
332 #define M_SOF7 0xC7
333 #define M_SOF9 0xC9
334 #define M_SOF10 0xCA
335 #define M_SOF11 0xCB
336 #define M_SOF13 0xCD
337 #define M_SOF14 0xCE
338 #define M_SOF15 0xCF
339 #define M_SOI 0xD8
340 #define M_EOI 0xD9
341 #define M_SOS 0xDA
342 #define M_APP0 0xe0
343 #define M_APP1 0xe1
344 #define M_APP2 0xe2
345 #define M_APP3 0xe3
346 #define M_APP4 0xe4
347 #define M_APP5 0xe5
348 #define M_APP6 0xe6
349 #define M_APP7 0xe7
350 #define M_APP8 0xe8
351 #define M_APP9 0xe9
352 #define M_APP10 0xea
353 #define M_APP11 0xeb
354 #define M_APP12 0xec
355 #define M_APP13 0xed
356 #define M_APP14 0xee
357 #define M_APP15 0xef
358 #define M_COM 0xFE
359
360 #define M_PSEUDO 0xFFD8
361
362
363
364 static unsigned short php_read2(php_stream * stream TSRMLS_DC)
365 {
366 unsigned char a[2];
367
368
369 if((php_stream_read(stream, a, sizeof(a))) < sizeof(a)) return 0;
370
371 return (((unsigned short)a[0]) << 8) + ((unsigned short)a[1]);
372 }
373
374
375
376
377 static unsigned int php_next_marker(php_stream * stream, int last_marker, int comment_correction, int ff_read TSRMLS_DC)
378 {
379 int a=0, marker;
380
381
382 if (last_marker==M_COM && comment_correction) {
383
384
385
386 comment_correction = 2;
387 } else {
388 last_marker = 0;
389 comment_correction = 0;
390 }
391 if (ff_read) {
392 a = 1;
393 }
394 do {
395 if ((marker = php_stream_getc(stream)) == EOF)
396 {
397 return M_EOI;
398 }
399 if (last_marker==M_COM && comment_correction>0)
400 {
401 if (marker != 0xFF)
402 {
403 marker = 0xff;
404 comment_correction--;
405 } else {
406 last_marker = M_PSEUDO;
407 }
408 }
409 a++;
410 } while (marker == 0xff);
411 if (a < 2)
412 {
413 return M_EOI;
414 }
415 if ( last_marker==M_COM && comment_correction)
416 {
417 return M_EOI;
418 }
419 return (unsigned int)marker;
420 }
421
422
423
424
425 static int php_skip_variable(php_stream * stream TSRMLS_DC)
426 {
427 off_t length = ((unsigned int)php_read2(stream TSRMLS_CC));
428
429 if (length < 2) {
430 return 0;
431 }
432 length = length - 2;
433 php_stream_seek(stream, (long)length, SEEK_CUR);
434 return 1;
435 }
436
437
438
439
440 static int php_read_APP(php_stream * stream, unsigned int marker, zval *info TSRMLS_DC)
441 {
442 unsigned short length;
443 unsigned char *buffer;
444 unsigned char markername[16];
445 zval *tmp;
446
447 length = php_read2(stream TSRMLS_CC);
448 if (length < 2) {
449 return 0;
450 }
451 length -= 2;
452
453 buffer = emalloc(length);
454
455 if (php_stream_read(stream, buffer, (long) length) <= 0) {
456 efree(buffer);
457 return 0;
458 }
459
460 snprintf(markername, sizeof(markername), "APP%d", marker - M_APP0);
461
462 if (zend_hash_find(Z_ARRVAL_P(info), markername, strlen(markername)+1, (void **) &tmp) == FAILURE) {
463
464 add_assoc_stringl(info, markername, buffer, length, 1);
465 }
466
467 efree(buffer);
468 return 1;
469 }
470
471
472
473
474 static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info TSRMLS_DC)
475 {
476 struct gfxinfo *result = NULL;
477 unsigned int marker = M_PSEUDO;
478 unsigned short length, ff_read=1;
479
480 for (;;) {
481 marker = php_next_marker(stream, marker, 1, ff_read TSRMLS_CC);
482 ff_read = 0;
483 switch (marker) {
484 case M_SOF0:
485 case M_SOF1:
486 case M_SOF2:
487 case M_SOF3:
488 case M_SOF5:
489 case M_SOF6:
490 case M_SOF7:
491 case M_SOF9:
492 case M_SOF10:
493 case M_SOF11:
494 case M_SOF13:
495 case M_SOF14:
496 case M_SOF15:
497 if (result == NULL) {
498
499 result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
500 length = php_read2(stream TSRMLS_CC);
501 result->bits = php_stream_getc(stream);
502 result->height = php_read2(stream TSRMLS_CC);
503 result->width = php_read2(stream TSRMLS_CC);
504 result->channels = php_stream_getc(stream);
505 if (!info || length < 8) {
506 return result;
507 }
508 if (php_stream_seek(stream, length - 8, SEEK_CUR)) {
509 return result;
510 }
511 } else {
512 if (!php_skip_variable(stream TSRMLS_CC)) {
513 return result;
514 }
515 }
516 break;
517
518 case M_APP0:
519 case M_APP1:
520 case M_APP2:
521 case M_APP3:
522 case M_APP4:
523 case M_APP5:
524 case M_APP6:
525 case M_APP7:
526 case M_APP8:
527 case M_APP9:
528 case M_APP10:
529 case M_APP11:
530 case M_APP12:
531 case M_APP13:
532 case M_APP14:
533 case M_APP15:
534 if (info) {
535 if (!php_read_APP(stream, marker, info TSRMLS_CC)) {
536 return result;
537 }
538 } else {
539 if (!php_skip_variable(stream TSRMLS_CC)) {
540 return result;
541 }
542 }
543 break;
544
545 case M_SOS:
546 case M_EOI:
547 return result;
548
549 default:
550 if (!php_skip_variable(stream TSRMLS_CC)) {
551 return result;
552 }
553 break;
554 }
555 }
556
557 return result;
558 }
559
560
561
562
563 static unsigned int php_read4(php_stream * stream TSRMLS_DC)
564 {
565 unsigned char a[4];
566
567
568 if ((php_stream_read(stream, a, sizeof(a))) != sizeof(a)) return 0;
569
570 return (((unsigned int)a[0]) << 24)
571 + (((unsigned int)a[1]) << 16)
572 + (((unsigned int)a[2]) << 8)
573 + (((unsigned int)a[3]));
574 }
575
576
577
578 #define JPEG2000_MARKER_PREFIX 0xFF
579 #define JPEG2000_MARKER_SOC 0x4F
580 #define JPEG2000_MARKER_SOT 0x90
581 #define JPEG2000_MARKER_SOD 0x93
582 #define JPEG2000_MARKER_EOC 0xD9
583 #define JPEG2000_MARKER_SIZ 0x51
584 #define JPEG2000_MARKER_COD 0x52
585 #define JPEG2000_MARKER_COC 0x53
586 #define JPEG2000_MARKER_RGN 0x5E
587 #define JPEG2000_MARKER_QCD 0x5C
588 #define JPEG2000_MARKER_QCC 0x5D
589 #define JPEG2000_MARKER_POC 0x5F
590 #define JPEG2000_MARKER_TLM 0x55
591 #define JPEG2000_MARKER_PLM 0x57
592 #define JPEG2000_MARKER_PLT 0x58
593 #define JPEG2000_MARKER_PPM 0x60
594 #define JPEG2000_MARKER_PPT 0x61
595 #define JPEG2000_MARKER_SOP 0x91
596 #define JPEG2000_MARKER_EPH 0x92
597 #define JPEG2000_MARKER_CRG 0x63
598 #define JPEG2000_MARKER_COM 0x64
599
600
601
602
603 static struct gfxinfo *php_handle_jpc(php_stream * stream TSRMLS_DC)
604 {
605 struct gfxinfo *result = NULL;
606 unsigned short dummy_short;
607 int highest_bit_depth, bit_depth;
608 unsigned char first_marker_id;
609 unsigned int i;
610
611
612
613
614
615
616
617
618
619 first_marker_id = php_stream_getc(stream);
620
621
622 if (first_marker_id != JPEG2000_MARKER_SIZ) {
623 php_error_docref(NULL TSRMLS_CC, E_WARNING, "JPEG2000 codestream corrupt(Expected SIZ marker not found after SOC)");
624 return NULL;
625 }
626
627 result = (struct gfxinfo *)ecalloc(1, sizeof(struct gfxinfo));
628
629 dummy_short = php_read2(stream TSRMLS_CC);
630 dummy_short = php_read2(stream TSRMLS_CC);
631 result->width = php_read4(stream TSRMLS_CC);
632 result->height = php_read4(stream TSRMLS_CC);
633
634 #if MBO_0
635 php_read4(stream TSRMLS_CC);
636 php_read4(stream TSRMLS_CC);
637 php_read4(stream TSRMLS_CC);
638 php_read4(stream TSRMLS_CC);
639 php_read4(stream TSRMLS_CC);
640 php_read4(stream TSRMLS_CC);
641 #else
642 if (php_stream_seek(stream, 24, SEEK_CUR)) {
643 efree(result);
644 return NULL;
645 }
646 #endif
647
648 result->channels = php_read2(stream TSRMLS_CC);
649 if (result->channels == 0 && php_stream_eof(stream) || result->channels > 256) {
650 efree(result);
651 return NULL;
652 }
653
654
655 highest_bit_depth = 0;
656 for (i = 0; i < result->channels; i++) {
657 bit_depth = php_stream_getc(stream);
658 bit_depth++;
659 if (bit_depth > highest_bit_depth) {
660 highest_bit_depth = bit_depth;
661 }
662
663 php_stream_getc(stream);
664 php_stream_getc(stream);
665 }
666
667 result->bits = highest_bit_depth;
668
669 return result;
670 }
671
672
673
674
675 static struct gfxinfo *php_handle_jp2(php_stream *stream TSRMLS_DC)
676 {
677 struct gfxinfo *result = NULL;
678 unsigned int box_length;
679 unsigned int box_type;
680 char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
681
682
683
684
685
686
687
688
689
690
691 for (;;)
692 {
693 box_length = php_read4(stream TSRMLS_CC);
694
695 if (php_stream_read(stream, (void *)&box_type, sizeof(box_type)) != sizeof(box_type)) {
696
697 break;
698 }
699
700 if (box_length == 1) {
701
702 return NULL;
703 }
704
705 if (!memcmp(&box_type, jp2c_box_id, 4))
706 {
707
708 php_stream_seek(stream, 3, SEEK_CUR);
709
710 result = php_handle_jpc(stream TSRMLS_CC);
711 break;
712 }
713
714
715 if ((int)box_length <= 0) {
716 break;
717 }
718
719
720 if (php_stream_seek(stream, box_length - 8, SEEK_CUR)) {
721 break;
722 }
723 }
724
725 if (result == NULL) {
726 php_error_docref(NULL TSRMLS_CC, E_WARNING, "JP2 file has no codestreams at root level");
727 }
728
729 return result;
730 }
731
732
733
734
735 PHPAPI const int php_tiff_bytes_per_format[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};
736
737
738 #define TAG_IMAGEWIDTH 0x0100
739 #define TAG_IMAGEHEIGHT 0x0101
740
741 #define TAG_COMP_IMAGEWIDTH 0xA002
742 #define TAG_COMP_IMAGEHEIGHT 0xA003
743
744 #define TAG_FMT_BYTE 1
745 #define TAG_FMT_STRING 2
746 #define TAG_FMT_USHORT 3
747 #define TAG_FMT_ULONG 4
748 #define TAG_FMT_URATIONAL 5
749 #define TAG_FMT_SBYTE 6
750 #define TAG_FMT_UNDEFINED 7
751 #define TAG_FMT_SSHORT 8
752 #define TAG_FMT_SLONG 9
753 #define TAG_FMT_SRATIONAL 10
754 #define TAG_FMT_SINGLE 11
755 #define TAG_FMT_DOUBLE 12
756
757
758
759
760 static int php_ifd_get16u(void *Short, int motorola_intel)
761 {
762 if (motorola_intel) {
763 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
764 } else {
765 return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
766 }
767 }
768
769
770
771
772 static signed short php_ifd_get16s(void *Short, int motorola_intel)
773 {
774 return (signed short)php_ifd_get16u(Short, motorola_intel);
775 }
776
777
778
779
780 static int php_ifd_get32s(void *Long, int motorola_intel)
781 {
782 if (motorola_intel) {
783 return ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16)
784 | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
785 } else {
786 return ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16)
787 | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
788 }
789 }
790
791
792
793
794 static unsigned php_ifd_get32u(void *Long, int motorola_intel)
795 {
796 return (unsigned)php_ifd_get32s(Long, motorola_intel) & 0xffffffff;
797 }
798
799
800
801
802 static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel TSRMLS_DC)
803 {
804 struct gfxinfo *result = NULL;
805 int i, num_entries;
806 unsigned char *dir_entry;
807 size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
808 int entry_tag , entry_type;
809 char *ifd_data, ifd_ptr[4];
810
811 if (php_stream_read(stream, ifd_ptr, 4) != 4)
812 return NULL;
813 ifd_addr = php_ifd_get32u(ifd_ptr, motorola_intel);
814 if (php_stream_seek(stream, ifd_addr-8, SEEK_CUR))
815 return NULL;
816 ifd_size = 2;
817 ifd_data = emalloc(ifd_size);
818 if (php_stream_read(stream, ifd_data, 2) != 2) {
819 efree(ifd_data);
820 return NULL;
821 }
822 num_entries = php_ifd_get16u(ifd_data, motorola_intel);
823 dir_size = 2 +12*num_entries +4;
824 ifd_size = dir_size;
825 ifd_data = erealloc(ifd_data,ifd_size);
826 if (php_stream_read(stream, ifd_data+2, dir_size-2) != dir_size-2) {
827 efree(ifd_data);
828 return NULL;
829 }
830
831 ifd_size = dir_size;
832 for(i=0;i<num_entries;i++) {
833 dir_entry = ifd_data+2+i*12;
834 entry_tag = php_ifd_get16u(dir_entry+0, motorola_intel);
835 entry_type = php_ifd_get16u(dir_entry+2, motorola_intel);
836 switch(entry_type) {
837 case TAG_FMT_BYTE:
838 case TAG_FMT_SBYTE:
839 entry_value = (size_t)(dir_entry[8]);
840 break;
841 case TAG_FMT_USHORT:
842 entry_value = php_ifd_get16u(dir_entry+8, motorola_intel);
843 break;
844 case TAG_FMT_SSHORT:
845 entry_value = php_ifd_get16s(dir_entry+8, motorola_intel);
846 break;
847 case TAG_FMT_ULONG:
848 entry_value = php_ifd_get32u(dir_entry+8, motorola_intel);
849 break;
850 case TAG_FMT_SLONG:
851 entry_value = php_ifd_get32s(dir_entry+8, motorola_intel);
852 break;
853 default:
854 continue;
855 }
856 switch(entry_tag) {
857 case TAG_IMAGEWIDTH:
858 case TAG_COMP_IMAGEWIDTH:
859 width = entry_value;
860 break;
861 case TAG_IMAGEHEIGHT:
862 case TAG_COMP_IMAGEHEIGHT:
863 height = entry_value;
864 break;
865 }
866 }
867 efree(ifd_data);
868 if ( width && height) {
869
870 result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
871 result->height = height;
872 result->width = width;
873 result->bits = 0;
874 result->channels = 0;
875 return result;
876 }
877 return NULL;
878 }
879
880
881
882
883 static struct gfxinfo *php_handle_iff(php_stream * stream TSRMLS_DC)
884 {
885 struct gfxinfo * result;
886 unsigned char a[10];
887 int chunkId;
888 int size;
889 short width, height, bits;
890
891 if (php_stream_read(stream, a, 8) != 8) {
892 return NULL;
893 }
894 if (strncmp(a+4, "ILBM", 4) && strncmp(a+4, "PBM ", 4)) {
895 return NULL;
896 }
897
898
899 do {
900 if (php_stream_read(stream, a, 8) != 8) {
901 return NULL;
902 }
903 chunkId = php_ifd_get32s(a+0, 1);
904 size = php_ifd_get32s(a+4, 1);
905 if (size < 0) {
906 return NULL;
907 }
908 if ((size & 1) == 1) {
909 size++;
910 }
911 if (chunkId == 0x424d4844) {
912 if (size < 9 || php_stream_read(stream, a, 9) != 9) {
913 return NULL;
914 }
915 width = php_ifd_get16s(a+0, 1);
916 height = php_ifd_get16s(a+2, 1);
917 bits = a[8] & 0xff;
918 if (width > 0 && height > 0 && bits > 0 && bits < 33) {
919 result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
920 result->width = width;
921 result->height = height;
922 result->bits = bits;
923 result->channels = 0;
924 return result;
925 }
926 } else {
927 if (php_stream_seek(stream, size, SEEK_CUR)) {
928 return NULL;
929 }
930 }
931 } while (1);
932 }
933
934
935
936
937
938
939
940
941
942
943
944 static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check TSRMLS_DC)
945 {
946 int i, width = 0, height = 0;
947
948 if (php_stream_rewind(stream)) {
949 return 0;
950 }
951
952
953 if (php_stream_getc(stream) != 0) {
954 return 0;
955 }
956
957
958 do {
959 i = php_stream_getc(stream);
960 if (i < 0) {
961 return 0;
962 }
963 } while (i & 0x80);
964
965
966 do {
967 i = php_stream_getc(stream);
968 if (i < 0) {
969 return 0;
970 }
971 width = (width << 7) | (i & 0x7f);
972
973 if (width > 2048) {
974 return 0;
975 }
976 } while (i & 0x80);
977
978
979 do {
980 i = php_stream_getc(stream);
981 if (i < 0) {
982 return 0;
983 }
984 height = (height << 7) | (i & 0x7f);
985
986 if (height > 2048) {
987 return 0;
988 }
989 } while (i & 0x80);
990
991 if (!height || !width) {
992 return 0;
993 }
994
995 if (!check) {
996 (*result)->width = width;
997 (*result)->height = height;
998 }
999
1000 return IMAGE_FILETYPE_WBMP;
1001 }
1002
1003
1004
1005
1006 static struct gfxinfo *php_handle_wbmp(php_stream * stream TSRMLS_DC)
1007 {
1008 struct gfxinfo *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
1009
1010 if (!php_get_wbmp(stream, &result, 0 TSRMLS_CC)) {
1011 efree(result);
1012 return NULL;
1013 }
1014
1015 return result;
1016 }
1017
1018
1019
1020
1021 static int php_get_xbm(php_stream *stream, struct gfxinfo **result TSRMLS_DC)
1022 {
1023 char *fline;
1024 char *iname;
1025 char *type;
1026 int value;
1027 unsigned int width = 0, height = 0;
1028
1029 if (result) {
1030 *result = NULL;
1031 }
1032 if (php_stream_rewind(stream)) {
1033 return 0;
1034 }
1035 while ((fline=php_stream_gets(stream, NULL, 0)) != NULL) {
1036 iname = estrdup(fline);
1037 if (sscanf(fline, "#define %s %d", iname, &value) == 2) {
1038 if (!(type = strrchr(iname, '_'))) {
1039 type = iname;
1040 } else {
1041 type++;
1042 }
1043
1044 if (!strcmp("width", type)) {
1045 width = (unsigned int) value;
1046 if (height) {
1047 efree(iname);
1048 break;
1049 }
1050 }
1051 if (!strcmp("height", type)) {
1052 height = (unsigned int) value;
1053 if (width) {
1054 efree(iname);
1055 break;
1056 }
1057 }
1058 }
1059 efree(fline);
1060 efree(iname);
1061 }
1062 if (fline) {
1063 efree(fline);
1064 }
1065
1066 if (width && height) {
1067 if (result) {
1068 *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
1069 (*result)->width = width;
1070 (*result)->height = height;
1071 }
1072 return IMAGE_FILETYPE_XBM;
1073 }
1074
1075 return 0;
1076 }
1077
1078
1079
1080
1081 static struct gfxinfo *php_handle_xbm(php_stream * stream TSRMLS_DC)
1082 {
1083 struct gfxinfo *result;
1084 php_get_xbm(stream, &result TSRMLS_CC);
1085 return result;
1086 }
1087
1088
1089
1090
1091 static struct gfxinfo *php_handle_ico(php_stream * stream TSRMLS_DC)
1092 {
1093 struct gfxinfo *result = NULL;
1094 unsigned char dim[16];
1095 int num_icons = 0;
1096
1097 if (php_stream_read(stream, dim, 2) != 2)
1098 return NULL;
1099
1100 num_icons = (((unsigned int)dim[1]) << 8) + ((unsigned int) dim[0]);
1101
1102 if (num_icons < 1 || num_icons > 255)
1103 return NULL;
1104
1105 result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
1106
1107 while (num_icons > 0)
1108 {
1109 if (php_stream_read(stream, dim, sizeof(dim)) != sizeof(dim))
1110 break;
1111
1112 if ((((unsigned int)dim[7]) << 8) + ((unsigned int)dim[6]) >= result->bits)
1113 {
1114 result->width = (unsigned int)dim[0];
1115 result->height = (unsigned int)dim[1];
1116 result->bits = (((unsigned int)dim[7]) << 8) + ((unsigned int)dim[6]);
1117 }
1118 num_icons--;
1119 }
1120
1121 return result;
1122 }
1123
1124
1125
1126
1127 PHPAPI char * php_image_type_to_mime_type(int image_type)
1128 {
1129 switch( image_type) {
1130 case IMAGE_FILETYPE_GIF:
1131 return "image/gif";
1132 case IMAGE_FILETYPE_JPEG:
1133 return "image/jpeg";
1134 case IMAGE_FILETYPE_PNG:
1135 return "image/png";
1136 case IMAGE_FILETYPE_SWF:
1137 case IMAGE_FILETYPE_SWC:
1138 return "application/x-shockwave-flash";
1139 case IMAGE_FILETYPE_PSD:
1140 return "image/psd";
1141 case IMAGE_FILETYPE_BMP:
1142 return "image/x-ms-bmp";
1143 case IMAGE_FILETYPE_TIFF_II:
1144 case IMAGE_FILETYPE_TIFF_MM:
1145 return "image/tiff";
1146 case IMAGE_FILETYPE_IFF:
1147 return "image/iff";
1148 case IMAGE_FILETYPE_WBMP:
1149 return "image/vnd.wap.wbmp";
1150 case IMAGE_FILETYPE_JPC:
1151 return "application/octet-stream";
1152 case IMAGE_FILETYPE_JP2:
1153 return "image/jp2";
1154 case IMAGE_FILETYPE_XBM:
1155 return "image/xbm";
1156 case IMAGE_FILETYPE_ICO:
1157 return "image/vnd.microsoft.icon";
1158 default:
1159 case IMAGE_FILETYPE_UNKNOWN:
1160 return "application/octet-stream";
1161 }
1162 }
1163
1164
1165
1166
1167 PHP_FUNCTION(image_type_to_mime_type)
1168 {
1169 long p_image_type;
1170
1171 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &p_image_type) == FAILURE) {
1172 return;
1173 }
1174
1175 ZVAL_STRING(return_value, (char*)php_image_type_to_mime_type(p_image_type), 1);
1176 }
1177
1178
1179
1180
1181 PHP_FUNCTION(image_type_to_extension)
1182 {
1183 long image_type;
1184 zend_bool inc_dot=1;
1185
1186 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &image_type, &inc_dot) == FAILURE) {
1187 RETURN_FALSE;
1188 }
1189
1190 switch (image_type) {
1191 case IMAGE_FILETYPE_GIF:
1192 RETURN_STRING(".gif" + !inc_dot, 1);
1193 case IMAGE_FILETYPE_JPEG:
1194 RETURN_STRING(".jpeg" + !inc_dot, 1);
1195 case IMAGE_FILETYPE_PNG:
1196 RETURN_STRING(".png" + !inc_dot, 1);
1197 case IMAGE_FILETYPE_SWF:
1198 case IMAGE_FILETYPE_SWC:
1199 RETURN_STRING(".swf" + !inc_dot, 1);
1200 case IMAGE_FILETYPE_PSD:
1201 RETURN_STRING(".psd" + !inc_dot, 1);
1202 case IMAGE_FILETYPE_BMP:
1203 case IMAGE_FILETYPE_WBMP:
1204 RETURN_STRING(".bmp" + !inc_dot, 1);
1205 case IMAGE_FILETYPE_TIFF_II:
1206 case IMAGE_FILETYPE_TIFF_MM:
1207 RETURN_STRING(".tiff" + !inc_dot, 1);
1208 case IMAGE_FILETYPE_IFF:
1209 RETURN_STRING(".iff" + !inc_dot, 1);
1210 case IMAGE_FILETYPE_JPC:
1211 RETURN_STRING(".jpc" + !inc_dot, 1);
1212 case IMAGE_FILETYPE_JP2:
1213 RETURN_STRING(".jp2" + !inc_dot, 1);
1214 case IMAGE_FILETYPE_JPX:
1215 RETURN_STRING(".jpx" + !inc_dot, 1);
1216 case IMAGE_FILETYPE_JB2:
1217 RETURN_STRING(".jb2" + !inc_dot, 1);
1218 case IMAGE_FILETYPE_XBM:
1219 RETURN_STRING(".xbm" + !inc_dot, 1);
1220 case IMAGE_FILETYPE_ICO:
1221 RETURN_STRING(".ico" + !inc_dot, 1);
1222 }
1223
1224 RETURN_FALSE;
1225 }
1226
1227
1228
1229
1230 PHPAPI int php_getimagetype(php_stream * stream, char *filetype TSRMLS_DC)
1231 {
1232 char tmp[12];
1233 int twelve_bytes_read;
1234
1235 if ( !filetype) filetype = tmp;
1236 if((php_stream_read(stream, filetype, 3)) != 3) {
1237 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Read error!");
1238 return IMAGE_FILETYPE_UNKNOWN;
1239 }
1240
1241
1242 if (!memcmp(filetype, php_sig_gif, 3)) {
1243 return IMAGE_FILETYPE_GIF;
1244 } else if (!memcmp(filetype, php_sig_jpg, 3)) {
1245 return IMAGE_FILETYPE_JPEG;
1246 } else if (!memcmp(filetype, php_sig_png, 3)) {
1247 if (php_stream_read(stream, filetype+3, 5) != 5) {
1248 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Read error!");
1249 return IMAGE_FILETYPE_UNKNOWN;
1250 }
1251 if (!memcmp(filetype, php_sig_png, 8)) {
1252 return IMAGE_FILETYPE_PNG;
1253 } else {
1254 php_error_docref(NULL TSRMLS_CC, E_WARNING, "PNG file corrupted by ASCII conversion");
1255 return IMAGE_FILETYPE_UNKNOWN;
1256 }
1257 } else if (!memcmp(filetype, php_sig_swf, 3)) {
1258 return IMAGE_FILETYPE_SWF;
1259 } else if (!memcmp(filetype, php_sig_swc, 3)) {
1260 return IMAGE_FILETYPE_SWC;
1261 } else if (!memcmp(filetype, php_sig_psd, 3)) {
1262 return IMAGE_FILETYPE_PSD;
1263 } else if (!memcmp(filetype, php_sig_bmp, 2)) {
1264 return IMAGE_FILETYPE_BMP;
1265 } else if (!memcmp(filetype, php_sig_jpc, 3)) {
1266 return IMAGE_FILETYPE_JPC;
1267 }
1268
1269 if (php_stream_read(stream, filetype+3, 1) != 1) {
1270 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Read error!");
1271 return IMAGE_FILETYPE_UNKNOWN;
1272 }
1273
1274 if (!memcmp(filetype, php_sig_tif_ii, 4)) {
1275 return IMAGE_FILETYPE_TIFF_II;
1276 } else if (!memcmp(filetype, php_sig_tif_mm, 4)) {
1277 return IMAGE_FILETYPE_TIFF_MM;
1278 } else if (!memcmp(filetype, php_sig_iff, 4)) {
1279 return IMAGE_FILETYPE_IFF;
1280 } else if (!memcmp(filetype, php_sig_ico, 4)) {
1281 return IMAGE_FILETYPE_ICO;
1282 }
1283
1284
1285 twelve_bytes_read = (php_stream_read(stream, filetype+4, 8) == 8);
1286
1287
1288 if (twelve_bytes_read && !memcmp(filetype, php_sig_jp2, 12)) {
1289 return IMAGE_FILETYPE_JP2;
1290 }
1291
1292
1293 if (php_get_wbmp(stream, NULL, 1 TSRMLS_CC)) {
1294 return IMAGE_FILETYPE_WBMP;
1295 }
1296 if (!twelve_bytes_read) {
1297 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Read error!");
1298 return IMAGE_FILETYPE_UNKNOWN;
1299 }
1300 if (php_get_xbm(stream, NULL TSRMLS_CC)) {
1301 return IMAGE_FILETYPE_XBM;
1302 }
1303 return IMAGE_FILETYPE_UNKNOWN;
1304 }
1305
1306
1307 static void php_getimagesize_from_stream(php_stream *stream, zval **info, INTERNAL_FUNCTION_PARAMETERS)
1308 {
1309 char *temp;
1310 int itype = 0;
1311 struct gfxinfo *result = NULL;
1312
1313 if (!stream) {
1314 RETURN_FALSE;
1315 }
1316
1317 itype = php_getimagetype(stream, NULL TSRMLS_CC);
1318 switch( itype) {
1319 case IMAGE_FILETYPE_GIF:
1320 result = php_handle_gif(stream TSRMLS_CC);
1321 break;
1322 case IMAGE_FILETYPE_JPEG:
1323 if (info) {
1324 result = php_handle_jpeg(stream, *info TSRMLS_CC);
1325 } else {
1326 result = php_handle_jpeg(stream, NULL TSRMLS_CC);
1327 }
1328 break;
1329 case IMAGE_FILETYPE_PNG:
1330 result = php_handle_png(stream TSRMLS_CC);
1331 break;
1332 case IMAGE_FILETYPE_SWF:
1333 result = php_handle_swf(stream TSRMLS_CC);
1334 break;
1335 case IMAGE_FILETYPE_SWC:
1336 #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
1337 result = php_handle_swc(stream TSRMLS_CC);
1338 #else
1339 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "The image is a compressed SWF file, but you do not have a static version of the zlib extension enabled");
1340 #endif
1341 break;
1342 case IMAGE_FILETYPE_PSD:
1343 result = php_handle_psd(stream TSRMLS_CC);
1344 break;
1345 case IMAGE_FILETYPE_BMP:
1346 result = php_handle_bmp(stream TSRMLS_CC);
1347 break;
1348 case IMAGE_FILETYPE_TIFF_II:
1349 result = php_handle_tiff(stream, NULL, 0 TSRMLS_CC);
1350 break;
1351 case IMAGE_FILETYPE_TIFF_MM:
1352 result = php_handle_tiff(stream, NULL, 1 TSRMLS_CC);
1353 break;
1354 case IMAGE_FILETYPE_JPC:
1355 result = php_handle_jpc(stream TSRMLS_CC);
1356 break;
1357 case IMAGE_FILETYPE_JP2:
1358 result = php_handle_jp2(stream TSRMLS_CC);
1359 break;
1360 case IMAGE_FILETYPE_IFF:
1361 result = php_handle_iff(stream TSRMLS_CC);
1362 break;
1363 case IMAGE_FILETYPE_WBMP:
1364 result = php_handle_wbmp(stream TSRMLS_CC);
1365 break;
1366 case IMAGE_FILETYPE_XBM:
1367 result = php_handle_xbm(stream TSRMLS_CC);
1368 break;
1369 case IMAGE_FILETYPE_ICO:
1370 result = php_handle_ico(stream TSRMLS_CC);
1371 break;
1372 default:
1373 case IMAGE_FILETYPE_UNKNOWN:
1374 break;
1375 }
1376
1377 if (result) {
1378 array_init(return_value);
1379 add_index_long(return_value, 0, result->width);
1380 add_index_long(return_value, 1, result->height);
1381 add_index_long(return_value, 2, itype);
1382 spprintf(&temp, 0, "width=\"%d\" height=\"%d\"", result->width, result->height);
1383 add_index_string(return_value, 3, temp, 0);
1384
1385 if (result->bits != 0) {
1386 add_assoc_long(return_value, "bits", result->bits);
1387 }
1388 if (result->channels != 0) {
1389 add_assoc_long(return_value, "channels", result->channels);
1390 }
1391 add_assoc_string(return_value, "mime", (char*)php_image_type_to_mime_type(itype), 1);
1392 efree(result);
1393 } else {
1394 RETURN_FALSE;
1395 }
1396 }
1397
1398
1399 #define FROM_DATA 0
1400 #define FROM_PATH 1
1401
1402 static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) {
1403 zval **info = NULL;
1404 php_stream *stream = NULL;
1405 char *input;
1406 int input_len;
1407 const int argc = ZEND_NUM_ARGS();
1408
1409 if (zend_parse_parameters(argc TSRMLS_CC, "s|Z", &input, &input_len, &info) == FAILURE) {
1410 return;
1411 }
1412
1413 if (argc == 2) {
1414 zval_dtor(*info);
1415 array_init(*info);
1416 }
1417
1418
1419 if (mode == FROM_PATH) {
1420 stream = php_stream_open_wrapper(input, "rb", STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH, NULL);
1421 } else {
1422 stream = php_stream_memory_open(TEMP_STREAM_READONLY, input, input_len);
1423 }
1424
1425 if (!stream) {
1426 RETURN_FALSE;
1427 }
1428
1429 php_getimagesize_from_stream(stream, info, INTERNAL_FUNCTION_PARAM_PASSTHRU);
1430 php_stream_close(stream);
1431 }
1432
1433
1434
1435
1436 PHP_FUNCTION(getimagesize)
1437 {
1438 php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_PATH);
1439 }
1440
1441
1442
1443
1444 PHP_FUNCTION(getimagesizefromstring)
1445 {
1446 php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_DATA);
1447 }
1448
1449
1450
1451
1452
1453
1454
1455
1456