LibGD fonts and text functions





gdFontPtr gdFontGetSmall(void) (FUNCTION)

Returns a font pointer for the "small" gd font. Your code must include the header file gdfonts.h before calling this function. Under Windows, due to the nature of DLLs, the use of this function is strongly recommended rather than attempting to use the gdFontSmall pointer directly. (You may safely assign the result to a local gdFontPtr variable in your own code.)

See gdImageString for more information and examples, or gdImageStringFT for a freetype-based alternative that supports truetype fonts.

gdFontPtr gdFontGetLarge(void) (FUNCTION)

Returns a font pointer for the "large" gd font. Your code must include the header file gdfontl.h before calling this function. Under Windows, due to the nature of DLLs, the use of this function is strongly recommended rather than attempting to use the gdFontLarge pointer directly. (You may safely assign the result to a local gdFontPtr variable in your own code.)

See gdImageString for more information and examples, or gdImageStringFT for a freetype-based alternative that supports truetype fonts.

gdFontPtr gdFontGetMediumBold(void) (FUNCTION)

Returns a font pointer for the "medium bold" gd font. Your code must include the header file gdfontmb.h before calling this function. Under Windows, due to the nature of DLLs, the use of this function is strongly recommended rather than attempting to use the gdFontMediumBold pointer directly. (You may safely assign the result to a local gdFontPtr variable in your own code.)

See gdImageString for more information and examples, or gdImageStringFT for a freetype-based alternative that supports truetype fonts.

gdFontPtr gdFontGetGiant(void) (FUNCTION)

Returns a font pointer for the "giant" gd font. Your code must include the header file gdfontg.h before calling this function. Under Windows, due to the nature of DLLs, the use of this function is strongly recommended rather than attempting to use the gdFontGiant pointer directly. (You may safely assign the result to a local gdFontPtr variable in your own code.)

See gdImageString for more information and examples, or gdImageStringFT for a freetype-based alternative that supports truetype fonts.

gdFontPtr gdFontGetTiny(void) (FUNCTION)

Returns a font pointer for the "tiny" gd font. Your code must include the header file gdfontt.h before calling this function. Under Windows, due to the nature of DLLs, the use of this function is strongly recommended rather than attempting to use the gdFontTiny pointer directly. (You may safely assign the result to a local gdFontPtr variable in your own code.)

See gdImageString for more information and examples, or gdImageStringFT for a freetype-based alternative that supports truetype fonts.

void gdImageChar(gdImagePtr im, gdFontPtr font, int x, int y, int c, int color) (FUNCTION)

gdImageChar is used to draw single characters on the image. (To draw multiple characters, use gdImageString or gdImageString16. See also gdImageStringFT for a high quality solution.) The second argument is a pointer to a font definition structure; five fonts are provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, gdFontLarge, and gdFontGiant.

You must include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", "gdfontl.h" and "gdfontg.h" respectively and (if you are not using a library-based approach) link with the corresponding .c files to use the provided fonts.

Windows DLL users: although you can use these DLL-exported pointers directly, you cannot easily assign them to other pointers. This will cause hard-to-debug problems. To avoid such troubles, you should call the functions gdFontGetTiny(), gdFontGetSmall(), gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to obtain pointers to the fonts under Windows.

The character specified by the fifth argument is drawn from left to right in the specified color. (See gdImageCharUp for a way of drawing vertical text.) Pixels not set by a particular character retain their previous color.

  1. #include "gd.h"
  2. #include "gdfontl.h"
  3. /*... inside a function ...*/
  4. gdImagePtr im;
  5. int black;
  6. int white;
  7. im = gdImageCreate(100, 100);
  8. /* Background color (first allocated) */
  9. black = gdImageColorAllocate(im, 0, 0, 0);
  10. /* Allocate the color white (red, green and blue all maximum). */
  11. white = gdImageColorAllocate(im, 255, 255, 255);
  12. /* Draw a character. */
  13. gdImageChar(im, gdFontGetLarge(), 0, 0, 'Q', white);
  14. /* ... Do something with the image, such as
  15. saving it to a file... */
  16. /* Destroy it */
  17. gdImageDestroy(im);

void gdImageCharUp(gdImagePtr im, gdFontPtr font, int x, int y, int c, int color) (FUNCTION)

gdImageCharUp is used to draw single characters on the image, rotated 90 degrees. (To draw multiple characters, use gdImageStringUp or gdImageStringUp16.) The second argument is a pointer to a font definition structure; five fonts are provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, gdFontLarge, and gdFontGiant. You must include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", "gdfontl.h" and "gdfontg.h" respectively and (if you are not using a library-based approach) link with the corresponding .c files to use the provided fonts.

