image.c 41 KB

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