derick Wed Jan 24 01:24:27 2001 EDT Modified files: /php4/ext/gd gd.c php_gd.h Log: - Commited a patch that adds support for converting Images to WBMP format. (Thanx to Denis Gasparin <[EMAIL PROTECTED]>) @- Added support for converting images from JPG/PNG on the fly with the GD @ extension, which is usefull for creating dynamic WAP-sites (Derick)
Index: php4/ext/gd/gd.c diff -u php4/ext/gd/gd.c:1.106 php4/ext/gd/gd.c:1.107 --- php4/ext/gd/gd.c:1.106 Tue Jan 16 14:34:19 2001 +++ php4/ext/gd/gd.c Wed Jan 24 01:24:26 2001 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: gd.c,v 1.106 2001/01/16 22:34:19 sniper Exp $ */ +/* $Id: gd.c,v 1.107 2001/01/24 09:24:26 derick Exp $ */ /* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center, Cold Spring Harbor Labs. */ @@ -176,6 +176,11 @@ PHP_FE(imagepstext, NULL) PHP_FE(imagepsbbox, NULL) PHP_FE(imagetypes, NULL) + + PHP_FE(jpeg2wbmp, + NULL) + PHP_FE(png2wbmp, + NULL) + PHP_FE(image2wbmp, + NULL) + {NULL, NULL, NULL} }; @@ -788,6 +793,9 @@ int output = 1, q = -1; GDLS_FETCH(); + /* The quality parameter for Wbmp stands for the threshold + So the q variable */ + if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &imgind, &file, &quality) == FAILURE) { WRONG_PARAM_COUNT; @@ -815,10 +823,20 @@ php_error(E_WARNING, "%s: unable to open '%s' for writing", get_active_function_name(), fn); RETURN_FALSE; } - if (image_type == PHP_GDIMG_TYPE_JPG) { - (*func_p)(im, fp, q); - } else { - (*func_p)(im, fp); + + switch(image_type) { + case PHP_GDIMG_TYPE_JPG: + (*func_p)(im, fp, q); + break; + case PHP_GDIMG_TYPE_WBM: + if(q<0||q>255) { + php_error(E_WARNING, "%s: invalid threshold +value '%d'. It must be between 0 and 255",get_active_function_name(), q); + } + (*func_p)(im, q, fp); + break; + default: + (*func_p)(im, fp); + break; } fflush(fp); fclose(fp); @@ -834,11 +852,21 @@ } output = php_header(); if (output) { - if (image_type == PHP_GDIMG_TYPE_JPG) { - (*func_p)(im, tmp, q); - } else { - (*func_p)(im, tmp); + switch(image_type) { + case PHP_GDIMG_TYPE_JPG: + (*func_p)(im, tmp, q); + break; + case PHP_GDIMG_TYPE_WBM: + if(q<0||q>255) { + php_error(E_WARNING, "%s: invalid +threshold value '%d'. It must be between 0 and 255",get_active_function_name(), q); + } + (*func_p)(im, q, fp); + break; + default: + (*func_p)(im, fp); + break; } + fseek(tmp, 0, SEEK_SET); #if APACHE && defined(CHARSET_EBCDIC) SLS_FETCH(); @@ -2617,8 +2645,270 @@ } /* }}} */ -#endif /* HAVE_LIBGD */ +/* {{{ proto int image2wbmp(int im [, string filename [, int threshold]]) + Output WBMP image to browser or file */ +PHP_FUNCTION(image2wbmp) +{ +#ifdef HAVE_GD_WBMP + _php_image_output (INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, +"WBMP", _php_image_bw_convert); +#else /* HAVE_GD_WBMP */ + php_error(E_WARNING, "Image2Wbmp: No WBMP support in this PHP build"); + RETURN_FALSE; +#endif /* HAVE_GD_WBMP */ +} + + +/* {{{ proto void jpeg2wbmp (string f_org, string f_dest, d_height, d_width) + Convert Jpeg Image to Wbmp image */ +PHP_FUNCTION(jpeg2wbmp) +{ +#ifdef HAVE_GD_JPG + _php_image_convert (INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG); +#else /* HAVE_GD_JPG */ + php_error(E_WARNING, "jpeg2wbmp: No JPG support in this PHP build"); + RETURN_FALSE; +#endif /* HAVE_GD_JPG */ +} +/* }}} */ + +/* {{{ proto void png2wbmp (string f_org, string f_dest, d_height, d_width) + Convert Png Image to Wbmp image */ +PHP_FUNCTION(png2wbmp) +{ +#ifdef HAVE_GD_PNG + _php_image_convert (INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG); +#else /* HAVE_GD_PNG */ + php_error(E_WARNING, "png2wbmp: No PNG support in this PHP build"); + RETURN_FALSE; +#endif /* HAVE_GD_PNG */ +} +/* }}} */ + + +/* It converts a gd Image to bw using a threshold value */ +static void _php_image_bw_convert( gdImagePtr im_org, int threshold, FILE *out) { + gdImagePtr im_dest; + int white, black; + int color, color_org, median; + int dest_height = gdImageSY (im_org); + int dest_width = gdImageSX (im_org); + int x,y; + + im_dest = gdImageCreate (dest_width, dest_height); + if (im_dest == NULL) { + php_error (E_WARNING, "%s: unable to allocate temporary buffer", +get_active_function_name()); + return; + } + 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()); + return; + } + + 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()); + return; + } + + for (y = 0; y < dest_height; y++) { + for (x = 0; x < dest_width; x++) { + color_org = gdImageGetPixel (im_org, x, y); + median = (im_org->red[color_org] + im_org->green[color_org] + +im_org->blue[color_org]) / 3; + if (median < threshold) { + color = black; + } + else { + color = white; + } + gdImageSetPixel (im_dest, x, y, color); + } + } + + gdImageWBMP (im_dest, black, out); +} + + +/* _php_image_convert converts jpeg/png images to wbmp and resizes them as needed */ +static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type ) { + zval **f_org, **f_dest, **height, **width, **threshold; + gdImagePtr im_org, im_dest, im_tmp; + char *fn_org = NULL; + char *fn_dest = NULL; + FILE *org,*dest; + int argc; + int dest_height = -1; + int dest_width = -1; + int org_height, org_width; + int output = 1; + int q = -1; + int white, black; + int color, color_org, median; + int int_threshold; + int x, y; + float x_ratio, y_ratio; + GDLS_FETCH(); + + argc = ZEND_NUM_ARGS(); + if (argc < 1 || argc > 5 || zend_get_parameters_ex(argc, &f_org, &f_dest, +&height, &width, &threshold) == FAILURE) { + 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()); + RETURN_FALSE; + } + + /* Check origin file */ + if (!fn_org || fn_org == empty_string || +php_check_open_basedir(fn_org)) { + php_error (E_WARNING, "%s: invalid origin filename '%s'", +get_active_function_name(), fn_org); + RETURN_FALSE; + } + + /* Check destination file */ + if (!fn_dest || fn_dest == empty_string || +php_check_open_basedir(fn_dest)) { + php_error (E_WARNING, "%s: invalid destination filename '%s'", +get_active_function_name(), fn_dest); + RETURN_FALSE; + } + + /* Open origin file */ + org = V_FOPEN(fn_org, "rb"); + if (!org) { + php_error (E_WARNING, "%s: unable to open '%s' for reading", +get_active_function_name(), fn_org); + RETURN_FALSE; + } + + /* Open destination file */ + dest = V_FOPEN(fn_dest, "wb"); + if (!dest) { + php_error (E_WARNING, "%s: unable to open '%s' for writing", +get_active_function_name(), fn_dest); + RETURN_FALSE; + } + + switch (image_type) { + 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(), 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(), fn_dest); + RETURN_FALSE; + } + break; + default: + php_error(E_WARNING, "%s: Format not supported", +get_active_function_name()); + break; + } + + 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; + + if (x_ratio > 1 && y_ratio > 1) { + if (y_ratio > x_ratio) { + x_ratio = y_ratio; + } + else { + y_ratio = x_ratio; + } + dest_width = org_width / x_ratio; + dest_height = org_height / 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 = org_width * x_ratio; + dest_height = org_height * y_ratio; + } + + 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()); + RETURN_FALSE; + } + + gdImageCopyResized (im_tmp, im_org, 0, 0, 0, 0, dest_width, +dest_height, org_width, org_height); + + gdImageDestroy(im_org); + + fclose(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()); + 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()); + RETURN_FALSE; + } + + 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()); + RETURN_FALSE; + } + + 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 ); + + gdImageWBMP (im_dest, black , dest); + + fflush(dest); + fclose(dest); + + gdImageDestroy( im_dest ); + + RETURN_TRUE; + } + WRONG_PARAM_COUNT; +} + +#endif /* HAVE_LIBGD */ /* * Local variables: Index: php4/ext/gd/php_gd.h diff -u php4/ext/gd/php_gd.h:1.20 php4/ext/gd/php_gd.h:1.21 --- php4/ext/gd/php_gd.h:1.20 Sat Dec 9 16:37:43 2000 +++ php4/ext/gd/php_gd.h Wed Jan 24 01:24:26 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_gd.h,v 1.20 2000/12/10 00:37:43 sniper Exp $ */ +/* $Id: php_gd.h,v 1.21 2001/01/24 09:24:26 derick Exp $ */ #ifndef PHP_GD_H #define PHP_GD_H @@ -131,11 +131,18 @@ PHP_FUNCTION(imagepstext); PHP_FUNCTION(imagepsbbox); +PHP_FUNCTION(jpeg2wbmp); +PHP_FUNCTION(png2wbmp); +PHP_FUNCTION(image2wbmp); + + gdImagePtr _php_image_create_from_string ( zval **Data, char *tn, gdImagePtr (*ioctx_func_p)() ); static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)()); static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()); static void _php_image_output_wbmp(gdImagePtr im, FILE *fp); static int _php_image_type ( char data[8] ); +static void _php_image_bw_convert( gdImagePtr im_org, int threshold, FILE *out ); +static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type); PHPAPI int phpi_get_le_gd(void);
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]