PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/standard/image.c

http://github.com/php/php-src
C | 1530 lines | 1161 code | 171 blank | 198 comment | 236 complexity | 7c78782bd2b7f0279cf2273640f4ce12 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  14. | Marcus Boerger <helly@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php.h"
  18. #include <stdio.h>
  19. #if HAVE_FCNTL_H
  20. #include <fcntl.h>
  21. #endif
  22. #include "fopen_wrappers.h"
  23. #include "ext/standard/fsock.h"
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #include "php_image.h"
  28. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  29. #include "zlib.h"
  30. #endif
  31. /* file type markers */
  32. PHPAPI const char php_sig_gif[3] = {'G', 'I', 'F'};
  33. PHPAPI const char php_sig_psd[4] = {'8', 'B', 'P', 'S'};
  34. PHPAPI const char php_sig_bmp[2] = {'B', 'M'};
  35. PHPAPI const char php_sig_swf[3] = {'F', 'W', 'S'};
  36. PHPAPI const char php_sig_swc[3] = {'C', 'W', 'S'};
  37. PHPAPI const char php_sig_jpg[3] = {(char) 0xff, (char) 0xd8, (char) 0xff};
  38. PHPAPI const char php_sig_png[8] = {(char) 0x89, (char) 0x50, (char) 0x4e, (char) 0x47,
  39. (char) 0x0d, (char) 0x0a, (char) 0x1a, (char) 0x0a};
  40. PHPAPI const char php_sig_tif_ii[4] = {'I','I', (char)0x2A, (char)0x00};
  41. PHPAPI const char php_sig_tif_mm[4] = {'M','M', (char)0x00, (char)0x2A};
  42. PHPAPI const char php_sig_jpc[3] = {(char)0xff, (char)0x4f, (char)0xff};
  43. PHPAPI const char php_sig_jp2[12] = {(char)0x00, (char)0x00, (char)0x00, (char)0x0c,
  44. (char)0x6a, (char)0x50, (char)0x20, (char)0x20,
  45. (char)0x0d, (char)0x0a, (char)0x87, (char)0x0a};
  46. PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
  47. PHPAPI const char php_sig_ico[4] = {(char)0x00, (char)0x00, (char)0x01, (char)0x00};
  48. PHPAPI const char php_sig_riff[4] = {'R', 'I', 'F', 'F'};
  49. PHPAPI const char php_sig_webp[4] = {'W', 'E', 'B', 'P'};
  50. /* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
  51. /* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
  52. /* return info as a struct, to make expansion easier */
  53. struct gfxinfo {
  54. unsigned int width;
  55. unsigned int height;
  56. unsigned int bits;
  57. unsigned int channels;
  58. };
  59. /* {{{ PHP_MINIT_FUNCTION(imagetypes)
  60. * Register IMAGETYPE_<xxx> constants used by GetImageSize(), image_type_to_mime_type, ext/exif */
  61. PHP_MINIT_FUNCTION(imagetypes)
  62. {
  63. REGISTER_LONG_CONSTANT("IMAGETYPE_GIF", IMAGE_FILETYPE_GIF, CONST_CS | CONST_PERSISTENT);
  64. REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG", IMAGE_FILETYPE_JPEG, CONST_CS | CONST_PERSISTENT);
  65. REGISTER_LONG_CONSTANT("IMAGETYPE_PNG", IMAGE_FILETYPE_PNG, CONST_CS | CONST_PERSISTENT);
  66. REGISTER_LONG_CONSTANT("IMAGETYPE_SWF", IMAGE_FILETYPE_SWF, CONST_CS | CONST_PERSISTENT);
  67. REGISTER_LONG_CONSTANT("IMAGETYPE_PSD", IMAGE_FILETYPE_PSD, CONST_CS | CONST_PERSISTENT);
  68. REGISTER_LONG_CONSTANT("IMAGETYPE_BMP", IMAGE_FILETYPE_BMP, CONST_CS | CONST_PERSISTENT);
  69. REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_II", IMAGE_FILETYPE_TIFF_II, CONST_CS | CONST_PERSISTENT);
  70. REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_MM", IMAGE_FILETYPE_TIFF_MM, CONST_CS | CONST_PERSISTENT);
  71. REGISTER_LONG_CONSTANT("IMAGETYPE_JPC", IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT);
  72. REGISTER_LONG_CONSTANT("IMAGETYPE_JP2", IMAGE_FILETYPE_JP2, CONST_CS | CONST_PERSISTENT);
  73. REGISTER_LONG_CONSTANT("IMAGETYPE_JPX", IMAGE_FILETYPE_JPX, CONST_CS | CONST_PERSISTENT);
  74. REGISTER_LONG_CONSTANT("IMAGETYPE_JB2", IMAGE_FILETYPE_JB2, CONST_CS | CONST_PERSISTENT);
  75. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  76. REGISTER_LONG_CONSTANT("IMAGETYPE_SWC", IMAGE_FILETYPE_SWC, CONST_CS | CONST_PERSISTENT);
  77. #endif
  78. REGISTER_LONG_CONSTANT("IMAGETYPE_IFF", IMAGE_FILETYPE_IFF, CONST_CS | CONST_PERSISTENT);
  79. REGISTER_LONG_CONSTANT("IMAGETYPE_WBMP", IMAGE_FILETYPE_WBMP, CONST_CS | CONST_PERSISTENT);
  80. REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG2000",IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT); /* keep alias */
  81. REGISTER_LONG_CONSTANT("IMAGETYPE_XBM", IMAGE_FILETYPE_XBM, CONST_CS | CONST_PERSISTENT);
  82. REGISTER_LONG_CONSTANT("IMAGETYPE_ICO", IMAGE_FILETYPE_ICO, CONST_CS | CONST_PERSISTENT);
  83. REGISTER_LONG_CONSTANT("IMAGETYPE_WEBP", IMAGE_FILETYPE_WEBP, CONST_CS | CONST_PERSISTENT);
  84. REGISTER_LONG_CONSTANT("IMAGETYPE_UNKNOWN", IMAGE_FILETYPE_UNKNOWN, CONST_CS | CONST_PERSISTENT);
  85. REGISTER_LONG_CONSTANT("IMAGETYPE_COUNT", IMAGE_FILETYPE_COUNT, CONST_CS | CONST_PERSISTENT);
  86. return SUCCESS;
  87. }
  88. /* }}} */
  89. /* {{{ php_handle_gif
  90. * routine to handle GIF files. If only everything were that easy... ;} */
  91. static struct gfxinfo *php_handle_gif (php_stream * stream)
  92. {
  93. struct gfxinfo *result = NULL;
  94. unsigned char dim[5];
  95. if (php_stream_seek(stream, 3, SEEK_CUR))
  96. return NULL;
  97. if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
  98. return NULL;
  99. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  100. result->width = (unsigned int)dim[0] | (((unsigned int)dim[1])<<8);
  101. result->height = (unsigned int)dim[2] | (((unsigned int)dim[3])<<8);
  102. result->bits = dim[4]&0x80 ? ((((unsigned int)dim[4])&0x07) + 1) : 0;
  103. result->channels = 3; /* always */
  104. return result;
  105. }
  106. /* }}} */
  107. /* {{{ php_handle_psd
  108. */
  109. static struct gfxinfo *php_handle_psd (php_stream * stream)
  110. {
  111. struct gfxinfo *result = NULL;
  112. unsigned char dim[8];
  113. if (php_stream_seek(stream, 11, SEEK_CUR))
  114. return NULL;
  115. if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
  116. return NULL;
  117. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  118. result->height = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
  119. result->width = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
  120. return result;
  121. }
  122. /* }}} */
  123. /* {{{ php_handle_bmp
  124. */
  125. static struct gfxinfo *php_handle_bmp (php_stream * stream)
  126. {
  127. struct gfxinfo *result = NULL;
  128. unsigned char dim[16];
  129. int size;
  130. if (php_stream_seek(stream, 11, SEEK_CUR))
  131. return NULL;
  132. if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
  133. return NULL;
  134. size = (((unsigned int)dim[ 3]) << 24) + (((unsigned int)dim[ 2]) << 16) + (((unsigned int)dim[ 1]) << 8) + ((unsigned int) dim[ 0]);
  135. if (size == 12) {
  136. result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
  137. result->width = (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
  138. result->height = (((unsigned int)dim[ 7]) << 8) + ((unsigned int) dim[ 6]);
  139. result->bits = ((unsigned int)dim[11]);
  140. } else if (size > 12 && (size <= 64 || size == 108 || size == 124)) {
  141. result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
  142. result->width = (((unsigned int)dim[ 7]) << 24) + (((unsigned int)dim[ 6]) << 16) + (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
  143. result->height = (((unsigned int)dim[11]) << 24) + (((unsigned int)dim[10]) << 16) + (((unsigned int)dim[ 9]) << 8) + ((unsigned int) dim[ 8]);
  144. result->height = abs((int32_t)result->height);
  145. result->bits = (((unsigned int)dim[15]) << 8) + ((unsigned int)dim[14]);
  146. } else {
  147. return NULL;
  148. }
  149. return result;
  150. }
  151. /* }}} */
  152. /* {{{ php_swf_get_bits
  153. * routines to handle SWF files. */
  154. static unsigned long int php_swf_get_bits (unsigned char* buffer, unsigned int pos, unsigned int count)
  155. {
  156. unsigned int loop;
  157. unsigned long int result = 0;
  158. for (loop = pos; loop < pos + count; loop++)
  159. {
  160. result = result +
  161. ((((buffer[loop / 8]) >> (7 - (loop % 8))) & 0x01) << (count - (loop - pos) - 1));
  162. }
  163. return result;
  164. }
  165. /* }}} */
  166. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  167. /* {{{ php_handle_swc
  168. */
  169. static struct gfxinfo *php_handle_swc(php_stream * stream)
  170. {
  171. struct gfxinfo *result = NULL;
  172. long bits;
  173. unsigned char a[64];
  174. unsigned long len=64, szlength;
  175. int factor = 1,maxfactor = 16;
  176. int status = 0;
  177. unsigned char *b, *buf = NULL;
  178. zend_string *bufz;
  179. if (php_stream_seek(stream, 5, SEEK_CUR)) {
  180. return NULL;
  181. }
  182. if (php_stream_read(stream, (char *) a, sizeof(a)) != sizeof(a)) {
  183. return NULL;
  184. }
  185. b = ecalloc(1, len + 1);
  186. if (uncompress(b, &len, a, sizeof(a)) != Z_OK) {
  187. /* failed to decompress the file, will try reading the rest of the file */
  188. if (php_stream_seek(stream, 8, SEEK_SET)) {
  189. efree(b);
  190. return NULL;
  191. }
  192. bufz = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  193. if (!bufz) {
  194. efree(b);
  195. return NULL;
  196. }
  197. /*
  198. * zlib::uncompress() wants to know the output data length
  199. * if none was given as a parameter
  200. * we try from input length * 2 up to input length * 2^8
  201. * doubling it whenever it wasn't big enough
  202. * that should be eneugh for all real life cases
  203. */
  204. do {
  205. szlength = ZSTR_LEN(bufz) * (1<<factor++);
  206. buf = erealloc(buf, szlength);
  207. status = uncompress(buf, &szlength, (unsigned char *) ZSTR_VAL(bufz), ZSTR_LEN(bufz));
  208. } while ((status==Z_BUF_ERROR)&&(factor<maxfactor));
  209. if (bufz) {
  210. zend_string_release_ex(bufz, 0);
  211. }
  212. if (status == Z_OK) {
  213. memcpy(b, buf, len);
  214. }
  215. if (buf) {
  216. efree(buf);
  217. }
  218. }
  219. if (!status) {
  220. result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
  221. bits = php_swf_get_bits (b, 0, 5);
  222. result->width = (php_swf_get_bits (b, 5 + bits, bits) -
  223. php_swf_get_bits (b, 5, bits)) / 20;
  224. result->height = (php_swf_get_bits (b, 5 + (3 * bits), bits) -
  225. php_swf_get_bits (b, 5 + (2 * bits), bits)) / 20;
  226. } else {
  227. result = NULL;
  228. }
  229. efree (b);
  230. return result;
  231. }
  232. /* }}} */
  233. #endif
  234. /* {{{ php_handle_swf
  235. */
  236. static struct gfxinfo *php_handle_swf (php_stream * stream)
  237. {
  238. struct gfxinfo *result = NULL;
  239. long bits;
  240. unsigned char a[32];
  241. if (php_stream_seek(stream, 5, SEEK_CUR))
  242. return NULL;
  243. if (php_stream_read(stream, (char*)a, sizeof(a)) != sizeof(a))
  244. return NULL;
  245. result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
  246. bits = php_swf_get_bits (a, 0, 5);
  247. result->width = (php_swf_get_bits (a, 5 + bits, bits) -
  248. php_swf_get_bits (a, 5, bits)) / 20;
  249. result->height = (php_swf_get_bits (a, 5 + (3 * bits), bits) -
  250. php_swf_get_bits (a, 5 + (2 * bits), bits)) / 20;
  251. result->bits = 0;
  252. result->channels = 0;
  253. return result;
  254. }
  255. /* }}} */
  256. /* {{{ php_handle_png
  257. * routine to handle PNG files */
  258. static struct gfxinfo *php_handle_png (php_stream * stream)
  259. {
  260. struct gfxinfo *result = NULL;
  261. unsigned char dim[9];
  262. /* Width: 4 bytes
  263. * Height: 4 bytes
  264. * Bit depth: 1 byte
  265. * Color type: 1 byte
  266. * Compression method: 1 byte
  267. * Filter method: 1 byte
  268. * Interlace method: 1 byte
  269. */
  270. if (php_stream_seek(stream, 8, SEEK_CUR))
  271. return NULL;
  272. if((php_stream_read(stream, (char*)dim, sizeof(dim))) < sizeof(dim))
  273. return NULL;
  274. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  275. result->width = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
  276. result->height = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
  277. result->bits = (unsigned int)dim[8];
  278. return result;
  279. }
  280. /* }}} */
  281. /* routines to handle JPEG data */
  282. /* some defines for the different JPEG block types */
  283. #define M_SOF0 0xC0 /* Start Of Frame N */
  284. #define M_SOF1 0xC1 /* N indicates which compression process */
  285. #define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
  286. #define M_SOF3 0xC3
  287. #define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
  288. #define M_SOF6 0xC6
  289. #define M_SOF7 0xC7
  290. #define M_SOF9 0xC9
  291. #define M_SOF10 0xCA
  292. #define M_SOF11 0xCB
  293. #define M_SOF13 0xCD
  294. #define M_SOF14 0xCE
  295. #define M_SOF15 0xCF
  296. #define M_SOI 0xD8
  297. #define M_EOI 0xD9 /* End Of Image (end of datastream) */
  298. #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
  299. #define M_APP0 0xe0
  300. #define M_APP1 0xe1
  301. #define M_APP2 0xe2
  302. #define M_APP3 0xe3
  303. #define M_APP4 0xe4
  304. #define M_APP5 0xe5
  305. #define M_APP6 0xe6
  306. #define M_APP7 0xe7
  307. #define M_APP8 0xe8
  308. #define M_APP9 0xe9
  309. #define M_APP10 0xea
  310. #define M_APP11 0xeb
  311. #define M_APP12 0xec
  312. #define M_APP13 0xed
  313. #define M_APP14 0xee
  314. #define M_APP15 0xef
  315. #define M_COM 0xFE /* COMment */
  316. #define M_PSEUDO 0xFFD8 /* pseudo marker for start of image(byte 0) */
  317. /* {{{ php_read2
  318. */
  319. static unsigned short php_read2(php_stream * stream)
  320. {
  321. unsigned char a[2];
  322. /* return 0 if we couldn't read enough data */
  323. if((php_stream_read(stream, (char *) a, sizeof(a))) < sizeof(a)) return 0;
  324. return (((unsigned short)a[0]) << 8) + ((unsigned short)a[1]);
  325. }
  326. /* }}} */
  327. /* {{{ php_next_marker
  328. * get next marker byte from file */
  329. static unsigned int php_next_marker(php_stream * stream, int last_marker, int ff_read)
  330. {
  331. int a=0, marker;
  332. /* get marker byte, swallowing possible padding */
  333. if (!ff_read) {
  334. size_t extraneous = 0;
  335. while ((marker = php_stream_getc(stream)) != 0xff) {
  336. if (marker == EOF) {
  337. return M_EOI;/* we hit EOF */
  338. }
  339. extraneous++;
  340. }
  341. if (extraneous) {
  342. php_error_docref(NULL, E_WARNING, "Corrupt JPEG data: %zu extraneous bytes before marker", extraneous);
  343. }
  344. }
  345. a = 1;
  346. do {
  347. if ((marker = php_stream_getc(stream)) == EOF)
  348. {
  349. return M_EOI;/* we hit EOF */
  350. }
  351. a++;
  352. } while (marker == 0xff);
  353. if (a < 2)
  354. {
  355. return M_EOI; /* at least one 0xff is needed before marker code */
  356. }
  357. return (unsigned int)marker;
  358. }
  359. /* }}} */
  360. /* {{{ php_skip_variable
  361. * skip over a variable-length block; assumes proper length marker */
  362. static int php_skip_variable(php_stream * stream)
  363. {
  364. zend_off_t length = ((unsigned int)php_read2(stream));
  365. if (length < 2) {
  366. return 0;
  367. }
  368. length = length - 2;
  369. php_stream_seek(stream, (zend_long)length, SEEK_CUR);
  370. return 1;
  371. }
  372. /* }}} */
  373. /* {{{ php_read_APP
  374. */
  375. static int php_read_APP(php_stream * stream, unsigned int marker, zval *info)
  376. {
  377. size_t length;
  378. char *buffer;
  379. char markername[16];
  380. zval *tmp;
  381. length = php_read2(stream);
  382. if (length < 2) {
  383. return 0;
  384. }
  385. length -= 2; /* length includes itself */
  386. buffer = emalloc(length);
  387. if (php_stream_read(stream, buffer, (size_t) length) != length) {
  388. efree(buffer);
  389. return 0;
  390. }
  391. snprintf(markername, sizeof(markername), "APP%d", marker - M_APP0);
  392. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(info), markername, strlen(markername))) == NULL) {
  393. /* XXX we only catch the 1st tag of it's kind! */
  394. add_assoc_stringl(info, markername, buffer, length);
  395. }
  396. efree(buffer);
  397. return 1;
  398. }
  399. /* }}} */
  400. /* {{{ php_handle_jpeg
  401. main loop to parse JPEG structure */
  402. static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
  403. {
  404. struct gfxinfo *result = NULL;
  405. unsigned int marker = M_PSEUDO;
  406. unsigned short length, ff_read=1;
  407. for (;;) {
  408. marker = php_next_marker(stream, marker, ff_read);
  409. ff_read = 0;
  410. switch (marker) {
  411. case M_SOF0:
  412. case M_SOF1:
  413. case M_SOF2:
  414. case M_SOF3:
  415. case M_SOF5:
  416. case M_SOF6:
  417. case M_SOF7:
  418. case M_SOF9:
  419. case M_SOF10:
  420. case M_SOF11:
  421. case M_SOF13:
  422. case M_SOF14:
  423. case M_SOF15:
  424. if (result == NULL) {
  425. /* handle SOFn block */
  426. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  427. length = php_read2(stream);
  428. result->bits = php_stream_getc(stream);
  429. result->height = php_read2(stream);
  430. result->width = php_read2(stream);
  431. result->channels = php_stream_getc(stream);
  432. if (!info || length < 8) { /* if we don't want an extanded info -> return */
  433. return result;
  434. }
  435. if (php_stream_seek(stream, length - 8, SEEK_CUR)) { /* file error after info */
  436. return result;
  437. }
  438. } else {
  439. if (!php_skip_variable(stream)) {
  440. return result;
  441. }
  442. }
  443. break;
  444. case M_APP0:
  445. case M_APP1:
  446. case M_APP2:
  447. case M_APP3:
  448. case M_APP4:
  449. case M_APP5:
  450. case M_APP6:
  451. case M_APP7:
  452. case M_APP8:
  453. case M_APP9:
  454. case M_APP10:
  455. case M_APP11:
  456. case M_APP12:
  457. case M_APP13:
  458. case M_APP14:
  459. case M_APP15:
  460. if (info) {
  461. if (!php_read_APP(stream, marker, info)) { /* read all the app marks... */
  462. return result;
  463. }
  464. } else {
  465. if (!php_skip_variable(stream)) {
  466. return result;
  467. }
  468. }
  469. break;
  470. case M_SOS:
  471. case M_EOI:
  472. return result; /* we're about to hit image data, or are at EOF. stop processing. */
  473. default:
  474. if (!php_skip_variable(stream)) { /* anything else isn't interesting */
  475. return result;
  476. }
  477. break;
  478. }
  479. }
  480. return result; /* perhaps image broken -> no info but size */
  481. }
  482. /* }}} */
  483. /* {{{ php_read4
  484. */
  485. static unsigned int php_read4(php_stream * stream)
  486. {
  487. unsigned char a[4];
  488. /* just return 0 if we hit the end-of-file */
  489. if ((php_stream_read(stream, (char*)a, sizeof(a))) != sizeof(a)) return 0;
  490. return (((unsigned int)a[0]) << 24)
  491. + (((unsigned int)a[1]) << 16)
  492. + (((unsigned int)a[2]) << 8)
  493. + (((unsigned int)a[3]));
  494. }
  495. /* }}} */
  496. /* {{{ JPEG 2000 Marker Codes */
  497. #define JPEG2000_MARKER_PREFIX 0xFF /* All marker codes start with this */
  498. #define JPEG2000_MARKER_SOC 0x4F /* Start of Codestream */
  499. #define JPEG2000_MARKER_SOT 0x90 /* Start of Tile part */
  500. #define JPEG2000_MARKER_SOD 0x93 /* Start of Data */
  501. #define JPEG2000_MARKER_EOC 0xD9 /* End of Codestream */
  502. #define JPEG2000_MARKER_SIZ 0x51 /* Image and tile size */
  503. #define JPEG2000_MARKER_COD 0x52 /* Coding style default */
  504. #define JPEG2000_MARKER_COC 0x53 /* Coding style component */
  505. #define JPEG2000_MARKER_RGN 0x5E /* Region of interest */
  506. #define JPEG2000_MARKER_QCD 0x5C /* Quantization default */
  507. #define JPEG2000_MARKER_QCC 0x5D /* Quantization component */
  508. #define JPEG2000_MARKER_POC 0x5F /* Progression order change */
  509. #define JPEG2000_MARKER_TLM 0x55 /* Tile-part lengths */
  510. #define JPEG2000_MARKER_PLM 0x57 /* Packet length, main header */
  511. #define JPEG2000_MARKER_PLT 0x58 /* Packet length, tile-part header */
  512. #define JPEG2000_MARKER_PPM 0x60 /* Packed packet headers, main header */
  513. #define JPEG2000_MARKER_PPT 0x61 /* Packed packet headers, tile part header */
  514. #define JPEG2000_MARKER_SOP 0x91 /* Start of packet */
  515. #define JPEG2000_MARKER_EPH 0x92 /* End of packet header */
  516. #define JPEG2000_MARKER_CRG 0x63 /* Component registration */
  517. #define JPEG2000_MARKER_COM 0x64 /* Comment */
  518. /* }}} */
  519. /* {{{ php_handle_jpc
  520. Main loop to parse JPEG2000 raw codestream structure */
  521. static struct gfxinfo *php_handle_jpc(php_stream * stream)
  522. {
  523. struct gfxinfo *result = NULL;
  524. int highest_bit_depth, bit_depth;
  525. unsigned char first_marker_id;
  526. unsigned int i;
  527. /* JPEG 2000 components can be vastly different from one another.
  528. Each component can be sampled at a different resolution, use
  529. a different colour space, have a separate colour depth, and
  530. be compressed totally differently! This makes giving a single
  531. "bit depth" answer somewhat problematic. For this implementation
  532. we'll use the highest depth encountered. */
  533. /* Get the single byte that remains after the file type identification */
  534. first_marker_id = php_stream_getc(stream);
  535. /* Ensure that this marker is SIZ (as is mandated by the standard) */
  536. if (first_marker_id != JPEG2000_MARKER_SIZ) {
  537. php_error_docref(NULL, E_WARNING, "JPEG2000 codestream corrupt(Expected SIZ marker not found after SOC)");
  538. return NULL;
  539. }
  540. result = (struct gfxinfo *)ecalloc(1, sizeof(struct gfxinfo));
  541. php_read2(stream); /* Lsiz */
  542. php_read2(stream); /* Rsiz */
  543. result->width = php_read4(stream); /* Xsiz */
  544. result->height = php_read4(stream); /* Ysiz */
  545. #if MBO_0
  546. php_read4(stream); /* XOsiz */
  547. php_read4(stream); /* YOsiz */
  548. php_read4(stream); /* XTsiz */
  549. php_read4(stream); /* YTsiz */
  550. php_read4(stream); /* XTOsiz */
  551. php_read4(stream); /* YTOsiz */
  552. #else
  553. if (php_stream_seek(stream, 24, SEEK_CUR)) {
  554. efree(result);
  555. return NULL;
  556. }
  557. #endif
  558. result->channels = php_read2(stream); /* Csiz */
  559. if ((result->channels == 0 && php_stream_eof(stream)) || result->channels > 256) {
  560. efree(result);
  561. return NULL;
  562. }
  563. /* Collect bit depth info */
  564. highest_bit_depth = 0;
  565. for (i = 0; i < result->channels; i++) {
  566. bit_depth = php_stream_getc(stream); /* Ssiz[i] */
  567. bit_depth++;
  568. if (bit_depth > highest_bit_depth) {
  569. highest_bit_depth = bit_depth;
  570. }
  571. php_stream_getc(stream); /* XRsiz[i] */
  572. php_stream_getc(stream); /* YRsiz[i] */
  573. }
  574. result->bits = highest_bit_depth;
  575. return result;
  576. }
  577. /* }}} */
  578. /* {{{ php_handle_jp2
  579. main loop to parse JPEG 2000 JP2 wrapper format structure */
  580. static struct gfxinfo *php_handle_jp2(php_stream *stream)
  581. {
  582. struct gfxinfo *result = NULL;
  583. unsigned int box_length;
  584. unsigned int box_type;
  585. char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
  586. /* JP2 is a wrapper format for JPEG 2000. Data is contained within "boxes".
  587. Boxes themselves can be contained within "super-boxes". Super-Boxes can
  588. contain super-boxes which provides us with a hierarchical storage system.
  589. It is valid for a JP2 file to contain multiple individual codestreams.
  590. We'll just look for the first codestream at the root of the box structure
  591. and handle that.
  592. */
  593. for (;;)
  594. {
  595. box_length = php_read4(stream); /* LBox */
  596. /* TBox */
  597. if (php_stream_read(stream, (void *)&box_type, sizeof(box_type)) != sizeof(box_type)) {
  598. /* Use this as a general "out of stream" error */
  599. break;
  600. }
  601. if (box_length == 1) {
  602. /* We won't handle XLBoxes */
  603. return NULL;
  604. }
  605. if (!memcmp(&box_type, jp2c_box_id, 4))
  606. {
  607. /* Skip the first 3 bytes to emulate the file type examination */
  608. php_stream_seek(stream, 3, SEEK_CUR);
  609. result = php_handle_jpc(stream);
  610. break;
  611. }
  612. /* Stop if this was the last box */
  613. if ((int)box_length <= 0) {
  614. break;
  615. }
  616. /* Skip over LBox (Which includes both TBox and LBox itself */
  617. if (php_stream_seek(stream, box_length - 8, SEEK_CUR)) {
  618. break;
  619. }
  620. }
  621. if (result == NULL) {
  622. php_error_docref(NULL, E_WARNING, "JP2 file has no codestreams at root level");
  623. }
  624. return result;
  625. }
  626. /* }}} */
  627. /* {{{ tiff constants
  628. */
  629. PHPAPI const int php_tiff_bytes_per_format[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};
  630. /* uncompressed only */
  631. #define TAG_IMAGEWIDTH 0x0100
  632. #define TAG_IMAGEHEIGHT 0x0101
  633. /* compressed images only */
  634. #define TAG_COMP_IMAGEWIDTH 0xA002
  635. #define TAG_COMP_IMAGEHEIGHT 0xA003
  636. #define TAG_FMT_BYTE 1
  637. #define TAG_FMT_STRING 2
  638. #define TAG_FMT_USHORT 3
  639. #define TAG_FMT_ULONG 4
  640. #define TAG_FMT_URATIONAL 5
  641. #define TAG_FMT_SBYTE 6
  642. #define TAG_FMT_UNDEFINED 7
  643. #define TAG_FMT_SSHORT 8
  644. #define TAG_FMT_SLONG 9
  645. #define TAG_FMT_SRATIONAL 10
  646. #define TAG_FMT_SINGLE 11
  647. #define TAG_FMT_DOUBLE 12
  648. /* }}} */
  649. /* {{{ php_ifd_get16u
  650. * Convert a 16 bit unsigned value from file's native byte order */
  651. static int php_ifd_get16u(void *Short, int motorola_intel)
  652. {
  653. if (motorola_intel) {
  654. return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
  655. } else {
  656. return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
  657. }
  658. }
  659. /* }}} */
  660. /* {{{ php_ifd_get16s
  661. * Convert a 16 bit signed value from file's native byte order */
  662. static signed short php_ifd_get16s(void *Short, int motorola_intel)
  663. {
  664. return (signed short)php_ifd_get16u(Short, motorola_intel);
  665. }
  666. /* }}} */
  667. /* {{{ php_ifd_get32s
  668. * Convert a 32 bit signed value from file's native byte order */
  669. static int php_ifd_get32s(void *Long, int motorola_intel)
  670. {
  671. if (motorola_intel) {
  672. return ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16)
  673. | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
  674. } else {
  675. return ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16)
  676. | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
  677. }
  678. }
  679. /* }}} */
  680. /* {{{ php_ifd_get32u
  681. * Convert a 32 bit unsigned value from file's native byte order */
  682. static unsigned php_ifd_get32u(void *Long, int motorola_intel)
  683. {
  684. return (unsigned)php_ifd_get32s(Long, motorola_intel) & 0xffffffff;
  685. }
  686. /* }}} */
  687. /* {{{ php_handle_tiff
  688. main loop to parse TIFF structure */
  689. static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
  690. {
  691. struct gfxinfo *result = NULL;
  692. int i, num_entries;
  693. unsigned char *dir_entry;
  694. size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
  695. int entry_tag , entry_type;
  696. char *ifd_data, ifd_ptr[4];
  697. if (php_stream_read(stream, ifd_ptr, 4) != 4)
  698. return NULL;
  699. ifd_addr = php_ifd_get32u(ifd_ptr, motorola_intel);
  700. if (php_stream_seek(stream, ifd_addr-8, SEEK_CUR))
  701. return NULL;
  702. ifd_size = 2;
  703. ifd_data = emalloc(ifd_size);
  704. if (php_stream_read(stream, ifd_data, 2) != 2) {
  705. efree(ifd_data);
  706. return NULL;
  707. }
  708. num_entries = php_ifd_get16u(ifd_data, motorola_intel);
  709. dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
  710. ifd_size = dir_size;
  711. ifd_data = erealloc(ifd_data,ifd_size);
  712. if (php_stream_read(stream, ifd_data+2, dir_size-2) != dir_size-2) {
  713. efree(ifd_data);
  714. return NULL;
  715. }
  716. /* now we have the directory we can look how long it should be */
  717. ifd_size = dir_size;
  718. for(i=0;i<num_entries;i++) {
  719. dir_entry = (unsigned char *) ifd_data+2+i*12;
  720. entry_tag = php_ifd_get16u(dir_entry+0, motorola_intel);
  721. entry_type = php_ifd_get16u(dir_entry+2, motorola_intel);
  722. switch(entry_type) {
  723. case TAG_FMT_BYTE:
  724. case TAG_FMT_SBYTE:
  725. entry_value = (size_t)(dir_entry[8]);
  726. break;
  727. case TAG_FMT_USHORT:
  728. entry_value = php_ifd_get16u(dir_entry+8, motorola_intel);
  729. break;
  730. case TAG_FMT_SSHORT:
  731. entry_value = php_ifd_get16s(dir_entry+8, motorola_intel);
  732. break;
  733. case TAG_FMT_ULONG:
  734. entry_value = php_ifd_get32u(dir_entry+8, motorola_intel);
  735. break;
  736. case TAG_FMT_SLONG:
  737. entry_value = php_ifd_get32s(dir_entry+8, motorola_intel);
  738. break;
  739. default:
  740. continue;
  741. }
  742. switch(entry_tag) {
  743. case TAG_IMAGEWIDTH:
  744. case TAG_COMP_IMAGEWIDTH:
  745. width = entry_value;
  746. break;
  747. case TAG_IMAGEHEIGHT:
  748. case TAG_COMP_IMAGEHEIGHT:
  749. height = entry_value;
  750. break;
  751. }
  752. }
  753. efree(ifd_data);
  754. if ( width && height) {
  755. /* not the same when in for-loop */
  756. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  757. result->height = height;
  758. result->width = width;
  759. result->bits = 0;
  760. result->channels = 0;
  761. return result;
  762. }
  763. return NULL;
  764. }
  765. /* }}} */
  766. /* {{{ php_handle_psd
  767. */
  768. static struct gfxinfo *php_handle_iff(php_stream * stream)
  769. {
  770. struct gfxinfo * result;
  771. unsigned char a[10];
  772. int chunkId;
  773. int size;
  774. short width, height, bits;
  775. if (php_stream_read(stream, (char *) a, 8) != 8) {
  776. return NULL;
  777. }
  778. if (strncmp((char *) a+4, "ILBM", 4) && strncmp((char *) a+4, "PBM ", 4)) {
  779. return NULL;
  780. }
  781. /* loop chunks to find BMHD chunk */
  782. do {
  783. if (php_stream_read(stream, (char*)a, 8) != 8) {
  784. return NULL;
  785. }
  786. chunkId = php_ifd_get32s(a+0, 1);
  787. size = php_ifd_get32s(a+4, 1);
  788. if (size < 0) {
  789. return NULL;
  790. }
  791. if ((size & 1) == 1) {
  792. size++;
  793. }
  794. if (chunkId == 0x424d4844) { /* BMHD chunk */
  795. if (size < 9 || php_stream_read(stream, (char*)a, 9) != 9) {
  796. return NULL;
  797. }
  798. width = php_ifd_get16s(a+0, 1);
  799. height = php_ifd_get16s(a+2, 1);
  800. bits = a[8] & 0xff;
  801. if (width > 0 && height > 0 && bits > 0 && bits < 33) {
  802. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  803. result->width = width;
  804. result->height = height;
  805. result->bits = bits;
  806. result->channels = 0;
  807. return result;
  808. }
  809. } else {
  810. if (php_stream_seek(stream, size, SEEK_CUR)) {
  811. return NULL;
  812. }
  813. }
  814. } while (1);
  815. }
  816. /* }}} */
  817. /* {{{ php_get_wbmp
  818. * int WBMP file format type
  819. * byte Header Type
  820. * byte Extended Header
  821. * byte Header Data (type 00 = multibyte)
  822. * byte Header Data (type 11 = name/pairs)
  823. * int Number of columns
  824. * int Number of rows
  825. */
  826. static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
  827. {
  828. int i, width = 0, height = 0;
  829. if (php_stream_rewind(stream)) {
  830. return 0;
  831. }
  832. /* get type */
  833. if (php_stream_getc(stream) != 0) {
  834. return 0;
  835. }
  836. /* skip header */
  837. do {
  838. i = php_stream_getc(stream);
  839. if (i < 0) {
  840. return 0;
  841. }
  842. } while (i & 0x80);
  843. /* get width */
  844. do {
  845. i = php_stream_getc(stream);
  846. if (i < 0) {
  847. return 0;
  848. }
  849. width = (width << 7) | (i & 0x7f);
  850. /* maximum valid width for wbmp (although 127 may be a more accurate one) */
  851. if (width > 2048) {
  852. return 0;
  853. }
  854. } while (i & 0x80);
  855. /* get height */
  856. do {
  857. i = php_stream_getc(stream);
  858. if (i < 0) {
  859. return 0;
  860. }
  861. height = (height << 7) | (i & 0x7f);
  862. /* maximum valid height for wbmp (although 127 may be a more accurate one) */
  863. if (height > 2048) {
  864. return 0;
  865. }
  866. } while (i & 0x80);
  867. if (!height || !width) {
  868. return 0;
  869. }
  870. if (!check) {
  871. (*result)->width = width;
  872. (*result)->height = height;
  873. }
  874. return IMAGE_FILETYPE_WBMP;
  875. }
  876. /* }}} */
  877. /* {{{ php_handle_wbmp
  878. */
  879. static struct gfxinfo *php_handle_wbmp(php_stream * stream)
  880. {
  881. struct gfxinfo *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  882. if (!php_get_wbmp(stream, &result, 0)) {
  883. efree(result);
  884. return NULL;
  885. }
  886. return result;
  887. }
  888. /* }}} */
  889. /* {{{ php_get_xbm
  890. */
  891. static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
  892. {
  893. char *fline;
  894. char *iname;
  895. char *type;
  896. int value;
  897. unsigned int width = 0, height = 0;
  898. if (result) {
  899. *result = NULL;
  900. }
  901. if (php_stream_rewind(stream)) {
  902. return 0;
  903. }
  904. while ((fline=php_stream_gets(stream, NULL, 0)) != NULL) {
  905. iname = estrdup(fline); /* simple way to get necessary buffer of required size */
  906. if (sscanf(fline, "#define %s %d", iname, &value) == 2) {
  907. if (!(type = strrchr(iname, '_'))) {
  908. type = iname;
  909. } else {
  910. type++;
  911. }
  912. if (!strcmp("width", type)) {
  913. width = (unsigned int) value;
  914. if (height) {
  915. efree(iname);
  916. break;
  917. }
  918. }
  919. if (!strcmp("height", type)) {
  920. height = (unsigned int) value;
  921. if (width) {
  922. efree(iname);
  923. break;
  924. }
  925. }
  926. }
  927. efree(fline);
  928. efree(iname);
  929. }
  930. if (fline) {
  931. efree(fline);
  932. }
  933. if (width && height) {
  934. if (result) {
  935. *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  936. (*result)->width = width;
  937. (*result)->height = height;
  938. }
  939. return IMAGE_FILETYPE_XBM;
  940. }
  941. return 0;
  942. }
  943. /* }}} */
  944. /* {{{ php_handle_xbm
  945. */
  946. static struct gfxinfo *php_handle_xbm(php_stream * stream)
  947. {
  948. struct gfxinfo *result;
  949. php_get_xbm(stream, &result);
  950. return result;
  951. }
  952. /* }}} */
  953. /* {{{ php_handle_ico
  954. */
  955. static struct gfxinfo *php_handle_ico(php_stream * stream)
  956. {
  957. struct gfxinfo *result = NULL;
  958. unsigned char dim[16];
  959. int num_icons = 0;
  960. if (php_stream_read(stream, (char *) dim, 2) != 2)
  961. return NULL;
  962. num_icons = (((unsigned int)dim[1]) << 8) + ((unsigned int) dim[0]);
  963. if (num_icons < 1 || num_icons > 255)
  964. return NULL;
  965. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  966. while (num_icons > 0)
  967. {
  968. if (php_stream_read(stream, (char *) dim, sizeof(dim)) != sizeof(dim))
  969. break;
  970. if ((((unsigned int)dim[7]) << 8) + ((unsigned int)dim[6]) >= result->bits)
  971. {
  972. result->width = (unsigned int)dim[0];
  973. result->height = (unsigned int)dim[1];
  974. result->bits = (((unsigned int)dim[7]) << 8) + ((unsigned int)dim[6]);
  975. }
  976. num_icons--;
  977. }
  978. return result;
  979. }
  980. /* }}} */
  981. /* {{{ php_handle_webp
  982. */
  983. static struct gfxinfo *php_handle_webp(php_stream * stream)
  984. {
  985. struct gfxinfo *result = NULL;
  986. const char sig[3] = {'V', 'P', '8'};
  987. unsigned char buf[18];
  988. char format;
  989. if (php_stream_read(stream, (char *) buf, 18) != 18)
  990. return NULL;
  991. if (memcmp(buf, sig, 3)) {
  992. return NULL;
  993. }
  994. switch (buf[3]) {
  995. case ' ':
  996. case 'L':
  997. case 'X':
  998. format = buf[3];
  999. break;
  1000. default:
  1001. return NULL;
  1002. }
  1003. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  1004. switch (format) {
  1005. case ' ':
  1006. result->width = buf[14] + ((buf[15] & 0x3F) << 8);
  1007. result->height = buf[16] + ((buf[17] & 0x3F) << 8);
  1008. break;
  1009. case 'L':
  1010. result->width = buf[9] + ((buf[10] & 0x3F) << 8) + 1;
  1011. result->height = (buf[10] >> 6) + (buf[11] << 2) + ((buf[12] & 0xF) << 10) + 1;
  1012. break;
  1013. case 'X':
  1014. result->width = buf[12] + (buf[13] << 8) + (buf[14] << 16) + 1;
  1015. result->height = buf[15] + (buf[16] << 8) + (buf[17] << 16) + 1;
  1016. break;
  1017. }
  1018. result->bits = 8; /* always 1 byte */
  1019. return result;
  1020. }
  1021. /* }}} */
  1022. /* {{{ php_image_type_to_mime_type
  1023. * Convert internal image_type to mime type */
  1024. PHPAPI char * php_image_type_to_mime_type(int image_type)
  1025. {
  1026. switch( image_type) {
  1027. case IMAGE_FILETYPE_GIF:
  1028. return "image/gif";
  1029. case IMAGE_FILETYPE_JPEG:
  1030. return "image/jpeg";
  1031. case IMAGE_FILETYPE_PNG:
  1032. return "image/png";
  1033. case IMAGE_FILETYPE_SWF:
  1034. case IMAGE_FILETYPE_SWC:
  1035. return "application/x-shockwave-flash";
  1036. case IMAGE_FILETYPE_PSD:
  1037. return "image/psd";
  1038. case IMAGE_FILETYPE_BMP:
  1039. return "image/bmp";
  1040. case IMAGE_FILETYPE_TIFF_II:
  1041. case IMAGE_FILETYPE_TIFF_MM:
  1042. return "image/tiff";
  1043. case IMAGE_FILETYPE_IFF:
  1044. return "image/iff";
  1045. case IMAGE_FILETYPE_WBMP:
  1046. return "image/vnd.wap.wbmp";
  1047. case IMAGE_FILETYPE_JPC:
  1048. return "application/octet-stream";
  1049. case IMAGE_FILETYPE_JP2:
  1050. return "image/jp2";
  1051. case IMAGE_FILETYPE_XBM:
  1052. return "image/xbm";
  1053. case IMAGE_FILETYPE_ICO:
  1054. return "image/vnd.microsoft.icon";
  1055. case IMAGE_FILETYPE_WEBP:
  1056. return "image/webp";
  1057. default:
  1058. case IMAGE_FILETYPE_UNKNOWN:
  1059. return "application/octet-stream"; /* suppose binary format */
  1060. }
  1061. }
  1062. /* }}} */
  1063. /* {{{ proto string image_type_to_mime_type(int imagetype)
  1064. Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
  1065. PHP_FUNCTION(image_type_to_mime_type)
  1066. {
  1067. zend_long p_image_type;
  1068. ZEND_PARSE_PARAMETERS_START(1, 1)
  1069. Z_PARAM_LONG(p_image_type)
  1070. ZEND_PARSE_PARAMETERS_END();
  1071. ZVAL_STRING(return_value, (char*)php_image_type_to_mime_type(p_image_type));
  1072. }
  1073. /* }}} */
  1074. /* {{{ proto string|false image_type_to_extension(int imagetype [, bool include_dot])
  1075. Get file extension for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
  1076. PHP_FUNCTION(image_type_to_extension)
  1077. {
  1078. zend_long image_type;
  1079. zend_bool inc_dot=1;
  1080. const char *imgext = NULL;
  1081. ZEND_PARSE_PARAMETERS_START(1, 2)
  1082. Z_PARAM_LONG(image_type)
  1083. Z_PARAM_OPTIONAL
  1084. Z_PARAM_BOOL(inc_dot)
  1085. ZEND_PARSE_PARAMETERS_END();
  1086. switch (image_type) {
  1087. case IMAGE_FILETYPE_GIF:
  1088. imgext = ".gif";
  1089. break;
  1090. case IMAGE_FILETYPE_JPEG:
  1091. imgext = ".jpeg";
  1092. break;
  1093. case IMAGE_FILETYPE_PNG:
  1094. imgext = ".png";
  1095. break;
  1096. case IMAGE_FILETYPE_SWF:
  1097. case IMAGE_FILETYPE_SWC:
  1098. imgext = ".swf";
  1099. break;
  1100. case IMAGE_FILETYPE_PSD:
  1101. imgext = ".psd";
  1102. break;
  1103. case IMAGE_FILETYPE_BMP:
  1104. case IMAGE_FILETYPE_WBMP:
  1105. imgext = ".bmp";
  1106. break;
  1107. case IMAGE_FILETYPE_TIFF_II:
  1108. case IMAGE_FILETYPE_TIFF_MM:
  1109. imgext = ".tiff";
  1110. break;
  1111. case IMAGE_FILETYPE_IFF:
  1112. imgext = ".iff";
  1113. break;
  1114. case IMAGE_FILETYPE_JPC:
  1115. imgext = ".jpc";
  1116. break;
  1117. case IMAGE_FILETYPE_JP2:
  1118. imgext = ".jp2";
  1119. break;
  1120. case IMAGE_FILETYPE_JPX:
  1121. imgext = ".jpx";
  1122. break;
  1123. case IMAGE_FILETYPE_JB2:
  1124. imgext = ".jb2";
  1125. break;
  1126. case IMAGE_FILETYPE_XBM:
  1127. imgext = ".xbm";
  1128. break;
  1129. case IMAGE_FILETYPE_ICO:
  1130. imgext = ".ico";
  1131. break;
  1132. case IMAGE_FILETYPE_WEBP:
  1133. imgext = ".webp";
  1134. break;
  1135. }
  1136. if (imgext) {
  1137. RETURN_STRING(&imgext[!inc_dot]);
  1138. }
  1139. RETURN_FALSE;
  1140. }
  1141. /* }}} */
  1142. /* {{{ php_imagetype
  1143. detect filetype from first bytes */
  1144. PHPAPI int php_getimagetype(php_stream * stream, char *input, char *filetype)
  1145. {
  1146. char tmp[12];
  1147. int twelve_bytes_read;
  1148. if ( !filetype) filetype = tmp;
  1149. if((php_stream_read(stream, filetype, 3)) != 3) {
  1150. php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
  1151. return IMAGE_FILETYPE_UNKNOWN;
  1152. }
  1153. /* BYTES READ: 3 */
  1154. if (!memcmp(filetype, php_sig_gif, 3)) {
  1155. return IMAGE_FILETYPE_GIF;
  1156. } else if (!memcmp(filetype, php_sig_jpg, 3)) {
  1157. return IMAGE_FILETYPE_JPEG;
  1158. } else if (!memcmp(filetype, php_sig_png, 3)) {
  1159. if (php_stream_read(stream, filetype+3, 5) != 5) {
  1160. php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
  1161. return IMAGE_FILETYPE_UNKNOWN;
  1162. }
  1163. if (!memcmp(filetype, php_sig_png, 8)) {
  1164. return IMAGE_FILETYPE_PNG;
  1165. } else {
  1166. php_error_docref(NULL, E_WARNING, "PNG file corrupted by ASCII conversion");
  1167. return IMAGE_FILETYPE_UNKNOWN;
  1168. }
  1169. } else if (!memcmp(filetype, php_sig_swf, 3)) {
  1170. return IMAGE_FILETYPE_SWF;
  1171. } else if (!memcmp(filetype, php_sig_swc, 3)) {
  1172. return IMAGE_FILETYPE_SWC;
  1173. } else if (!memcmp(filetype, php_sig_psd, 3)) {
  1174. return IMAGE_FILETYPE_PSD;
  1175. } else if (!memcmp(filetype, php_sig_bmp, 2)) {
  1176. return IMAGE_FILETYPE_BMP;
  1177. } else if (!memcmp(filetype, php_sig_jpc, 3)) {
  1178. return IMAGE_FILETYPE_JPC;
  1179. } else if (!memcmp(filetype, php_sig_riff, 3)) {
  1180. if (php_stream_read(stream, filetype+3, 9) != 9) {
  1181. php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
  1182. return IMAGE_FILETYPE_UNKNOWN;
  1183. }
  1184. if (!memcmp(filetype+8, php_sig_webp, 4)) {
  1185. return IMAGE_FILETYPE_WEBP;
  1186. } else {
  1187. return IMAGE_FILETYPE_UNKNOWN;
  1188. }
  1189. }
  1190. if (php_stream_read(stream, filetype+3, 1) != 1) {
  1191. php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
  1192. return IMAGE_FILETYPE_UNKNOWN;
  1193. }
  1194. /* BYTES READ: 4 */
  1195. if (!memcmp(filetype, php_sig_tif_ii, 4)) {
  1196. return IMAGE_FILETYPE_TIFF_II;
  1197. } else if (!memcmp(filetype, php_sig_tif_mm, 4)) {
  1198. return IMAGE_FILETYPE_TIFF_MM;
  1199. } else if (!memcmp(filetype, php_sig_iff, 4)) {
  1200. return IMAGE_FILETYPE_IFF;
  1201. } else if (!memcmp(filetype, php_sig_ico, 4)) {
  1202. return IMAGE_FILETYPE_ICO;
  1203. }
  1204. /* WBMP may be smaller than 12 bytes, so delay error */
  1205. twelve_bytes_read = (php_stream_read(stream, filetype+4, 8) == 8);
  1206. /* BYTES READ: 12 */
  1207. if (twelve_bytes_read && !memcmp(filetype, php_sig_jp2, 12)) {
  1208. return IMAGE_FILETYPE_JP2;
  1209. }
  1210. /* AFTER ALL ABOVE FAILED */
  1211. if (php_get_wbmp(stream, NULL, 1)) {
  1212. return IMAGE_FILETYPE_WBMP;
  1213. }
  1214. if (!twelve_bytes_read) {
  1215. php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
  1216. return IMAGE_FILETYPE_UNKNOWN;
  1217. }
  1218. if (php_get_xbm(stream, NULL)) {
  1219. return IMAGE_FILETYPE_XBM;
  1220. }
  1221. return IMAGE_FILETYPE_UNKNOWN;
  1222. }
  1223. /* }}} */
  1224. static void php_getimagesize_from_stream(php_stream *stream, char *input, zval *info, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  1225. {
  1226. int itype = 0;
  1227. struct gfxinfo *result = NULL;
  1228. if (!stream) {
  1229. RETURN_FALSE;
  1230. }
  1231. itype = php_getimagetype(stream, input, NULL);
  1232. switch( itype) {
  1233. case IMAGE_FILETYPE_GIF:
  1234. result = php_handle_gif(stream);
  1235. break;
  1236. case IMAGE_FILETYPE_JPEG:
  1237. if (info) {
  1238. result = php_handle_jpeg(stream, info);
  1239. } else {
  1240. result = php_handle_jpeg(stream, NULL);
  1241. }
  1242. break;
  1243. case IMAGE_FILETYPE_PNG:
  1244. result = php_handle_png(stream);
  1245. break;
  1246. case IMAGE_FILETYPE_SWF:
  1247. result = php_handle_swf(stream);
  1248. break;
  1249. case IMAGE_FILETYPE_SWC:
  1250. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  1251. result = php_handle_swc(stream);
  1252. #else
  1253. php_error_docref(NULL, E_NOTICE, "The image is a compressed SWF file, but you do not have a static version of the zlib extension enabled");
  1254. #endif
  1255. break;
  1256. case IMAGE_FILETYPE_PSD:
  1257. result = php_handle_psd(stream);
  1258. break;
  1259. case IMAGE_FILETYPE_BMP:
  1260. result = php_handle_bmp(stream);
  1261. break;
  1262. case IMAGE_FILETYPE_TIFF_II:
  1263. result = php_handle_tiff(stream, NULL, 0);
  1264. break;
  1265. case IMAGE_FILETYPE_TIFF_MM:
  1266. result = php_handle_tiff(stream, NULL, 1);
  1267. break;
  1268. case IMAGE_FILETYPE_JPC:
  1269. result = php_handle_jpc(stream);
  1270. break;
  1271. case IMAGE_FILETYPE_JP2:
  1272. result = php_handle_jp2(stream);
  1273. break;
  1274. case IMAGE_FILETYPE_IFF:
  1275. result = php_handle_iff(stream);
  1276. break;
  1277. case IMAGE_FILETYPE_WBMP:
  1278. result = php_handle_wbmp(stream);
  1279. break;
  1280. case IMAGE_FILETYPE_XBM:
  1281. result = php_handle_xbm(stream);
  1282. break;
  1283. case IMAGE_FILETYPE_ICO:
  1284. result = php_handle_ico(stream);
  1285. break;
  1286. case IMAGE_FILETYPE_WEBP:
  1287. result = php_handle_webp(stream);
  1288. break;
  1289. default:
  1290. case IMAGE_FILETYPE_UNKNOWN:
  1291. break;
  1292. }
  1293. if (result) {
  1294. char temp[MAX_LENGTH_OF_LONG * 2 + sizeof("width=\"\" height=\"\"")];
  1295. array_init(return_value);
  1296. add_index_long(return_value, 0, result->width);
  1297. add_index_long(return_value, 1, result->height);
  1298. add_index_long(return_value, 2, itype);
  1299. snprintf(temp, sizeof(temp), "width=\"%d\" height=\"%d\"", result->width, result->height);
  1300. add_index_string(return_value, 3, temp);
  1301. if (result->bits != 0) {
  1302. add_assoc_long(return_value, "bits", result->bits);
  1303. }
  1304. if (result->channels != 0) {
  1305. add_assoc_long(return_value, "channels", result->channels);
  1306. }
  1307. add_assoc_string(return_value, "mime", (char*)php_image_type_to_mime_type(itype));
  1308. efree(result);
  1309. } else {
  1310. RETURN_FALSE;
  1311. }
  1312. }
  1313. /* }}} */
  1314. #define FROM_DATA 0
  1315. #define FROM_PATH 1
  1316. static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) { /* {{{ */
  1317. zval *info = NULL;
  1318. php_stream *stream = NULL;
  1319. char *input;
  1320. size_t input_len;
  1321. const int argc = ZEND_NUM_ARGS();
  1322. ZEND_PARSE_PARAMETERS_START(1, 2)
  1323. Z_PARAM_STRING(input, input_len)
  1324. Z_PARAM_OPTIONAL
  1325. Z_PARAM_ZVAL(info)
  1326. ZEND_PARSE_PARAMETERS_END();
  1327. if (argc == 2) {
  1328. info = zend_try_array_init(info);
  1329. if (!info) {
  1330. RETURN_THROWS();
  1331. }
  1332. }
  1333. if (mode == FROM_PATH) {
  1334. stream = php_stream_open_wrapper(input, "rb", STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH, NULL);
  1335. } else {
  1336. stream = php_stream_memory_open(TEMP_STREAM_READONLY, input, input_len);
  1337. }
  1338. if (!stream) {
  1339. RETURN_FALSE;
  1340. }
  1341. php_getimagesize_from_stream(stream, input, info, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1342. php_stream_close(stream);
  1343. }
  1344. /* }}} */
  1345. /* {{{ proto array|false getimagesize(string imagefile [, array info])
  1346. Get the size of an image as 4-element array */
  1347. PHP_FUNCTION(getimagesize)
  1348. {
  1349. php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_PATH);
  1350. }
  1351. /* }}} */
  1352. /* {{{ proto array|false getimagesizefromstring(string data [, array info])
  1353. Get the size of an image as 4-element array */
  1354. PHP_FUNCTION(getimagesizefromstring)
  1355. {
  1356. php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_DATA);
  1357. }
  1358. /* }}} */