xref: /PHP-7.0/ext/gd/gd_ctx.c (revision e56fe7e5)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 	php_write(&ch, 1);
33 } /* }}} */
34 
_php_image_output_putbuf(struct gdIOCtx * ctx,const void * buf,int l)35 static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
36 {
37 	return php_write((void *)buf, l);
38 } /* }}} */
39 
_php_image_output_ctxfree(struct gdIOCtx * ctx)40 static void _php_image_output_ctxfree(struct gdIOCtx *ctx) /* {{{ */
41 {
42 	if(ctx) {
43 		efree(ctx);
44 	}
45 } /* }}} */
46 
_php_image_stream_putc(struct gdIOCtx * ctx,int c)47 static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) /* {{{ */ {
48 	char ch = (char) c;
49 	php_stream * stream = (php_stream *)ctx->data;
50 	php_stream_write(stream, &ch, 1);
51 } /* }}} */
52 
_php_image_stream_putbuf(struct gdIOCtx * ctx,const void * buf,int l)53 static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
54 {
55 	php_stream * stream = (php_stream *)ctx->data;
56 	return php_stream_write(stream, (void *)buf, l);
57 } /* }}} */
58 
_php_image_stream_ctxfree(struct gdIOCtx * ctx)59 static void _php_image_stream_ctxfree(struct gdIOCtx *ctx) /* {{{ */
60 {
61 	if(ctx->data) {
62 		ctx->data = NULL;
63 	}
64 	if(ctx) {
65 		efree(ctx);
66 	}
67 } /* }}} */
68 
_php_image_stream_ctxfreeandclose(struct gdIOCtx * ctx)69 static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
70 {
71 
72 	if(ctx->data) {
73 		php_stream_close((php_stream *) ctx->data);
74 		ctx->data = NULL;
75 	}
76 	if(ctx) {
77 		efree(ctx);
78 	}
79 } /* }}} */
80 
81 /* {{{ _php_image_output_ctx */
_php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn,void (* func_p)())82 static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
83 {
84 	zval *imgind;
85 	char *file = NULL;
86 	size_t file_len = 0;
87 	zend_long quality, basefilter;
88 	gdImagePtr im;
89 	int argc = ZEND_NUM_ARGS();
90 	int q = -1, i;
91 	int f = -1;
92 	gdIOCtx *ctx = NULL;
93 	zval *to_zval = NULL;
94 	php_stream *stream;
95 	int close_stream = 1;
96 
97 	/* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
98 	 * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
99 	 * from imagey<type>().
100 	 */
101 	if (image_type == PHP_GDIMG_TYPE_XBM) {
102 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
103 			return;
104 		}
105 	} else {
106 		/* PHP_GDIMG_TYPE_GIF
107 		 * PHP_GDIMG_TYPE_PNG
108 		 * PHP_GDIMG_TYPE_JPG
109 		 * PHP_GDIMG_TYPE_WBM
110 		 * PHP_GDIMG_TYPE_WEBP
111 		 * */
112 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z/!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) {
113 			return;
114 		}
115 	}
116 
117 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", phpi_get_le_gd())) == NULL) {
118 		RETURN_FALSE;
119 	}
120 
121 	if (argc >= 3) {
122 		q = quality; /* or colorindex for foreground of BW images (defaults to black) */
123 		if (argc == 4) {
124 			f = basefilter;
125 		}
126 	}
127 
128 	if (argc > 1 && to_zval != NULL) {
129 		if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
130 			php_stream_from_zval_no_verify(stream, to_zval);
131 			if (stream == NULL) {
132 				RETURN_FALSE;
133 			}
134 			close_stream = 0;
135 		} else if (Z_TYPE_P(to_zval) == IS_STRING) {
136 			if (CHECK_ZVAL_NULL_PATH(to_zval)) {
137 				php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes");
138 				RETURN_FALSE;
139 			}
140 
141 			stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
142 			if (stream == NULL) {
143 				RETURN_FALSE;
144 			}
145 		} else {
146 			php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
147 			RETURN_FALSE;
148 		}
149 	} else if (argc > 1 && file != NULL) {
150 		stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
151 		if (stream == NULL) {
152 			RETURN_FALSE;
153 		}
154 	} else {
155 		ctx = ecalloc(1, sizeof(gdIOCtx));
156 		ctx->putC = _php_image_output_putc;
157 		ctx->putBuf = _php_image_output_putbuf;
158 		ctx->gd_free = _php_image_output_ctxfree;
159 
160 #if APACHE && defined(CHARSET_EBCDIC)
161 		/* XXX this is unlikely to work any more thies@thieso.net */
162 		/* This is a binary file already: avoid EBCDIC->ASCII conversion */
163 		ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
164 #endif
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 		default:
211 			(*func_p)(im, ctx);
212 			break;
213 	}
214 
215 	ctx->gd_free(ctx);
216 
217 	RETURN_TRUE;
218 }
219 /* }}} */
220 
221 /*
222  * Local variables:
223  * tab-width: 4
224  * c-basic-offset: 4
225  * End:
226  * vim600: sw=4 ts=4 fdm=marker
227  * vim<600: sw=4 ts=4
228  */
229