Windows DLL users: although you can use these DLL-exported pointers directly, you cannot easily assign them to other pointers. This will cause hard-to-debug problems. To avoid such troubles, you should call the functions gdFontGetTiny(), gdFontGetSmall(), gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to obtain pointers to the fonts under Windows.

The character specified by the fifth argument is drawn from bottom to top, rotated at a 90-degree angle, in the specified color. (See gdImageChar for a way of drawing horizontal text.) Pixels not set by a particular character retain their previous color.

  1. #include "gd.h"
  2. #include "gdfontl.h"
  3. /*... inside a function ...*/
  4. gdImagePtr im;
  5. int black;
  6. int white;
  7. im = gdImageCreate(100, 100);
  8. /* Background color (first allocated) */
  9. black = gdImageColorAllocate(im, 0, 0, 0);
  10. /* Allocate the color white (red, green and blue all maximum). */
  11. white = gdImageColorAllocate(im, 255, 255, 255);
  12. /* Draw a character upwards so it rests against the top of the image. */
  13. gdImageCharUp(im, gdFontGetLarge(),
  14. 0, gdFontGetLarge()->h, 'Q', white);
  15. /* ... Do something with the image, such as
  16. saving it to a file... */
  17. /* Destroy it */
  18. gdImageDestroy(im);

void gdImageString(gdImagePtr im, gdFontPtr font, int x, int y, unsigned char *s, int color) (FUNCTION)

gdImageString is used to draw multiple characters on the image. (To draw single characters, use gdImageChar.) The second argument is a pointer to a font definition structure; five fonts are provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, gdFontLarge, and gdFontGiant. You must include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", "gdfontl.h" and "gdfontg.h" respectively and (if you are not using a library-based approach) link with the corresponding .c files to use the provided fonts.

Windows DLL users: although you can use these DLL-exported pointers directly, you cannot easily assign them to other pointers. This will cause hard-to-debug problems. To avoid such troubles, you should call the functions gdFontGetTiny(), gdFontGetSmall(), gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to obtain pointers to the fonts under Windows.

The null-terminated C string specified by the fifth argument is drawn from left to right in the specified color. (See gdImageStringUp for a way of drawing vertical text. See also gdImageStringFT for a high quality solution.) Pixels not set by a particular character retain their previous color.

  1. #include "gd.h"
  2. #include "gdfontl.h"
  3. #include <string.h>
  4. /*... inside a function ...*/
  5. gdImagePtr im;
  6. int black;
  7. int white;
  8. /* String to draw. */
  9. char *s = "Hello.";
  10. im = gdImageCreate(100, 100);
  11. /* Background color (first allocated) */
  12. black = gdImageColorAllocate(im, 0, 0, 0);
  13. /* Allocate the color white (red, green and blue all maximum). */
  14. white = gdImageColorAllocate(im, 255, 255, 255);
  15. /* Draw a centered string. */
  16. gdImageString(im, gdFontGetLarge(),
  17. im->sx / 2 - (strlen(s) * gdFontGetLarge()->w / 2),
  18. im->sy / 2 - gdFontGetLarge()->h / 2,
  19. s, white);
  20. /* ... Do something with the image, such as
  21. saving it to a file... */
  22. /* Destroy it */
  23. gdImageDestroy(im);

void gdImageString16(gdImagePtr im, gdFontPtr font, int x, int y, unsigned short *s, int color) (FUNCTION)

gdImageString16 is used to draw multiple 16-bit characters on the image. (To draw single characters, use gdImageChar16.) The second argument is a pointer to a font definition structure; no 16-bit fonts are provided with gd as standard equipment and there does not seem to be much momentum to create them although the bdftogd script can do so. The preferred solution is gdImageStringFT, which uses freetype to provide truetype font support.

Windows DLL users: although you can use these DLL-exported pointers directly, you cannot easily assign them to other pointers. This will cause hard-to-debug problems. To avoid such troubles, you should call the functions gdFontGetTiny(), gdFontGetSmall(), gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to obtain pointers to the fonts under Windows.

The null-terminated string of characters represented as 16-bit unsigned short integers specified by the fifth argument is drawn from left to right in the specified color. (See gdImageStringUp16 for a way of drawing vertical text.) Pixels not set by a particular character retain their previous color.

