/* {{{ _php_image_output_ctx */ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()) { zval *imgind; char *file = NULL; int file_len = 0; long quality, basefilter; gdImagePtr im; FILE *fp = NULL; int argc = ZEND_NUM_ARGS(); int q = -1, i; int f = -1; gdIOCtx *ctx; /* 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>(). */ 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; } } ZEND_FETCH_RESOURCE(im, gdImagePtr, &imgind, -1, "Image", phpi_get_le_gd()); if (argc > 1) { if (argc >= 3) { q = quality; /* or colorindex for foreground of BW images (defaults to black) */ if (argc == 4) { f = basefilter; } } } if (argc > 1 && file_len) { PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename"); fp = VCWD_FOPEN(file, "wb"); if (!fp) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing: %s", file, strerror(errno)); RETURN_FALSE; } ctx = gdNewFileCtx(fp); } else { ctx = emalloc(sizeof(gdIOCtx)); ctx->putC = _php_image_output_putc; ctx->putBuf = _php_image_output_putbuf; ctx->gd_free = _php_image_output_ctxfree; } switch(image_type) { case PHP_GDIMG_CONVERT_WBM: if(q<0||q>255) { php_error_docref(NULL TSRMLS_CC, 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_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, q, ctx); } else { (*func_p)(im, q, ctx); } break; default: (*func_p)(im, ctx); break; } ctx->gd_free(ctx); if(fp) { fflush(fp); fclose(fp); } RETURN_TRUE; }
/* {{{ _php_image_output_ctx */ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()) { zval *imgind; char *file = NULL; int file_len = 0; long quality, basefilter; gdImagePtr im; int argc = ZEND_NUM_ARGS(); int q = -1, i; int f = -1; gdIOCtx *ctx = NULL; zval *to_zval = NULL; php_stream *stream; /* 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>(). */ if (image_type == PHP_GDIMG_TYPE_XBM) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rp!|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 * PHP_GDIMG_TYPE_WEBP * */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z/!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) { return; } } ZEND_FETCH_RESOURCE(im, gdImagePtr, &imgind, -1, "Image", phpi_get_le_gd()); if (argc >= 3) { q = quality; /* or colorindex for foreground of BW images (defaults to black) */ 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; } } else if (Z_TYPE_P(to_zval) == IS_STRING) { 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 TSRMLS_CC, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream"); RETURN_FALSE; } } else { ctx = emalloc(sizeof(gdIOCtx)); ctx->putC = _php_image_output_putc; ctx->putBuf = _php_image_output_putbuf; ctx->gd_free = _php_image_output_ctxfree; #if APACHE && defined(CHARSET_EBCDIC) /* XXX this is unlikely to work any more [email protected] */ /* This is a binary file already: avoid EBCDIC->ASCII conversion */ ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0); #endif } if (!ctx) { ctx = emalloc(sizeof(gdIOCtx)); ctx->putC = _php_image_stream_putc; ctx->putBuf = _php_image_stream_putbuf; 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 TSRMLS_CC, 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, q, ctx); } else { (*func_p)(im, q, ctx); } break; default: (*func_p)(im, ctx); break; } ctx->gd_free(ctx); RETURN_TRUE; }
static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()) { zval **imgind, **file, **quality; gdImagePtr im; char *fn = NULL; FILE *fp = NULL; int argc = ZEND_NUM_ARGS(); int q = -1, i; gdIOCtx *ctx; /* The quality parameter for Wbmp stands for the threshold when called from image2wbmp() */ if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &imgind, &file, &quality) == FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(im, gdImagePtr, imgind, -1, "Image", phpi_get_le_gd()); if (argc > 1) { convert_to_string_ex(file); fn = Z_STRVAL_PP(file); if (argc == 3) { convert_to_long_ex(quality); q = Z_LVAL_PP(quality); } } if ((argc == 2) || (argc > 2 && Z_STRLEN_PP(file))) { PHP_GD_CHECK_OPEN_BASEDIR(fn, "Invalid filename"); fp = VCWD_FOPEN(fn, "wb"); if (!fp) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing", fn); RETURN_FALSE; } ctx = gdNewFileCtx(fp); } else { ctx = emalloc(sizeof(gdIOCtx)); ctx->putC = _php_image_output_putc; ctx->putBuf = _php_image_output_putbuf; #if HAVE_LIBGD204 ctx->gd_free = _php_image_output_ctxfree; #else ctx->free = _php_image_output_ctxfree; #endif #if APACHE && defined(CHARSET_EBCDIC) /* XXX this is unlikely to work any more [email protected] */ /* This is a binary file already: avoid EBCDIC->ASCII conversion */ ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0); #endif } switch(image_type) { case PHP_GDIMG_CONVERT_WBM: if(q<0||q>255) { php_error_docref(NULL TSRMLS_CC, 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_WBM: for(i=0; i < gdImageColorsTotal(im); i++) { if(gdImageRed(im, i) == 0) break; } (*func_p)(im, i, ctx); break; default: (*func_p)(im, ctx); break; } #if HAVE_LIBGD204 ctx->gd_free(ctx); #else ctx->free(ctx); #endif if(fp) { fflush(fp); fclose(fp); } RETURN_TRUE; }
/* {{{ proto resource clmandelbrot(int width, int height[, float unit]) */ static PHP_FUNCTION(clmandelbrot) { long width = 0; long height = 0; double unit = 0.0; long device = 0; zend_fcall_info fci; zend_fcall_info_cache fcc; zval *zim = NULL, *callable, *args, *zwidth, *zheight; int err; gdImagePtr im; RETVAL_FALSE; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|dl", &width, &height, &unit, &device) == FAILURE) { return; } MAKE_STD_ZVAL(callable); ZVAL_STRING(callable, "imagecreatetruecolor", 1); err = zend_fcall_info_init(callable, 0, &fci, &fcc, NULL, NULL TSRMLS_CC); if (err != SUCCESS) { zval_ptr_dtor(&callable); return; } MAKE_STD_ZVAL(args); MAKE_STD_ZVAL(zwidth); MAKE_STD_ZVAL(zheight); ZVAL_LONG(zwidth, width); ZVAL_LONG(zheight, height); array_init(args); add_next_index_zval(args, zwidth); add_next_index_zval(args, zheight); zend_fcall_info_call(&fci, &fcc, &zim, args TSRMLS_CC); zval_ptr_dtor(&callable); zval_ptr_dtor(&args); if (!zim) { return; } ZEND_FETCH_RESOURCE_NO_RETURN(im, gdImagePtr, &zim, -1, "Image", phpi_get_le_gd()); if (im) { size_t len; clmandelbrot_t ctx = { 0 }; ctx.deviceId = (cl_uint)device; ctx.width = gdImageSX(im); ctx.height = gdImageSY(im); if (unit > 0.0) { ctx.unit = (float)unit; } else { ctx.unit = 10.0f / (float)(ctx.width + ctx.height); } len = ctx.width * ctx.height; ctx.bitmap = ecalloc(len, sizeof(unsigned char)); if (!ctx.bitmap) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot allocate memory"); } else { if (clm_process(im, &ctx TSRMLS_CC) == SUCCESS) { RETVAL_ZVAL(zim, 1, 0); } } clm_release(&ctx); } zval_ptr_dtor(&zim); }
/* {{{ _php_image_output_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; /* The third (quality) parameter for Wbmp stands for the foreground when called from image2wbmp(). * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called * from imagey<type>(). */ 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: /* PHP_GDIMG_TYPE_GIF * PHP_GDIMG_TYPE_PNG * PHP_GDIMG_TYPE_JPG * PHP_GDIMG_TYPE_WBM * PHP_GDIMG_TYPE_WEBP * */ 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; /* or colorindex for foreground of BW images (defaults to black) */ 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; }