From: jim winstead Date: Wed, 2 Jan 2002 01:25:48 +0000 (+0000) Subject: jpeg2wbmp,png2wbmp: fix prototypes; _php_image_convert: simplify argument count checking X-Git-Tag: PRE_ISSET_PATCH~334 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8e6a16984a29c16acefbe1af7f6b2d7b2cf2d1ed;p=php jpeg2wbmp,png2wbmp: fix prototypes; _php_image_convert: simplify argument count checking --- diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 6cb86db2cd..fd2458d338 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -3328,7 +3328,7 @@ PHP_FUNCTION(image2wbmp) } /* }}} */ -/* {{{ proto void jpeg2wbmp (string f_org, string f_dest, int d_height, int d_width) +/* {{{ proto void jpeg2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold) Convert JPEG image to WBMP image */ PHP_FUNCTION(jpeg2wbmp) { @@ -3346,7 +3346,7 @@ PHP_FUNCTION(jpeg2wbmp) } /* }}} */ -/* {{{ proto void png2wbmp (string f_org, string f_dest, int d_height, int d_width) +/* {{{ proto void png2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold) Convert PNG image to WBMP image */ PHP_FUNCTION(png2wbmp) { @@ -3436,179 +3436,176 @@ static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type ) float x_ratio, y_ratio; argc = ZEND_NUM_ARGS(); - if (argc < 1 || argc > 5 || zend_get_parameters_ex(argc, &f_org, &f_dest, &height, &width, &threshold) == FAILURE) { + if (argc != 5 || zend_get_parameters_ex(argc, &f_org, &f_dest, &height, &width, &threshold) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } - if (argc == 5) { - convert_to_string_ex (f_org); - convert_to_string_ex (f_dest); - fn_org = Z_STRVAL_PP(f_org); - fn_dest = Z_STRVAL_PP(f_dest); - convert_to_long_ex(height); - dest_height = Z_LVAL_PP(height); - convert_to_long_ex(width); - dest_width = Z_LVAL_PP(width); - convert_to_long_ex(threshold); - int_threshold = Z_LVAL_PP(threshold); - - /* Check threshold value */ - if( int_threshold < 0 || int_threshold > 8 ) { - php_error (E_WARNING, "Invalid threshold value '%d' in %s", int_threshold, get_active_function_name(TSRMLS_C)); - RETURN_FALSE; - } + convert_to_string_ex (f_org); + convert_to_string_ex (f_dest); + fn_org = Z_STRVAL_PP(f_org); + fn_dest = Z_STRVAL_PP(f_dest); + convert_to_long_ex(height); + dest_height = Z_LVAL_PP(height); + convert_to_long_ex(width); + dest_width = Z_LVAL_PP(width); + convert_to_long_ex(threshold); + int_threshold = Z_LVAL_PP(threshold); + + /* Check threshold value */ + if( int_threshold < 0 || int_threshold > 8 ) { + php_error (E_WARNING, "Invalid threshold value '%d' in %s", int_threshold, get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } - /* Check origin file */ - if (!fn_org || fn_org == empty_string || php_check_open_basedir(fn_org TSRMLS_CC)) { - php_error (E_WARNING, "%s: invalid origin filename '%s'", get_active_function_name(TSRMLS_C), fn_org); - RETURN_FALSE; - } + /* Check origin file */ + if (!fn_org || fn_org == empty_string || php_check_open_basedir(fn_org TSRMLS_CC)) { + php_error (E_WARNING, "%s: invalid origin filename '%s'", get_active_function_name(TSRMLS_C), fn_org); + RETURN_FALSE; + } - /* Check destination file */ - if (!fn_dest || fn_dest == empty_string || php_check_open_basedir(fn_dest TSRMLS_CC)) { - php_error (E_WARNING, "%s: invalid destination filename '%s'", get_active_function_name(TSRMLS_C), fn_dest); - RETURN_FALSE; - } + /* Check destination file */ + if (!fn_dest || fn_dest == empty_string || php_check_open_basedir(fn_dest TSRMLS_CC)) { + php_error (E_WARNING, "%s: invalid destination filename '%s'", get_active_function_name(TSRMLS_C), fn_dest); + RETURN_FALSE; + } - /* Open origin file */ - org = VCWD_FOPEN(fn_org, "rb"); - if (!org) { - php_error (E_WARNING, "%s: unable to open '%s' for reading", get_active_function_name(TSRMLS_C), fn_org); - RETURN_FALSE; - } + /* Open origin file */ + org = VCWD_FOPEN(fn_org, "rb"); + if (!org) { + php_error (E_WARNING, "%s: unable to open '%s' for reading", get_active_function_name(TSRMLS_C), fn_org); + RETURN_FALSE; + } - /* Open destination file */ - dest = VCWD_FOPEN(fn_dest, "wb"); - if (!dest) { - php_error (E_WARNING, "%s: unable to open '%s' for writing", get_active_function_name(TSRMLS_C), fn_dest); - RETURN_FALSE; - } + /* Open destination file */ + dest = VCWD_FOPEN(fn_dest, "wb"); + if (!dest) { + php_error (E_WARNING, "%s: unable to open '%s' for writing", get_active_function_name(TSRMLS_C), fn_dest); + RETURN_FALSE; + } - switch (image_type) { + switch (image_type) { #ifdef HAVE_GD_GIF_READ - case PHP_GDIMG_TYPE_GIF: - im_org = gdImageCreateFromGif (org); - if (im_org == NULL) { - php_error (E_WARNING, "%s: unable to open '%s' Not a valid GIF file", get_active_function_name(TSRMLS_C), fn_dest); - RETURN_FALSE; - } - break; + case PHP_GDIMG_TYPE_GIF: + im_org = gdImageCreateFromGif (org); + if (im_org == NULL) { + php_error (E_WARNING, "%s: unable to open '%s' Not a valid GIF file", get_active_function_name(TSRMLS_C), fn_dest); + RETURN_FALSE; + } + break; #endif /* HAVE_GD_GIF_READ */ #ifdef HAVE_GD_JPG - case PHP_GDIMG_TYPE_JPG: - im_org = gdImageCreateFromJpeg (org); - if (im_org == NULL) { - php_error (E_WARNING, "%s: unable to open '%s' Not a valid JPEG file", get_active_function_name(TSRMLS_C), fn_dest); - RETURN_FALSE; - } - break; + case PHP_GDIMG_TYPE_JPG: + im_org = gdImageCreateFromJpeg (org); + if (im_org == NULL) { + php_error (E_WARNING, "%s: unable to open '%s' Not a valid JPEG file", get_active_function_name(TSRMLS_C), fn_dest); + RETURN_FALSE; + } + break; #endif /* HAVE_GD_JPG */ #ifdef HAVE_GD_PNG - case PHP_GDIMG_TYPE_PNG: - im_org = gdImageCreateFromPng(org); - if (im_org == NULL) { - php_error (E_WARNING, "%s: unable to open '%s' Not a valid PNG file", get_active_function_name(TSRMLS_C), fn_dest); - RETURN_FALSE; - } - break; + case PHP_GDIMG_TYPE_PNG: + im_org = gdImageCreateFromPng(org); + if (im_org == NULL) { + php_error (E_WARNING, "%s: unable to open '%s' Not a valid PNG file", get_active_function_name(TSRMLS_C), fn_dest); + RETURN_FALSE; + } + break; #endif /* HAVE_GD_PNG */ - default: - php_error(E_WARNING, "%s: Format not supported", get_active_function_name(TSRMLS_C)); - RETURN_FALSE; - break; - } + default: + php_error(E_WARNING, "%s: Format not supported", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + break; + } - org_width = gdImageSX (im_org); - org_height = gdImageSY (im_org); + org_width = gdImageSX (im_org); + org_height = gdImageSY (im_org); - x_ratio = (float) org_width / (float) dest_width; - y_ratio = (float) org_height / (float) dest_height; + x_ratio = (float) org_width / (float) dest_width; + y_ratio = (float) org_height / (float) dest_height; - if (x_ratio > 1 && y_ratio > 1) { - if (y_ratio > x_ratio) { - x_ratio = y_ratio; - } - else { - y_ratio = x_ratio; - } - dest_width = (int)(org_width / x_ratio); - dest_height = (int)(org_height / y_ratio); + if (x_ratio > 1 && y_ratio > 1) { + if (y_ratio > x_ratio) { + x_ratio = y_ratio; } else { - x_ratio = (float) dest_width / (float) org_width; - y_ratio = (float) dest_height / (float) org_height; - - if (y_ratio < x_ratio) { - x_ratio = y_ratio; - } - else { - y_ratio = x_ratio; - } - dest_width = (int)(org_width * x_ratio); - dest_height = (int)(org_height * y_ratio); + y_ratio = x_ratio; } + dest_width = (int)(org_width / x_ratio); + dest_height = (int)(org_height / y_ratio); + } + else { + x_ratio = (float) dest_width / (float) org_width; + y_ratio = (float) dest_height / (float) org_height; - im_tmp = gdImageCreate (dest_width, dest_height); - if (im_tmp == NULL ) { - php_error(E_WARNING, "%s: unable to allocate temporary buffer", get_active_function_name(TSRMLS_C)); - RETURN_FALSE; + if (y_ratio < x_ratio) { + x_ratio = y_ratio; } + else { + y_ratio = x_ratio; + } + dest_width = (int)(org_width * x_ratio); + dest_height = (int)(org_height * y_ratio); + } - gdImageCopyResized (im_tmp, im_org, 0, 0, 0, 0, dest_width, dest_height, org_width, org_height); + im_tmp = gdImageCreate (dest_width, dest_height); + if (im_tmp == NULL ) { + php_error(E_WARNING, "%s: unable to allocate temporary buffer", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } - gdImageDestroy(im_org); + gdImageCopyResized (im_tmp, im_org, 0, 0, 0, 0, dest_width, dest_height, org_width, org_height); - fclose(org); + gdImageDestroy(im_org); - im_dest = gdImageCreate(dest_width, dest_height); - if (im_dest == NULL) { - php_error(E_WARNING, "%s: unable to allocate destination buffer", get_active_function_name(TSRMLS_C)); - RETURN_FALSE; - } - white = gdImageColorAllocate(im_dest, 255, 255, 255); - if (white == -1) { - php_error(E_WARNING, "%s: unable to allocate the colors for the destination buffer", get_active_function_name(TSRMLS_C)); - RETURN_FALSE; - } + fclose(org); - black = gdImageColorAllocate(im_dest, 0, 0, 0); - if (black == -1) { - php_error(E_WARNING, "%s: unable to allocate the colors for the destination buffer", get_active_function_name(TSRMLS_C)); - RETURN_FALSE; - } + im_dest = gdImageCreate(dest_width, dest_height); + if (im_dest == NULL) { + php_error(E_WARNING, "%s: unable to allocate destination buffer", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } + white = gdImageColorAllocate(im_dest, 255, 255, 255); + if (white == -1) { + php_error(E_WARNING, "%s: unable to allocate the colors for the destination buffer", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } - int_threshold = int_threshold * 32; + black = gdImageColorAllocate(im_dest, 0, 0, 0); + if (black == -1) { + php_error(E_WARNING, "%s: unable to allocate the colors for the destination buffer", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } - for (y = 0; y < dest_height; y++) { - for(x = 0; x < dest_width; x++) { - color_org = gdImageGetPixel (im_tmp, x, y); - median = (im_tmp->red[color_org] + im_tmp->green[color_org] + im_tmp->blue[color_org]) / 3; - if (median < int_threshold) { - color = black; - } - else { - color = white; - } - gdImageSetPixel (im_dest, x, y, color); + int_threshold = int_threshold * 32; + + for (y = 0; y < dest_height; y++) { + for(x = 0; x < dest_width; x++) { + color_org = gdImageGetPixel (im_tmp, x, y); + median = (im_tmp->red[color_org] + im_tmp->green[color_org] + im_tmp->blue[color_org]) / 3; + if (median < int_threshold) { + color = black; + } + else { + color = white; } + gdImageSetPixel (im_dest, x, y, color); } + } - gdImageDestroy (im_tmp ); + gdImageDestroy (im_tmp ); - gdImageWBMP (im_dest, black , dest); + gdImageWBMP (im_dest, black , dest); - fflush(dest); - fclose(dest); + fflush(dest); + fclose(dest); - gdImageDestroy( im_dest ); - - RETURN_TRUE; - } - ZEND_WRONG_PARAM_COUNT(); + gdImageDestroy( im_dest ); + + RETURN_TRUE; } /* }}} */ #endif /* HAVE_GD_WBMP */