This function was added in gd1.3 to provide a means of rendering fonts with more than 256 characters for those who have them. A more frequently used routine is gdImageString.

void gdImageStringUp(gdImagePtr im, gdFontPtr font, int x, int y, unsigned char *s, int color) (FUNCTION)

gdImageStringUp is used to draw multiple characters on the image, rotated 90 degrees. (To draw single characters, use gdImageCharUp.) The second argument is a pointer to a font definition structure; five fonts are provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, gdFontLarge, and gdFontGiant. You must include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", "gdfontl.h" and "gdfontg.h" respectively and (if you are not using a library-based approach) link with the corresponding .c files to use the provided fonts.

Windows DLL users: although you can use these DLL-exported pointers directly, you cannot easily assign them to other pointers. This will cause hard-to-debug problems. To avoid such troubles, you should call the functions gdFontGetTiny(), gdFontGetSmall(), gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to obtain pointers to the fonts under Windows.

The null-terminated C string specified by the fifth argument is drawn from bottom to top (rotated 90 degrees) in the specified color. (See gdImageString for a way of drawing horizontal text.) Pixels not set by a particular character retain their previous color.

  1. #include "gd.h"
  2. #include "gdfontl.h"
  3. #include <string.h>
  4. /*... inside a function ...*/
  5. gdImagePtr im;
  6. int black;
  7. int white;
  8. /* String to draw. */
  9. char *s = "Hello.";
  10. im = gdImageCreate(100, 100);
  11. /* Background color (first allocated) */
  12. black = gdImageColorAllocate(im, 0, 0, 0);
  13. /* Allocate the color white (red, green and blue all maximum). */
  14. white = gdImageColorAllocate(im, 255, 255, 255);
  15. /* Draw a centered string going upwards. Axes are reversed,
  16. and Y axis is decreasing as the string is drawn. */
  17. gdImageStringUp(im, gdFontGetLarge(),
  18. im->w / 2 - gdFontGetLarge()->h / 2,
  19. im->h / 2 + (strlen(s) * gdFontGetLarge()->w / 2),
  20. s, white);
  21. /* ... Do something with the image, such as
  22. saving it to a file... */
  23. /* Destroy it */
  24. gdImageDestroy(im);

void gdImageStringUp16(gdImagePtr im, gdFontPtr font, int x, int y, unsigned short *s, int color) (FUNCTION)

gdImageString is used to draw multiple 16-bit characters vertically on the image. (To draw single characters, use gdImageChar.) The second argument is a pointer to a font definition structure; five fonts are provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, gdFontLarge, and gdFontGiant. You must include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", "gdfontl.h" and "gdfontg.h" respectively and (if you are not using a library-based approach) link with the corresponding .c files to use the provided fonts.

Windows DLL users: although you can use these DLL-exported pointers directly, you cannot easily assign them to other pointers. This will cause hard-to-debug problems. To avoid such troubles, you should call the functions gdFontGetTiny(), gdFontGetSmall(), gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to obtain pointers to the fonts under Windows.

The null-terminated string of characters represented as 16-bit unsigned short integers specified by the fifth argument is drawn from bottom to top in the specified color. (See gdImageStringUp16 for a way of drawing horizontal text.) Pixels not set by a particular character retain their previous color.

This function was added in gd1.3 to provide a means of rendering fonts with more than 256 characters for those who have them. A more frequently used routine is gdImageStringUp.

int gdFTUseFontConfig(int flag) (FUNCTION)

