1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2013 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
19 /* $Id$ */
20
21 #include "php_gd.h"
22
23 #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
24
_php_image_output_putc(struct gdIOCtx * ctx,int c)25 static void _php_image_output_putc(struct gdIOCtx *ctx, int c)
26 {
27 /* without the following downcast, the write will fail
28 * (i.e., will write a zero byte) for all
29 * big endian architectures:
30 */
31 unsigned char ch = (unsigned char) c;
32 TSRMLS_FETCH();
33 php_write(&ch, 1 TSRMLS_CC);
34 }
35
_php_image_output_putbuf(struct gdIOCtx * ctx,const void * buf,int l)36 static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
37 {
38 TSRMLS_FETCH();
39 return php_write((void *)buf, l TSRMLS_CC);
40 }
41
_php_image_output_ctxfree(struct gdIOCtx * ctx)42 static void _php_image_output_ctxfree(struct gdIOCtx *ctx)
43 {
44 if(ctx) {
45 efree(ctx);
46 }
47 }
48
49 /* {{{ _php_image_output_ctx */
_php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn,void (* func_p)())50 static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
51 {
52 zval *imgind;
53 char *file = NULL;
54 int file_len = 0;
55 long quality, basefilter;
56 gdImagePtr im;
57 FILE *fp = NULL;
58 int argc = ZEND_NUM_ARGS();
59 int q = -1, i;
60 int f = -1;
61 gdIOCtx *ctx;
62
63 /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
64 * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
65 * from imagey<type>().
66 */
67
68 if (image_type == PHP_GDIMG_TYPE_XBM) {
69 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
70 return;
71 }
72 } else {
73 /* PHP_GDIMG_TYPE_GIF
74 * PHP_GDIMG_TYPE_PNG
75 * PHP_GDIMG_TYPE_JPG
76 * PHP_GDIMG_TYPE_WBM */
77 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
78 return;
79 }
80 }
81
82 ZEND_FETCH_RESOURCE(im, gdImagePtr, &imgind, -1, "Image", phpi_get_le_gd());
83
84 if (argc > 1) {
85 if (argc >= 3) {
86 q = quality; /* or colorindex for foreground of BW images (defaults to black) */
87 if (argc == 4) {
88 f = basefilter;
89 }
90 }
91 }
92
93 if (argc > 1 && file_len) {
94 if (strlen(file) != file_len) {
95 RETURN_FALSE;
96 }
97 PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
98
99 fp = VCWD_FOPEN(file, "wb");
100 if (!fp) {
101 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing: %s", file, strerror(errno));
102 RETURN_FALSE;
103 }
104
105 ctx = gdNewFileCtx(fp);
106 } else {
107 ctx = emalloc(sizeof(gdIOCtx));
108 ctx->putC = _php_image_output_putc;
109 ctx->putBuf = _php_image_output_putbuf;
110 #if HAVE_LIBGD204
111 ctx->gd_free = _php_image_output_ctxfree;
112 #else
113 ctx->free = _php_image_output_ctxfree;
114 #endif
115
116 #if APACHE && defined(CHARSET_EBCDIC)
117 /* XXX this is unlikely to work any more thies@thieso.net */
118 /* This is a binary file already: avoid EBCDIC->ASCII conversion */
119 ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
120 #endif
121 }
122
123 switch(image_type) {
124 case PHP_GDIMG_CONVERT_WBM:
125 if(q<0||q>255) {
126 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
127 }
128 case PHP_GDIMG_TYPE_JPG:
129 (*func_p)(im, ctx, q);
130 break;
131 case PHP_GDIMG_TYPE_PNG:
132 (*func_p)(im, ctx, q, f);
133 break;
134 case PHP_GDIMG_TYPE_XBM:
135 case PHP_GDIMG_TYPE_WBM:
136 if (argc < 3) {
137 for(i=0; i < gdImageColorsTotal(im); i++) {
138 if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
139 }
140 q = i;
141 }
142 if (image_type == PHP_GDIMG_TYPE_XBM) {
143 (*func_p)(im, file, q, ctx);
144 } else {
145 (*func_p)(im, q, ctx);
146 }
147 break;
148 default:
149 (*func_p)(im, ctx);
150 break;
151 }
152
153 #if HAVE_LIBGD204
154 ctx->gd_free(ctx);
155 #else
156 ctx->free(ctx);
157 #endif
158
159 if(fp) {
160 fflush(fp);
161 fclose(fp);
162 }
163
164 RETURN_TRUE;
165 }
166 /* }}} */
167
168 /*
169 * Local variables:
170 * tab-width: 4
171 * c-basic-offset: 4
172 * End:
173 * vim600: sw=4 ts=4 fdm=marker
174 * vim<600: sw=4 ts=4
175 */
176