📄 gd.c
字号:
RETURN_FALSE; } array_init(return_value); add_assoc_string(return_value, "GD Version", PHP_GD_VERSION_STRING, 1);#ifdef ENABLE_GD_TTF add_assoc_bool(return_value, "FreeType Support", 1);#if HAVE_LIBFREETYPE add_assoc_string(return_value, "FreeType Linkage", "with freetype", 1);#elif HAVE_LIBTTF add_assoc_string(return_value, "FreeType Linkage", "with TTF library", 1);#else add_assoc_string(return_value, "FreeType Linkage", "with unknown library", 1);#endif#else add_assoc_bool(return_value, "FreeType Support", 0);#endif#ifdef HAVE_LIBT1 add_assoc_bool(return_value, "T1Lib Support", 1);#else add_assoc_bool(return_value, "T1Lib Support", 0);#endif#ifdef HAVE_GD_GIF_READ add_assoc_bool(return_value, "GIF Read Support", 1);#else add_assoc_bool(return_value, "GIF Read Support", 0);#endif#ifdef HAVE_GD_GIF_CREATE add_assoc_bool(return_value, "GIF Create Support", 1);#else add_assoc_bool(return_value, "GIF Create Support", 0);#endif#ifdef HAVE_GD_JPG add_assoc_bool(return_value, "JPG Support", 1);#else add_assoc_bool(return_value, "JPG Support", 0);#endif#ifdef HAVE_GD_PNG add_assoc_bool(return_value, "PNG Support", 1);#else add_assoc_bool(return_value, "PNG Support", 0);#endif#ifdef HAVE_GD_WBMP add_assoc_bool(return_value, "WBMP Support", 1);#else add_assoc_bool(return_value, "WBMP Support", 0);#endif#ifdef HAVE_GD_XBM add_assoc_bool(return_value, "XBM Support", 1);#else add_assoc_bool(return_value, "XBM Support", 0);#endif#if defined(USE_GD_JISX0208) && defined(HAVE_GD_BUNDLED) add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);#else add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);#endif}/* }}} *//* Need this for cpdf. See also comment in file.c php3i_get_le_fp() */PHP_GD_API int phpi_get_le_gd(void){ return le_gd;}#ifndef HAVE_GDIMAGECOLORRESOLVE/* {{{ gdImageColorResolve *//********************************************************************//* gdImageColorResolve is a replacement for the old fragment: *//* *//* if ((color=gdImageColorExact(im,R,G,B)) < 0) *//* if ((color=gdImageColorAllocate(im,R,G,B)) < 0) *//* color=gdImageColorClosest(im,R,G,B); *//* *//* in a single function */int gdImageColorResolve(gdImagePtr im, int r, int g, int b){ int c; int ct = -1; int op = -1; long rd, gd, bd, dist; long mindist = 3*255*255; /* init to max poss dist */ for (c = 0; c < im->colorsTotal; c++) { if (im->open[c]) { op = c; /* Save open slot */ continue; /* Color not in use */ } rd = (long) (im->red [c] - r); gd = (long) (im->green[c] - g); bd = (long) (im->blue [c] - b); dist = rd * rd + gd * gd + bd * bd; if (dist < mindist) { if (dist == 0) { return c; /* Return exact match color */ } mindist = dist; ct = c; } } /* no exact match. We now know closest, but first try to allocate exact */ if (op == -1) { op = im->colorsTotal; if (op == gdMaxColors) { /* No room for more colors */ return ct; /* Return closest available color */ } im->colorsTotal++; } im->red [op] = r; im->green[op] = g; im->blue [op] = b; im->open [op] = 0; return op; /* Return newly allocated color */}/* }}} */#endif#define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))/* {{{ proto int imageloadfont(string filename) Load a new font */PHP_FUNCTION(imageloadfont){ zval **file; int hdr_size = sizeof(gdFont) - sizeof(char *); int ind, body_size, n = 0, b, i, body_size_check; gdFontPtr font; php_stream *stream; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &file) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } convert_to_string_ex(file); stream = php_stream_open_wrapper(Z_STRVAL_PP(file), "rb", ENFORCE_SAFE_MODE | IGNORE_PATH | IGNORE_URL_WIN | REPORT_ERRORS, NULL); if (stream == NULL) { RETURN_FALSE; } /* Only supports a architecture-dependent binary dump format * at the moment. * The file format is like this on machines with 32-byte integers: * * byte 0-3: (int) number of characters in the font * byte 4-7: (int) value of first character in the font (often 32, space) * byte 8-11: (int) pixel width of each character * byte 12-15: (int) pixel height of each character * bytes 16-: (char) array with character data, one byte per pixel * in each character, for a total of * (nchars*width*height) bytes. */ font = (gdFontPtr) emalloc(sizeof(gdFont)); b = 0; while (b < hdr_size && (n = php_stream_read(stream, (char*)&font[b], hdr_size - b))) { b += n; } if (!n) { efree(font); if (php_stream_eof(stream)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "End of file while reading header"); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading header"); } php_stream_close(stream); RETURN_FALSE; } i = php_stream_tell(stream); php_stream_seek(stream, 0, SEEK_END); body_size_check = php_stream_tell(stream) - hdr_size; php_stream_seek(stream, i, SEEK_SET); body_size = font->w * font->h * font->nchars; if (body_size != body_size_check) { font->w = FLIPWORD(font->w); font->h = FLIPWORD(font->h); font->nchars = FLIPWORD(font->nchars); body_size = font->w * font->h * font->nchars; } if (body_size != body_size_check) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font"); efree(font); php_stream_close(stream); RETURN_FALSE; } font->data = emalloc(body_size); b = 0; while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b))) { b += n; } if (!n) { efree(font->data); efree(font); if (php_stream_eof(stream)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "End of file while reading body"); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading body"); } php_stream_close(stream); RETURN_FALSE; } php_stream_close(stream); /* Adding 5 to the font index so we will never have font indices * that overlap with the old fonts (with indices 1-5). The first * list index given out is always 1. */ ind = 5 + zend_list_insert(font, le_gd_font); RETURN_LONG(ind);}/* }}} *//* {{{ proto bool imagesetstyle(resource im, array styles) Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */PHP_FUNCTION(imagesetstyle){ zval **IM, **styles; gdImagePtr im; int * stylearr; int index; HashPosition pos; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &styles) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd); convert_to_array_ex(styles); /* copy the style values in the stylearr */ stylearr = safe_emalloc(sizeof(int), zend_hash_num_elements(HASH_OF(*styles)), 0); zend_hash_internal_pointer_reset_ex(HASH_OF(*styles), &pos); for (index = 0;; zend_hash_move_forward_ex(HASH_OF(*styles), &pos)) { zval ** item; if (zend_hash_get_current_data_ex(HASH_OF(*styles), (void **) &item, &pos) == FAILURE) { break; } convert_to_long_ex(item); stylearr[index++] = Z_LVAL_PP(item); } gdImageSetStyle(im, stylearr, index); efree(stylearr); RETURN_TRUE;}/* }}} */#if HAVE_LIBGD20/* {{{ proto resource imagecreatetruecolor(int x_size, int y_size) Create a new true color image */PHP_FUNCTION(imagecreatetruecolor){ zval **x_size, **y_size; gdImagePtr im; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &x_size, &y_size) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } convert_to_long_ex(x_size); convert_to_long_ex(y_size); if (Z_LVAL_PP(x_size) <= 0 || Z_LVAL_PP(y_size) <= 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions"); RETURN_FALSE; } im = gdImageCreateTrueColor(Z_LVAL_PP(x_size), Z_LVAL_PP(y_size)); ZEND_REGISTER_RESOURCE(return_value, im, le_gd);}/* }}} *//* {{{ proto bool imageistruecolor(resource im) return true if the image uses truecolor */PHP_FUNCTION(imageistruecolor){ 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); RETURN_BOOL(im->trueColor);}/* }}} *//* {{{ proto void imagetruecolortopalette(resource im, bool ditherFlag, int colorsWanted) Convert a true colour image to a palette based image with a number of colours, optionally using dithering. */PHP_FUNCTION(imagetruecolortopalette){ zval **IM, **dither, **ncolors; gdImagePtr im; if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &IM, &dither, &ncolors) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd); convert_to_boolean_ex(dither); convert_to_long_ex(ncolors); if (Z_LVAL_PP(ncolors) <= 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of colors has to be greater than zero"); RETURN_FALSE; } gdImageTrueColorToPalette(im, Z_LVAL_PP(dither), Z_LVAL_PP(ncolors)); RETURN_TRUE;}/* }}} */#if HAVE_GD_BUNDLED/* {{{ proto bool imagecolormatch(resource im1, resource im2) Makes the colors of the palette version of an image more closely match the true color version */PHP_FUNCTION(imagecolormatch){ zval **IM1, **IM2; gdImagePtr im1, im2; int result; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM1, &IM2 ) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } ZEND_FETCH_RESOURCE(im1, gdImagePtr, IM1, -1, "Image", le_gd); ZEND_FETCH_RESOURCE(im2, gdImagePtr, IM2, -1, "Image", le_gd); result = gdImageColorMatch(im1, im2); switch (result) { case -1: php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image1 must be TrueColor" ); RETURN_FALSE; break; case -2: php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image2 must be Palette" ); RETURN_FALSE; break; case -3: php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image1 and Image2 must be the same size" ); RETURN_FALSE; break; } RETURN_TRUE;}/* }}} */#endif/* {{{ proto bool imagesetthickness(resource im, int thickness) Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */PHP_FUNCTION(imagesetthickness){ zval **IM, **thick; gdImagePtr im; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &thick) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd); convert_to_long_ex(thick); gdImageSetThickness(im, Z_LVAL_PP(thick)); RETURN_TRUE;}/* }}} *//* {{{ proto bool imagefilledellipse(resource im, int cx, int cy, int w, int h, int color) Draw an ellipse */PHP_FUNCTION(imagefilledellipse){ zval **IM, **cx, **cy, **w, **h, **color; gdImagePtr im; if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &cx, &cy, &w, &h, &color) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd); convert_to_long_ex(cx); convert_to_long_ex(cy); convert_to_long_ex(w); convert_to_long_ex(h); convert_to_long_ex(color); gdImageFilledEllipse(im, Z_LVAL_PP(cx), Z_LVAL_PP(cy), Z_LVAL_PP(w), Z_LVAL_PP(h), Z_LVAL_PP(color)); RETURN_TRUE;}/* }}} *//* {{{ proto bool imagefilledarc(resource im, int cx, int cy, int w, int h, int s, int e, int col, int style) Draw a filled partial ellipse */PHP_FUNCTION(imagefilledarc){ zval **IM, **cx, **cy, **w, **h, **ST, **E, **col, **style; gdImagePtr im; int e, st; if (ZEND_NUM_ARGS() != 9 || zend_get_parameters_ex(9, &IM, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd); convert_to_long_ex(cx); convert_to_long_ex(cy); convert_to_long_ex(w); convert_to_long_ex(h); convert_to_long_ex(ST); convert_to_long_ex(E); convert_to_long_ex(col); convert_to_long_ex(style); e = Z_LVAL_PP(E); if (e < 0) { e %= 360; } st = Z_LVAL_PP(ST); if (st < 0) { st %= 360; } gdImageFilledArc(im, Z_LVAL_PP(cx), Z_LVAL_PP(cy), Z_LVAL_PP(w), Z_LVAL_PP(h), st, e, Z_LVAL_PP(col), Z_LVAL_PP(style)); RETURN_TRUE;}/* }}} *//* {{{ proto bool imagealphablending(resource im, bool on) Turn alpha blending mode on or off for the given image */PHP_FUNCTION(imagealphablending){ zval **IM, **blend; gdImagePtr im; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &blend) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd); convert_to_boolean_ex(blend); gdImageAlphaBlending(im, Z_LVAL_PP(blend)); RETURN_TRUE;}/* }}} */#if HAVE_LIBGD20/* {{{ proto bool imagesavealpha(resource im, bool on) Include alpha channel to a saved image */PHP_FUNCTION(imagesavealpha)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -