123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "php_gd.h"
- #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
- static void _php_image_output_putc(struct gdIOCtx *ctx, int c)
- {
-
- unsigned char ch = (unsigned char) c;
- php_write(&ch, 1);
- }
- static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
- {
- return php_write((void *)buf, l);
- }
- static void _php_image_output_ctxfree(struct gdIOCtx *ctx)
- {
- if(ctx) {
- efree(ctx);
- }
- }
- static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) {
- char ch = (char) c;
- php_stream * stream = (php_stream *)ctx->data;
- php_stream_write(stream, &ch, 1);
- }
- static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l)
- {
- php_stream * stream = (php_stream *)ctx->data;
- return php_stream_write(stream, (void *)buf, l);
- }
- static void _php_image_stream_ctxfree(struct gdIOCtx *ctx)
- {
- if(ctx->data) {
- ctx->data = NULL;
- }
- if(ctx) {
- efree(ctx);
- }
- }
- static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx)
- {
- if(ctx->data) {
- php_stream_close((php_stream *) ctx->data);
- ctx->data = NULL;
- }
- if(ctx) {
- efree(ctx);
- }
- }
- static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
- {
- zval *imgind;
- char *file = NULL;
- size_t file_len = 0;
- zend_long quality, basefilter;
- zend_bool compressed = 1;
- gdImagePtr im;
- int argc = ZEND_NUM_ARGS();
- int q = -1, i;
- int f = -1;
- gdIOCtx *ctx = NULL;
- zval *to_zval = NULL;
- php_stream *stream;
- int close_stream = 1;
-
- switch (image_type) {
- case PHP_GDIMG_TYPE_XBM:
- if (zend_parse_parameters(argc, "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
- return;
- }
- break;
- case PHP_GDIMG_TYPE_BMP:
- if (zend_parse_parameters(argc, "r|z!b", &imgind, &to_zval, &compressed) == FAILURE) {
- return;
- }
- break;
- default:
-
- if (zend_parse_parameters(argc, "r|z!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) {
- return;
- }
- }
- if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", phpi_get_le_gd())) == NULL) {
- RETURN_FALSE;
- }
- if (image_type != PHP_GDIMG_TYPE_BMP && argc >= 3) {
- q = quality;
- if (argc == 4) {
- f = basefilter;
- }
- }
- if (argc > 1 && to_zval != NULL) {
- if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
- php_stream_from_zval_no_verify(stream, to_zval);
- if (stream == NULL) {
- RETURN_FALSE;
- }
- close_stream = 0;
- } else if (Z_TYPE_P(to_zval) == IS_STRING) {
- if (CHECK_ZVAL_NULL_PATH(to_zval)) {
- php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes");
- RETURN_FALSE;
- }
- stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
- if (stream == NULL) {
- RETURN_FALSE;
- }
- } else {
- php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
- RETURN_FALSE;
- }
- } else if (argc > 1 && file != NULL) {
- stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
- if (stream == NULL) {
- RETURN_FALSE;
- }
- } else {
- ctx = ecalloc(1, sizeof(gdIOCtx));
- ctx->putC = _php_image_output_putc;
- ctx->putBuf = _php_image_output_putbuf;
- ctx->gd_free = _php_image_output_ctxfree;
- }
- if (!ctx) {
- ctx = ecalloc(1, sizeof(gdIOCtx));
- ctx->putC = _php_image_stream_putc;
- ctx->putBuf = _php_image_stream_putbuf;
- if (close_stream) {
- ctx->gd_free = _php_image_stream_ctxfreeandclose;
- } else {
- ctx->gd_free = _php_image_stream_ctxfree;
- }
- ctx->data = (void *)stream;
- }
- switch(image_type) {
- case PHP_GDIMG_CONVERT_WBM:
- if(q<0||q>255) {
- php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
- }
- case PHP_GDIMG_TYPE_JPG:
- (*func_p)(im, ctx, q);
- break;
- case PHP_GDIMG_TYPE_WEBP:
- if (q == -1) {
- q = 80;
- }
- (*func_p)(im, ctx, q);
- break;
- case PHP_GDIMG_TYPE_PNG:
- (*func_p)(im, ctx, q, f);
- break;
- case PHP_GDIMG_TYPE_XBM:
- case PHP_GDIMG_TYPE_WBM:
- 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) {
- (*func_p)(im, file ? file : "", q, ctx);
- } else {
- (*func_p)(im, q, ctx);
- }
- break;
- case PHP_GDIMG_TYPE_BMP:
- (*func_p)(im, ctx, (int) compressed);
- break;
- default:
- (*func_p)(im, ctx);
- break;
- }
- ctx->gd_free(ctx);
- RETURN_TRUE;
- }
|