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