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