GD 2.0.29 introduced the ability to use fontconfig patterns rather than font file names as parameters to gdImageStringFT, gdImageStringFTEx and gdImageStringFTCircle. For backwards compatibility reasons, the fontlist parameter to those functions is still expected to be a full or partial font file path name or list thereof by default. However, as a convenience, a single call to gdFTUseFontConfig with a nonzero parameter configures gd to expect the fontlist parameter to be a fontconfig pattern. Regardless of whether the flag argument is nonzero, this function returns true when the fontconfig library is available and false when it is not. When fontconfig is not available, the fontlist parameter always behaves as in previous versions of GD.

  1.  
  2. #include "gd.h"
  3. #include <string.h>
  4. /*... inside a function ...*/
  5. gdImagePtr im;
  6. int black;
  7. int white;
  8. int brect[8];
  9. int x, y;
  10. char *err;
  11.  
  12. char *s = "Hello."; /* String to draw. */
  13. double sz = 40.;
  14. char *fc = "times:bold:italic"; /* fontconfig pattern */
  15.  
  16. /* Signal that all freetype font calls in this program will receive
  17. fontconfig patterns rather than filenames of font files */
  18. gdFTUseFontConfig(1);
  19.  
  20. /* obtain brect so that we can size the image */
  21. err = gdImageStringFT(NULL,&brect[0],0,fc,sz,0.,0,0,s);
  22. if (err) {fprintf(stderr,err); return 1;}
  23.  
  24. /* create an image big enough for the string plus a little whitespace */
  25. x = brect[2]-brect[6] + 6;
  26. y = brect[3]-brect[7] + 6;
  27. im = gdImageCreate(x,y);
  28.  
  29. /* Background color (first allocated) */
  30. white = gdImageColorResolve(im, 255, 255, 255);
  31. black = gdImageColorResolve(im, 0, 0, 0);
  32.  
  33. /* render the string, offset origin to center string*/
  34. /* note that we use top-left coordinate for adjustment
  35. * since gd origin is in top-left with y increasing downwards. */
  36. x = 3 - brect[6];
  37. y = 3 - brect[7];
  38. err = gdImageStringFT(im,&brect[0],black,fc,sz,0.0,x,y,s);
  39. if (err) {fprintf(stderr,err); return 1;}
  40.  

char *gdImageStringFT(gdImagePtr im, int *brect, int fg, char *fontname, double ptsize, double angle, int x, int y, char *string) (FUNCTION)

gdImageStringFT draws a string of anti-aliased characters on the image using the FreeType library to render user-supplied TrueType fonts. We do not provide TrueType fonts (.ttf and .ttc files). Obtaining them is entirely up to you. The string is anti-aliased, meaning that there should be fewer "jaggies" visible. The fontname is the full pathname to a TrueType font file, or a font face name if the GDFONTPATH environment variable or the compiled-in DEFAULT_FONTPATH macro of gdft.c have been set intelligently. In the absence of a full path, the font face name may be presented with or without extension (2.0.26).

The null-terminated string argument is considered to be encoded via the UTF_8 standard; also, HTML entities are supported, including decimal, hexadecimal, and named entities (2.0.26). Those who are passing ordinary ASCII strings may have difficulty with the & character unless encoded correctly as & but should have no other difficulties.

The string may be arbitrarily scaled (ptsize) and rotated (angle in radians). The direction of rotation is counter-clockwise, with 0 radians (0 degrees) at 3 o'clock and PI/2 radians (90 degrees) at 12 o'clock.

The user-supplied int brect[8] array is filled on return from gdImageStringFT with the 8 elements representing the 4 corner coordinates of the bounding rectangle (the smallest rectangle that completely surrounds the rendered string and does not intersect any pixel of the rendered string).

   0	lower left corner, X position
   1	lower left corner, Y position
   2	lower right corner, X position
   3	lower right corner, Y position
   4	upper right corner, X position
   5	upper right corner, Y position
   6	upper left corner, X position
   7	upper left corner, Y position

The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner seeing the text horizontally.

Use a NULL gdImagePtr to get the bounding rectangle without rendering. This is a relatively cheap operation if followed by a rendering of the same string, because of the caching of the partial rendering during bounding rectangle calculation.

The string is rendered in the color indicated by the gf color index. Use the negative of the desired color index to disable anti-aliasing.

The string may contain UTF-8 sequences like: "À"

gdImageStringFT will return a null char* on success, or an error string on failure.

  1.  
  2. #include "gd.h"
  3. #include <string.h>
  4. /*... inside a function ...*/
  5. gdImagePtr im;
  6. int black;
  7. int white;
  8. int brect[8];
  9. int x, y;
  10. char *err;
  11.  
  12. char *s = "Hello."; /* String to draw. */
  13. double sz = 40.;
  14. char *f = "Vera.ttf"; /* User supplied font */
  15.  
  16. /* obtain brect so that we can size the image */
  17. err = gdImageStringFT(NULL,&brect[0],0,f,sz,0.,0,0,s);
  18. if (err) {fprintf(stderr,err); return 1;}
  19.  
  20. /* create an image big enough for the string plus a little whitespace */
  21. x = brect[2]-brect[6] + 6;
  22. y = brect[3]-brect[7] + 6;
  23. im = gdImageCreate(x,y);
  24.  
  25. /* Background color (first allocated) */
  26. white = gdImageColorResolve(im, 255, 255, 255);
  27. black = gdImageColorResolve(im, 0, 0, 0);
  28.  
  29. /* render the string, offset origin to center string*/
  30. /* note that we use top-left coordinate for adjustment
  31. * since gd origin is in top-left with y increasing downwards. */
  32. x = 3 - brect[6];
  33. y = 3 - brect[7];
  34. err = gdImageStringFT(im,&brect[0],black,f,sz,0.0,x,y,s);
  35. if (err) {fprintf(stderr,err); return 1;}
  36.  
  37. /* Write img to stdout */
  38. gdImagePng(im, stdout);
  39.  
  40. /* Destroy it */
  41. gdImageDestroy(im);
   See also gdImageStringFTEx.

char *gdImageStringFTEx(gdImagePtr im, int *brect, int fg, char *fontname, double ptsize, double angle, int x, int y, gdFTStringExtraPtr strex) (FUNCTION)

New in 2.0.5, also found in common third-party versions of gd. gdImageStringFTEx extends the capabilities of gdImageStringFT by providing a way to pass additional parameters.

If the strex parameter is not null, it must point to a gdFTStringExtra structure. As of gd 2.0.5, this structure is defined as follows:

   typedef struct {
          /* logical OR of gdFTEX_ values */
          int flags; 
          /* fine tune line spacing for '\n' */
          double linespacing; 
          /* Preferred character mapping */
          int charmap;
          /* Rendering resolution */
          int hdpi;
          int vdpi;
          char *xshow;
          char *fontpath;
   } gdFTStringExtra, *gdFTStringExtraPtr;

To output multiline text with a specific line spacing, include gdFTEX_LINESPACE in the setting of flags:

   flags |= gdFTEX_LINESPACE;

And also set linespacing to the desired spacing, expressed as a multiple of the font height. Thus a line spacing of 1.0 is the minimum to guarantee that lines of text do not collide.

If gdFTEX_LINESPACE is not present, or strex is null, or gdImageStringFT is called, linespacing defaults to 1.05.

To specify a preference for Unicode, Shift_JIS Big5 character encoding, set or To output multiline text with a specific line spacing, include gdFTEX_CHARMAP in the setting of flags:

   flags |= gdFTEX_CHARMAP;

And set charmap to the desired value, which can be any of gdFTEX_Unicode, gdFTEX_Shift_JIS, or gdFTEX_Big5. If you do not specify a preference, Unicode will be tried first. If the preferred character mapping is not found in the font, other character mappings are attempted.

GD operates on the assumption that the output image will be rendered to a computer screen. By default, gd passes a resolution of 96 dpi to the freetype text rendering engine. This influences the "hinting" decisions made by the renderer. To specify a different resolution, set hdpi and vdpi accordingly (in dots per inch) and add gdFTEX_RESOLUTION to flags:

   flags |= gdFTEX_RESOLUTION;

GD 2.0.29 and later will normally attempt to apply kerning tables, if fontconfig is available, to adjust the relative positions of consecutive characters more ideally for that pair of characters. This can be turn off by specifying the gdFTEX_DISABLE_KERNING flag:

   flags |= gdFTEX_DISABLE_KERNING;

GD 2.0.29 and later can return a vector of individual character position advances, occasionally useful in applications that must know exactly where each character begins. This is returned in the xshow element of the gdFTStringExtra structure if the gdFTEX_XSHOW flag is set:

   flags |= gdFTEX_XSHOW;

The caller is responsible for calling gdFree() on the xshow element after the call if gdFTEX_XSHOW is set.

GD 2.0.29 and later can also return the path to the actual font file used if the gdFTEX_RETURNFONTPATHNAME flag is set. This is useful because GD 2.0.29 and above are capable of selecting a font automatically based on a fontconfig font pattern when fontconfig is available. This information is returned in the fontpath element of the gdFTStringExtra structure.

   flags |= gdFTEX_RETURNFONTPATHNAME;

The caller is responsible for calling gdFree() on the fontpath element after the call if gdFTEX_RETURNFONTPATHNAME is set.

GD 2.0.29 and later can use fontconfig to resolve font names, including fontconfig patterns, if the gdFTEX_FONTCONFIG flag is set. As a convenience, this behavior can be made the default by calling gdFTUseFontConfig with a nonzero value. In that situation it is not necessary to set the gdFTEX_FONTCONFIG flag on every call; however explicit font path names can still be used if the gdFTEX_FONTPATHNAME flag is set:

   flags |= gdFTEX_FONTPATHNAME;

