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