gd_ctx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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: Stanislav Malyshev <stas@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php_gd.h"
  20. #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
  21. static void _php_image_output_putc(struct gdIOCtx *ctx, int c)
  22. {
  23. /* without the following downcast, the write will fail
  24. * (i.e., will write a zero byte) for all
  25. * big endian architectures:
  26. */
  27. unsigned char ch = (unsigned char) c;
  28. TSRMLS_FETCH();
  29. php_write(&ch, 1 TSRMLS_CC);
  30. }
  31. static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
  32. {
  33. TSRMLS_FETCH();
  34. return php_write((void *)buf, l TSRMLS_CC);
  35. }
  36. static void _php_image_output_ctxfree(struct gdIOCtx *ctx)
  37. {
  38. if(ctx) {
  39. efree(ctx);
  40. }
  41. }
  42. static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) {
  43. char ch = (char) c;
  44. php_stream * stream = (php_stream *)ctx->data;
  45. TSRMLS_FETCH();
  46. php_stream_write(stream, &ch, 1);
  47. }
  48. static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
  49. {
  50. php_stream * stream = (php_stream *)ctx->data;
  51. TSRMLS_FETCH();
  52. return php_stream_write(stream, (void *)buf, l);
  53. }
  54. static void _php_image_stream_ctxfree(struct gdIOCtx *ctx)
  55. {
  56. if(ctx->data) {
  57. ctx->data = NULL;
  58. }
  59. if(ctx) {
  60. efree(ctx);
  61. }
  62. } /* }}} */
  63. static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
  64. {
  65. TSRMLS_FETCH();
  66. if(ctx->data) {
  67. php_stream_close((php_stream *) ctx->data);
  68. ctx->data = NULL;
  69. }
  70. if(ctx) {
  71. efree(ctx);
  72. }
  73. }
  74. /* {{{ _php_image_output_ctx */
  75. static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
  76. {
  77. zval *imgind;
  78. char *file = NULL;
  79. int file_len = 0;
  80. long quality, basefilter;
  81. gdImagePtr im;
  82. int argc = ZEND_NUM_ARGS();
  83. int q = -1, i;
  84. int f = -1;
  85. gdIOCtx *ctx = NULL;
  86. zval *to_zval = NULL;
  87. php_stream *stream;
  88. int close_stream = 1;
  89. /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
  90. * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
  91. * from imagey<type>().
  92. */
  93. if (image_type == PHP_GDIMG_TYPE_XBM) {
  94. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
  95. return;
  96. }
  97. } else {
  98. /* PHP_GDIMG_TYPE_GIF
  99. * PHP_GDIMG_TYPE_PNG
  100. * PHP_GDIMG_TYPE_JPG
  101. * PHP_GDIMG_TYPE_WBM
  102. * PHP_GDIMG_TYPE_WEBP
  103. * */
  104. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z/!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) {
  105. return;
  106. }
  107. }
  108. ZEND_FETCH_RESOURCE(im, gdImagePtr, &imgind, -1, "Image", phpi_get_le_gd());
  109. if (argc >= 3) {
  110. q = quality; /* or colorindex for foreground of BW images (defaults to black) */
  111. if (argc == 4) {
  112. f = basefilter;
  113. }
  114. }
  115. if (argc > 1 && to_zval != NULL) {
  116. if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
  117. php_stream_from_zval_no_verify(stream, &to_zval);
  118. if (stream == NULL) {
  119. RETURN_FALSE;
  120. }
  121. close_stream = 0;
  122. } else if (Z_TYPE_P(to_zval) == IS_STRING) {
  123. if (CHECK_ZVAL_NULL_PATH(to_zval)) {
  124. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes");
  125. RETURN_FALSE;
  126. }
  127. stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
  128. if (stream == NULL) {
  129. RETURN_FALSE;
  130. }
  131. } else {
  132. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
  133. RETURN_FALSE;
  134. }
  135. } else if (argc > 1 && file != NULL) {
  136. stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
  137. if (stream == NULL) {
  138. RETURN_FALSE;
  139. }
  140. } else {
  141. ctx = emalloc(sizeof(gdIOCtx));
  142. ctx->putC = _php_image_output_putc;
  143. ctx->putBuf = _php_image_output_putbuf;
  144. ctx->gd_free = _php_image_output_ctxfree;
  145. #if APACHE && defined(CHARSET_EBCDIC)
  146. /* XXX this is unlikely to work any more thies@thieso.net */
  147. /* This is a binary file already: avoid EBCDIC->ASCII conversion */
  148. ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
  149. #endif
  150. }
  151. if (!ctx) {
  152. ctx = emalloc(sizeof(gdIOCtx));
  153. ctx->putC = _php_image_stream_putc;
  154. ctx->putBuf = _php_image_stream_putbuf;
  155. if (close_stream) {
  156. ctx->gd_free = _php_image_stream_ctxfreeandclose;
  157. } else {
  158. ctx->gd_free = _php_image_stream_ctxfree;
  159. }
  160. ctx->data = (void *)stream;
  161. }
  162. switch(image_type) {
  163. case PHP_GDIMG_CONVERT_WBM:
  164. if(q<0||q>255) {
  165. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
  166. }
  167. case PHP_GDIMG_TYPE_JPG:
  168. (*func_p)(im, ctx, q);
  169. break;
  170. case PHP_GDIMG_TYPE_WEBP:
  171. if (q == -1) {
  172. q = 80;
  173. }
  174. (*func_p)(im, ctx, q);
  175. break;
  176. case PHP_GDIMG_TYPE_PNG:
  177. (*func_p)(im, ctx, q, f);
  178. break;
  179. case PHP_GDIMG_TYPE_XBM:
  180. case PHP_GDIMG_TYPE_WBM:
  181. if (argc < 3) {
  182. for(i=0; i < gdImageColorsTotal(im); i++) {
  183. if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
  184. }
  185. q = i;
  186. }
  187. if (image_type == PHP_GDIMG_TYPE_XBM) {
  188. (*func_p)(im, file ? file : "", q, ctx);
  189. } else {
  190. (*func_p)(im, q, ctx);
  191. }
  192. break;
  193. default:
  194. (*func_p)(im, ctx);
  195. break;
  196. }
  197. ctx->gd_free(ctx);
  198. RETURN_TRUE;
  199. }
  200. /* }}} */
  201. /*
  202. * Local variables:
  203. * tab-width: 4
  204. * c-basic-offset: 4
  205. * End:
  206. * vim600: sw=4 ts=4 fdm=marker
  207. * vim<600: sw=4 ts=4
  208. */