⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gd.c

📁 php-4.4.7学习linux时下载的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
#ifndef USE_GD_IOCTX	ioctx_func_p = NULL; /* don't allow sockets without IOCtx */#endif	/* try and avoid allocating a FILE* if the stream is not naturally a FILE* */	if (php_stream_is(stream, PHP_STREAM_IS_STDIO))	{		if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {			goto out_err;		}	} else if (ioctx_func_p) {#ifdef USE_GD_IOCTX		/* we can create an io context */		gdIOCtx* io_ctx;		size_t buff_size;		char *buff;		/* needs to be malloc (persistent) - GD will free() it later */		buff_size = php_stream_copy_to_mem(stream, &buff, PHP_STREAM_COPY_ALL, 1);		if (!buff_size) {			php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot read image data");			goto out_err;		}		io_ctx = gdNewDynamicCtxEx(buff_size, buff, 0);		if (!io_ctx) {			php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot allocate GD IO context");			goto out_err;		}		if (image_type == PHP_GDIMG_TYPE_GD2PART) {			im = (*ioctx_func_p)(io_ctx, Z_LVAL_PP(srcx), Z_LVAL_PP(srcy), Z_LVAL_PP(width), Z_LVAL_PP(height));		} else {			im = (*ioctx_func_p)(io_ctx);		}#if HAVE_LIBGD204		io_ctx->gd_free(io_ctx);#else		io_ctx->free(io_ctx);#endif#endif	}	else {		/* try and force the stream to be FILE* */		if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_TRY_HARD, (void **) &fp, REPORT_ERRORS)) {			goto out_err;		}	}	if (!im && fp) {		switch (image_type) {			case PHP_GDIMG_TYPE_GD2PART:				im = (*func_p)(fp, Z_LVAL_PP(srcx), Z_LVAL_PP(srcy), Z_LVAL_PP(width), Z_LVAL_PP(height));				break;#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)			case PHP_GDIMG_TYPE_XPM:				im = gdImageCreateFromXpm(fn);				break;#endif			default:				im = (*func_p)(fp);				break;		}		fflush(fp);	}	if (im) {		ZEND_REGISTER_RESOURCE(return_value, im, le_gd);		php_stream_close(stream);		return;	}	php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid %s file", fn, tn);out_err:	php_stream_close(stream);	RETURN_FALSE;}/* }}} */#ifdef HAVE_GD_GIF_READ/* {{{ proto resource imagecreatefromgif(string filename)   Create a new image from GIF file or URL */PHP_FUNCTION(imagecreatefromgif){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);}/* }}} */#endif /* HAVE_GD_GIF_READ */#ifdef HAVE_GD_JPG/* {{{ proto resource imagecreatefromjpeg(string filename)   Create a new image from JPEG file or URL */PHP_FUNCTION(imagecreatefromjpeg){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);}/* }}} */#endif /* HAVE_GD_JPG */#ifdef HAVE_GD_PNG/* {{{ proto resource imagecreatefrompng(string filename)   Create a new image from PNG file or URL */PHP_FUNCTION(imagecreatefrompng){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);}/* }}} */#endif /* HAVE_GD_PNG */#ifdef HAVE_GD_XBM/* {{{ proto resource imagecreatefromxbm(string filename)   Create a new image from XBM file or URL */PHP_FUNCTION(imagecreatefromxbm){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageCreateFromXbm, NULL);}/* }}} */#endif /* HAVE_GD_XBM */#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)/* {{{ proto resource imagecreatefromxpm(string filename)   Create a new image from XPM file or URL */PHP_FUNCTION(imagecreatefromxpm){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XPM, "XPM", gdImageCreateFromXpm, NULL);}/* }}} */#endif#ifdef HAVE_GD_WBMP/* {{{ proto resource imagecreatefromwbmp(string filename)   Create a new image from WBMP file or URL */PHP_FUNCTION(imagecreatefromwbmp){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageCreateFromWBMP, gdImageCreateFromWBMPCtx);}/* }}} */#endif /* HAVE_GD_WBMP *//* {{{ proto resource imagecreatefromgd(string filename)   Create a new image from GD file or URL */PHP_FUNCTION(imagecreatefromgd){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageCreateFromGd, gdImageCreateFromGdCtx);}/* }}} */#ifdef HAVE_GD_GD2/* {{{ proto resource imagecreatefromgd2(string filename)   Create a new image from GD2 file or URL */PHP_FUNCTION(imagecreatefromgd2){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageCreateFromGd2, gdImageCreateFromGd2Ctx);}/* }}} *//* {{{ proto resource imagecreatefromgd2part(string filename, int srcX, int srcY, int width, int height)   Create a new image from a given part of GD2 file or URL */PHP_FUNCTION(imagecreatefromgd2part){	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2PART, "GD2", gdImageCreateFromGd2Part, gdImageCreateFromGd2PartCtx);}/* }}} */#endif /* HAVE_GD_GD2 *//* {{{ _php_image_output */static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()){	zval **imgind, **file, **quality, **type;	gdImagePtr im;	char *fn = NULL;	FILE *fp;	int argc = ZEND_NUM_ARGS();	int q = -1, i, t = 1;	/* The quality parameter for Wbmp stands for the threshold when called from image2wbmp() */	/* When called from imagewbmp() the quality parameter stands for the foreground color. Default: black. */	/* The quality parameter for gd2 stands for chunk size */	if (argc < 1 || argc > 4 || zend_get_parameters_ex(argc, &imgind, &file, &quality, &type) == FAILURE) {		ZEND_WRONG_PARAM_COUNT();	}	ZEND_FETCH_RESOURCE(im, gdImagePtr, imgind, -1, "Image", 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 == 4) {			convert_to_long_ex(type);			t = Z_LVAL_PP(type);		}	}	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;		}		switch (image_type) {#ifdef HAVE_GD_WBMP			case PHP_GDIMG_CONVERT_WBM:				if (q == -1) {					q = 0;				} else 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);					q = 0;				}				gdImageWBMP(im, q, fp);				break;#endif			case PHP_GDIMG_TYPE_JPG:				(*func_p)(im, fp, q);				break;			case PHP_GDIMG_TYPE_WBM:				for (i = 0; i < gdImageColorsTotal(im); i++) {					if (gdImageRed(im, i) == 0) break;				}				(*func_p)(im, i, fp);				break;#if HAVE_LIBGD20			case PHP_GDIMG_TYPE_GD:				if (im->trueColor){					gdImageTrueColorToPalette(im,1,256);				}				(*func_p)(im, fp);				break;#endif			default:				if (q == -1) {					q = 128;				}				(*func_p)(im, fp, q, t);				break;		}		fflush(fp);		fclose(fp);	} else {		int   b;		FILE *tmp;		char  buf[4096];		char *path;		tmp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC);		if (tmp == NULL) {			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open temporary file");			RETURN_FALSE;		}		switch (image_type) {#ifdef HAVE_GD_WBMP			case PHP_GDIMG_CONVERT_WBM: 				if (q == -1) {  					q = 0;  				} else 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); 					q = 0;  				}				gdImageWBMP(im, q, tmp);				break;#endif			case PHP_GDIMG_TYPE_JPG:				(*func_p)(im, tmp, q);				break;			case PHP_GDIMG_TYPE_WBM:				for (i = 0; i < gdImageColorsTotal(im); i++) {					if (gdImageRed(im, i) == 0) {						break;					}				}				(*func_p)(im, q, tmp);				break;#if HAVE_LIBGD20			case PHP_GDIMG_TYPE_GD:				if (im->trueColor) {					gdImageTrueColorToPalette(im,1,256);				}				(*func_p)(im, tmp);				break;#endif			default:				(*func_p)(im, tmp);				break;		}		fseek(tmp, 0, SEEK_SET);#if APACHE && defined(CHARSET_EBCDIC)		/* XXX this is unlikely to work any more thies@thieso.net */		/* This is a binary file already: avoid EBCDIC->ASCII conversion */		ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);#endif		while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {			php_write(buf, b TSRMLS_CC);		}		fclose(tmp);		VCWD_UNLINK((const char *)path); /* make sure that the temporary file is removed */		efree(path);	}	RETURN_TRUE;}/* }}} */#ifdef HAVE_GD_GIF_CREATE/* {{{ proto bool imagegif(resource im [, string filename])   Output GIF image to browser or file */PHP_FUNCTION(imagegif){#ifdef HAVE_GD_GIF_CTX	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageGifCtx);#else	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageGif);#endif}/* }}} */#endif /* HAVE_GD_GIF_CREATE */#ifdef HAVE_GD_PNG/* {{{ proto bool imagepng(resource im [, string filename])   Output PNG image to browser or file */PHP_FUNCTION(imagepng){#ifdef USE_GD_IOCTX	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePngCtx);#else	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePng);#endif}/* }}} */#endif /* HAVE_GD_PNG */#ifdef HAVE_GD_JPG/* {{{ proto bool imagejpeg(resource im [, string filename [, int quality]])   Output JPEG image to browser or file */PHP_FUNCTION(imagejpeg){#ifdef USE_GD_IOCTX	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageJpegCtx);#else	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageJpeg);#endif}/* }}} */#endif /* HAVE_GD_JPG */#ifdef HAVE_GD_WBMP/* {{{ proto bool imagewbmp(resource im [, string filename, [, int foreground]])   Output WBMP image to browser or file */PHP_FUNCTION(imagewbmp){#ifdef USE_GD_IOCTX	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageWBMPCtx);#else	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageWBMP);#endif}/* }}} */#endif /* HAVE_GD_WBMP *//* {{{ proto bool imagegd(resource im [, string filename])   Output GD image to browser or file */PHP_FUNCTION(imagegd){	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageGd);}/* }}} */#ifdef HAVE_GD_GD2/* {{{ proto bool imagegd2(resource im [, string filename, [, int chunk_size, [, int type]]])   Output GD2 image to browser or file */PHP_FUNCTION(imagegd2){	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageGd2);}/* }}} */#endif /* HAVE_GD_GD2 *//* {{{ proto bool imagedestroy(resource im)   Destroy an image */PHP_FUNCTION(imagedestroy){	zval **IM;	gdImagePtr im;	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &IM) == FAILURE) {		ZEND_WRONG_PARAM_COUNT();	}	ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);	zend_list_delete(Z_LVAL_PP(IM));	RETURN_TRUE;}/* }}} *//* {{{ proto int imagecolorallocate(resource im, int red, int green, int blue)   Allocate a color for an image */PHP_FUNCTION(imagecolorallocate){	zval **IM, **red, **green, **blue;	gdImagePtr im;	if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &red, &green, &blue) == FAILURE) {		ZEND_WRONG_PARAM_COUNT();	}	ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);	convert_to_long_ex(red);	convert_to_long_ex(green);	convert_to_long_ex(blue);	RETURN_LONG(gdImageColorAllocate(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue)));}/* }}} */#if HAVE_LIBGD15/* {{{ proto void imagepalettecopy(resource dst, resource src)   Copy the palette from the src image onto the dst image */PHP_FUNCTION(imagepalettecopy){	zval **dstim, **srcim;	gdImagePtr dst, src;	if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &dstim, &srcim) == FAILURE) {		ZEND_WRONG_PARAM_COUNT();	}	ZEND_FETCH_RESOURCE(dst, gdImagePtr, dstim, -1, "Image", le_gd);	ZEND_FETCH_RESOURCE(src, gdImagePtr, srcim, -1, "Image", le_gd);	gdImagePaletteCopy(dst, src);}/* }}} */#endif/* {{{ proto int imagecolorat(resource im, int x, int y)   Get the index of the color of a pixel */PHP_FUNCTION(imagecolorat){	zval **IM, **x, **y;	gdImagePtr im;	if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &IM, &x, &y) == FAILURE) {		ZEND_WRONG_PARAM_COUNT();	}	ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);	convert_to_long_ex(x);	convert_to_long_ex(y);#if HAVE_LIBGD20	if (gdImageTrueColor(im)) {		if (im->tpixels && gdImageBoundsSafe(im, Z_LVAL_PP(x), Z_LVAL_PP(y))) {			RETURN_LONG(gdImageTrueColorPixel(im, Z_LVAL_PP(x), Z_LVAL_PP(y)));		} else {			php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%ld,%ld is out of bounds", Z_LVAL_PP(x), Z_LVAL_PP(y));			RETURN_FALSE;		}	} else {#endif		if (im->pixels && gdImageBoundsSafe(im, Z_LVAL_PP(x), Z_LVAL_PP(y))) {#if HAVE_LIBGD13			RETURN_LONG(im->pixels[Z_LVAL_PP(y)][Z_LVAL_PP(x)]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -