2003-06-15 20:00:08 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 08:18:22 +00:00
|
|
|
| PHP Version 5 |
|
2003-06-15 20:00:08 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2007-12-31 07:17:19 +00:00
|
|
|
| Copyright (c) 1997-2008 The PHP Group |
|
2003-06-15 20:00:08 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 12:51:34 +00:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2003-06-15 20:00:08 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 12:51:34 +00:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2003-06-15 20:00:08 +00:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Stanislav Malyshev <stas@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
2001-05-18 20:32:47 +00:00
|
|
|
|
2003-06-15 20:00:08 +00:00
|
|
|
#include "php_gd.h"
|
2001-05-18 20:32:47 +00:00
|
|
|
|
2001-02-01 15:40:35 +00:00
|
|
|
#define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
|
2003-12-28 21:08:46 +00:00
|
|
|
|
2001-02-01 15:40:35 +00:00
|
|
|
static void _php_image_output_putc(struct gdIOCtx *ctx, int c)
|
|
|
|
{
|
2004-01-28 16:25:12 +00:00
|
|
|
/* without the following downcast, the write will fail
|
|
|
|
* (i.e., will write a zero byte) for all
|
|
|
|
* big endian architectures:
|
|
|
|
*/
|
|
|
|
unsigned char ch = (unsigned char) c;
|
2001-08-05 16:21:33 +00:00
|
|
|
TSRMLS_FETCH();
|
2004-01-28 16:25:12 +00:00
|
|
|
php_write(&ch, 1 TSRMLS_CC);
|
2001-02-01 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
|
|
|
|
{
|
2001-08-05 16:21:33 +00:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
return php_write((void *)buf, l TSRMLS_CC);
|
2001-02-01 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _php_image_output_ctxfree(struct gdIOCtx *ctx)
|
|
|
|
{
|
|
|
|
if(ctx) {
|
|
|
|
efree(ctx);
|
|
|
|
}
|
|
|
|
}
|
2003-06-15 20:00:08 +00:00
|
|
|
|
2003-12-28 21:08:46 +00:00
|
|
|
/* {{{ _php_image_output_ctx */
|
|
|
|
static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
|
2001-02-01 15:40:35 +00:00
|
|
|
{
|
2008-08-04 18:23:33 +00:00
|
|
|
zval *imgind;
|
|
|
|
char *file = NULL;
|
|
|
|
int file_len = 0;
|
|
|
|
long quality, basefilter;
|
2001-02-01 15:40:35 +00:00
|
|
|
gdImagePtr im;
|
|
|
|
FILE *fp = NULL;
|
|
|
|
int argc = ZEND_NUM_ARGS();
|
2001-04-01 05:42:07 +00:00
|
|
|
int q = -1, i;
|
2006-01-27 13:36:30 +00:00
|
|
|
int f = -1;
|
2001-02-01 15:40:35 +00:00
|
|
|
gdIOCtx *ctx;
|
|
|
|
|
2003-06-15 20:00:08 +00:00
|
|
|
/* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
|
|
|
|
* The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
|
|
|
|
* from imagey<type>().
|
|
|
|
*/
|
2003-12-28 21:08:46 +00:00
|
|
|
|
2008-08-04 18:23:33 +00:00
|
|
|
if (image_type == PHP_GDIMG_TYPE_XBM) {
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* PHP_GDIMG_TYPE_GIF
|
|
|
|
* PHP_GDIMG_TYPE_PNG
|
|
|
|
* PHP_GDIMG_TYPE_JPG
|
|
|
|
* PHP_GDIMG_TYPE_WBM */
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2001-02-01 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
2008-08-04 18:23:33 +00:00
|
|
|
ZEND_FETCH_RESOURCE(im, gdImagePtr, &imgind, -1, "Image", phpi_get_le_gd());
|
2001-02-01 15:40:35 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
2006-01-27 13:36:30 +00:00
|
|
|
if (argc >= 3) {
|
2008-08-04 18:23:33 +00:00
|
|
|
q = quality; /* or colorindex for foreground of BW images (defaults to black) */
|
2006-01-27 13:36:30 +00:00
|
|
|
if (argc == 4) {
|
2008-08-04 18:23:33 +00:00
|
|
|
f = basefilter;
|
2006-01-27 13:36:30 +00:00
|
|
|
}
|
2001-02-01 15:40:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-04 18:23:33 +00:00
|
|
|
if (argc > 1 && file_len) {
|
|
|
|
PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
|
2001-02-01 15:40:35 +00:00
|
|
|
|
2008-08-04 18:23:33 +00:00
|
|
|
fp = VCWD_FOPEN(file, "wb");
|
2001-02-01 15:40:35 +00:00
|
|
|
if (!fp) {
|
2008-08-04 18:23:33 +00:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing: %s", file, strerror(errno));
|
2001-02-01 15:40:35 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = gdNewFileCtx(fp);
|
|
|
|
} else {
|
|
|
|
ctx = emalloc(sizeof(gdIOCtx));
|
|
|
|
ctx->putC = _php_image_output_putc;
|
|
|
|
ctx->putBuf = _php_image_output_putbuf;
|
2002-10-30 01:05:16 +00:00
|
|
|
#if HAVE_LIBGD204
|
2002-10-29 23:08:01 +00:00
|
|
|
ctx->gd_free = _php_image_output_ctxfree;
|
2002-10-30 01:05:16 +00:00
|
|
|
#else
|
|
|
|
ctx->free = _php_image_output_ctxfree;
|
2003-12-28 21:08:46 +00:00
|
|
|
#endif
|
2001-02-01 15:40:35 +00:00
|
|
|
|
|
|
|
#if APACHE && defined(CHARSET_EBCDIC)
|
2001-02-15 14:49:01 +00:00
|
|
|
/* XXX this is unlikely to work any more thies@thieso.net */
|
2001-02-01 15:40:35 +00:00
|
|
|
/* This is a binary file already: avoid EBCDIC->ASCII conversion */
|
|
|
|
ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(image_type) {
|
2001-04-01 05:42:07 +00:00
|
|
|
case PHP_GDIMG_CONVERT_WBM:
|
|
|
|
if(q<0||q>255) {
|
2002-08-22 18:38:44 +00:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
|
2001-04-01 05:42:07 +00:00
|
|
|
}
|
2001-02-01 15:40:35 +00:00
|
|
|
case PHP_GDIMG_TYPE_JPG:
|
|
|
|
(*func_p)(im, ctx, q);
|
|
|
|
break;
|
2006-01-27 13:36:30 +00:00
|
|
|
case PHP_GDIMG_TYPE_PNG:
|
|
|
|
(*func_p)(im, ctx, q, f);
|
|
|
|
break;
|
2003-06-15 20:00:08 +00:00
|
|
|
case PHP_GDIMG_TYPE_XBM:
|
2001-02-01 15:40:35 +00:00
|
|
|
case PHP_GDIMG_TYPE_WBM:
|
2003-06-15 20:00:08 +00:00
|
|
|
if (argc < 3) {
|
|
|
|
for(i=0; i < gdImageColorsTotal(im); i++) {
|
|
|
|
if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
|
|
|
|
}
|
|
|
|
q = i;
|
|
|
|
}
|
|
|
|
if (image_type == PHP_GDIMG_TYPE_XBM) {
|
2008-08-04 18:23:33 +00:00
|
|
|
(*func_p)(im, file, q, ctx);
|
2003-06-15 20:00:08 +00:00
|
|
|
} else {
|
|
|
|
(*func_p)(im, q, ctx);
|
|
|
|
}
|
2001-04-01 05:42:07 +00:00
|
|
|
break;
|
2001-02-01 15:40:35 +00:00
|
|
|
default:
|
|
|
|
(*func_p)(im, ctx);
|
|
|
|
break;
|
|
|
|
}
|
2002-10-30 01:05:16 +00:00
|
|
|
|
2003-12-28 21:08:46 +00:00
|
|
|
#if HAVE_LIBGD204
|
2002-10-29 23:08:01 +00:00
|
|
|
ctx->gd_free(ctx);
|
2002-10-30 01:05:16 +00:00
|
|
|
#else
|
|
|
|
ctx->free(ctx);
|
2003-12-28 21:08:46 +00:00
|
|
|
#endif
|
2001-02-01 15:40:35 +00:00
|
|
|
|
|
|
|
if(fp) {
|
|
|
|
fflush(fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2003-12-28 21:08:46 +00:00
|
|
|
|
2001-02-01 15:40:35 +00:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
2003-06-15 20:00:08 +00:00
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
|
|
|
*/
|