Unless gdFTUseFontConfig has been called with a nonzero value, GD 2.0.29 and later will still expect the fontlist argument to the freetype text output functions to be a font file name or list thereof as in previous versions. If you do not wish to make fontconfig the default, it is still possible to force the use of fontconfig for a single call to the freetype text output functions by setting the gdFTEX_FONTCONFIG flag:

   flags |= gdFTEX_FONTCONFIG;

GD 2.0.29 and above can use fontconfig to resolve font names, including fontconfig patterns, if the gdFTEX_FONTCONFIG flag is set. As a convenience, this behavior can be made the default by calling gdFTUseFontConfig with a nonzero value. In that situation it is not necessary to set the gdFTEX_FONTCONFIG flag on every call; however explicit font path names can still be used if the gdFTEX_FONTPATHNAME flag is set:

   flags |= gdFTEX_FONTPATHNAME;

For more information, see gdImageStringFT.

char *gdImageStringFTCircle(gdImagePtr im, int cx, int cy, double radius, double textRadius, double fillPortion, char *font, double points, char *top, char *bottom, int fgcolor) (FUNCTION)

Draws the text strings specified by top and bottom on im, curved along the edge of a circle of radius radius, with its center at cx and cy. top is written clockwise along the top; bottom is written counterclockwise along the bottom. textRadius determines the "height" of each character; if textRadius is 1/2 of radius, characters extend halfway from the edge to the center. fillPortion varies from 0 to 1.0, with useful values from about 0.4 to 0.9, and determines how much of the 180 degrees of arc assigned to each section of text is actually occupied by text; 0.9 looks better than 1.0 which is rather crowded. font is a freetype font; see gdImageStringFT. points is passed to the freetype engine and has an effect on hinting; although the size of the text is determined by radius, textRadius, and fillPortion, you should pass a point size that "hints" appropriately -- if you know the text will be large, pass a large point size such as 24.0 to get the best results. fgcolor can be any color, and may have an alpha component, do blending, etc.

Returns 0 on success, or an error string otherwise.

  1.  
  2. #include <stdio.h>
  3. #include <gd.h>
  4.  
  5. int main (int argc, char *argv[])
  6. {
  7. FILE *in;
  8. FILE *out;
  9. gdImagePtr im;
  10. int radius;
  11. /* Create an image of text on a circle, with an
  12. alpha channel so that we can copy it onto a
  13. background */
  14. in = fopen("mypicture.jpg", "rb");
  15. if (!in) {
  16. im = gdImageCreateTrueColor(300, 300);
  17. } else {
  18. im = gdImageCreateFromJpeg(in);
  19. fclose(in);
  20. }
  21. if (gdImageSX(im) < gdImageSY(im)) {
  22. radius = gdImageSX(im) / 2;
  23. } else {
  24. radius = gdImageSY(im) / 2;
  25. }
  26. gdImageStringFTCircle(
  27. im,
  28. gdImageSX(im) / 2,
  29. gdImageSY(im) / 2,
  30. radius,
  31. radius / 2,
  32. 0.8,
  33. "arial",
  34. 24,
  35. "top text",
  36. "bottom text",
  37. gdTrueColorAlpha(240, 240, 255, 32));
  38. out = fopen("gdfx.png", "wb");
  39. if (!out) {
  40. fprintf(stderr, "Can't create gdfx.png\n");
  41. return 1;
  42. }
  43. gdImagePng(im, out);
  44. fclose(out);
  45. gdImageDestroy(im);
  46. return 0;
  47. }

For more information, see gdImageStringFTEx and gdImageSquareToCircle.

char *gdImageStringTTF(gdImagePtr im, int *brect, int fg, char *fontname, double ptsize, double angle, int x, int y, char *string) (FUNCTION)

DEPRECATED. This function simply invokes gdImageStringFT for backwards compatibility with old code that was written with FreeType 1.x.

int gdFontCacheSetup(void) (FUNCTION)

This function initializes the font cache for freetype text output functions such as gdImageStringFTEx. If this function is not called by the programmer, it is invoked automatically on the first truetype text output call, which is perfectly safe unless the application is multithreaded. Multithreaded applications should directly invoke this function before allowing any thread to use freetype text output. Returns 0 on success, nonzero if the freetype library fails to initialize.

void gdFontCacheShutdown(void) (FUNCTION)

This function releases the memory used by the freetype font cache and the text output mutex. It is always recommended to use this if you call gdFontCacheSetup(), but applications that use gd for their entire lifetime, then exit, need not call this function because your OS may take care of the memory resource.





Retrieved from http://www.libgd.org/Font

This library was originally developed by Thomas Boutell