LibGD drawing functions





void gdImageSetPixel(gdImagePtr im, int x, int y, int color)

gdImageSetPixel sets a pixel to a particular color index. Always use this function or one of the other drawing functions to access pixels; do not access the pixels of the gdImage structure directly.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = gdImageColorAllocate(im, 255, 255, 255);
  10. /* Set a pixel near the center. */
  11. gdImageSetPixel(im, 50, 50, white);
  12. /* ... Do something with the image, such as
  13. saving it to a file... */
  14. /* Destroy it */
  15. gdImageDestroy(im);

void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color)

gdImageLine is used to draw a line between two endpoints (x1,y1 and x2, y2). The line is drawn using the color index specified. Note that the color index can be an actual color returned by gdImageColorAllocate or one of gdStyled, gdBrushed or gdStyledBrushed.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green
  9. and blue all maximum). */
  10. white = gdImageColorAllocate(im, 255, 255, 255);
  11. /* Draw a line from the upper left corner to the
  12. lower right corner. */
  13. gdImageLine(im, 0, 0, 99, 99, white);
  14. /* ... Do something with the image, such as
  15. saving it to a file... */
  16. /* Destroy it */
  17. gdImageDestroy(im);
[edit]

void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color) (FUNCTION)

gdImageDashedLine is provided solely for backwards compatibility with gd 1.0. New programs should draw dashed lines using the normal gdImageLine function and the new gdImageSetStyle function.

gdImageDashedLine is used to draw a dashed line between two endpoints (x1,y1 and x2, y2). The line is drawn using the color index specified. The portions of the line that are not drawn are left transparent so the background is visible.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue
  9. all maximum). */
  10. white = gdImageColorAllocate(im, 255, 255, 255);
  11. /* Draw a dashed line from the upper left corner
  12. to the lower right corner. */
  13. gdImageDashedLine(im, 0, 0, 99, 99);
  14. /* ... Do something with the image, such as
  15. saving it to a file... */
  16. /* Destroy it */
  17. gdImageDestroy(im);

void gdImagePolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color)

gdImagePolygon is used to draw a polygon with the verticies (at least 3) specified, using the color index specified. See also gdImageFilledPolygon.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. /* Points of polygon */
  6. gdPoint points[3];
  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
  11. blue all maximum). */
  12. white = gdImageColorAllocate(im, 255, 255, 255);
  13. /* Draw a triangle. */
  14. points[0].x = 50;
  15. points[0].y = 0;
  16. points[1].x = 99;
  17. points[1].y = 99;
  18. points[2].x = 0;
  19. points[2].y = 99;
  20. gdImagePolygon(im, points, 3, white);
  21. /* ... Do something with the image, such as
  22. saving it to a file... */
  23. /* Destroy it */
  24. gdImageDestroy(im);

void gdImageOpenPolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color)

gdImageOpenPolygon is used to draw a sequence of lines with the verticies (at least 3) specified, using the color index specified. Unlike gdImagePolygon, the enpoints of the line sequence are not connected to a closed polygon.

void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)

gdImageRectangle is used to draw a rectangle with the two corners (upper left first, then lower right) specified, using the color index specified.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = gdImageColorAllocate(im, 255, 255, 255);
  10. /* Draw a rectangle occupying the central area. */
  11. gdImageRectangle(im, 25, 25, 74, 74, white);
  12. /* ... Do something with the image, such as
  13. saving it to a file... */
  14. /* Destroy it */
  15. gdImageDestroy(im);

void gdImageFilledPolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color)

gdImageFilledPolygon is used to fill a polygon with the verticies (at least 3) specified, using the color index specified. See also gdImagePolygon.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. int red;
  6. /* Points of polygon */
  7. gdPoint points[3];
  8. im = gdImageCreate(100, 100);
  9. /* Background color (first allocated) */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Allocate the color white (red, green and blue all maximum). */
  12. white = gdImageColorAllocate(im, 255, 255, 255);
  13. /* Allocate the color red. */
  14. red = gdImageColorAllocate(im, 255, 0, 0);
  15. /* Draw a triangle. */
  16. points[0].x = 50;
  17. points[0].y = 0;
  18. points[1].x = 99;
  19. points[1].y = 99;
  20. points[2].x = 0;
  21. points[2].y = 99;
  22. /* Paint it in white */
  23. gdImageFilledPolygon(im, points, 3, white);
  24. /* Outline it in red; must be done second */
  25. gdImagePolygon(im, points, 3, red);
  26. /* ... Do something with the image, such as
  27. saving it to a file... */
  28. /* Destroy it */
  29. gdImageDestroy(im);

void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)

gdImageFilledRectangle is used to draw a solid rectangle with the two corners (upper left first, then lower right) specified, using the color index specified.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = int gdImageColorAllocate(im, 255, 255, 255);
  10. /* Draw a filled rectangle occupying the central area. */
  11. gdImageFilledRectangle(im, 25, 25, 74, 74, white);
  12. /* ... Do something with the image, such as
  13. saving it to a file... */
  14. /* Destroy it */
  15. gdImageDestroy(im);

void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color)

gdImageArc is used to draw a partial ellipse centered at the given point, with the specified width and height in pixels. The arc begins at the position in degrees specified by s and ends at the position specified by e. The arc is drawn in the color specified by the last argument. A circle can be drawn by beginning from 0 degrees and ending at 360 degrees, with width and height being equal. e must be greater than s. Values greater than 360 are interpreted modulo 360.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 50);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = gdImageColorAllocate(im, 255, 255, 255);
  10. /* Inscribe an ellipse in the image. */
  11. gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
  12. /* ... Do something with the image, such as
  13. saving it to a file... */
  14. /* Destroy it */
  15. gdImageDestroy(im);

void gdImageFilledArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color, int style)

gdImageFilledArc is used to draw a partial ellipse centered at the given point, with the specified width and height in pixels. The arc begins at the position in degrees specified by s and ends at the position specified by e. The arc is filled in the color specified by the second to last argument. A circle can be drawn by beginning from 0 degrees and ending at 360 degrees, with width and height being equal. e must be greater than s. Values greater than 360 are interpreted modulo 360. The last argument is a bitwise OR of the following possibilities:

gdArc and gdChord are mutually exclusive; gdChord just connects the starting and ending angles with a straight line, while gdArc produces a rounded edge. gdPie is a synonym for gdArc. gdNoFill indicates that the arc or chord should be outlined, not filled. gdEdged, used together with gdNoFill, indicates that the beginning and ending angles should be connected to the center; this is a good way to outline (rather than fill) a 'pie slice'.

  1. /*... inside a function ...*(
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 50);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = gdImageColorAllocate(im, 255, 255, 255);
  10. /* Inscribe a filled pie slice in the image. */
  11. gdImageFilledArc(im, 50, 25, 98, 48, 0, 45, white, gdArc);
  12. /* ... Do something with the image, such as
  13. saving it to a file... */
  14. /* Destroy it */
  15. gdImageDestroy(im);

void gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color)

gdImageFilledEllipse is used to draw an ellipse centered at the given point, with the specified width and height in pixels. The ellipse is filled in the color specified by the last argument.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 50);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = gdImageColorAllocate(im, 255, 255, 255);
  10. /* Inscribe a filled ellipse in the image. */
  11. gdImageFilledEllipse(im, 50, 25, 98, 48, white);
  12. /* ... Do something with the image, such as
  13. saving it to a file... */
  14. /* Destroy it */
  15. gdImageDestroy(im);

void gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color)

gdImageFillToBorder floods a portion of the image with the specified color, beginning at the specified point and stopping at the specified border color. For a way of flooding an area defined by the color of the starting point, see gdImageFill.

The border color cannot be a special color such as gdTiled; it must be a proper solid color. The fill color can be, however.

Note that gdImageFillToBorder is recursive. It is not the most naive implementation possible, and the implementation is expected to improve, but there will always be degenerate cases in which the stack can become very deep. This can be a problem in MSDOS and MS Windows 3.1 environments. (Of course, in a Unix or Windows 95/98/NT environment with a proper stack, this is not a problem at all.)

  1. /* ... inside a function ... */
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. int red;
  6. im = gdImageCreate(100, 50);
  7. /* Background color (first allocated) */
  8. black = gdImageColorAllocate(im, 0, 0, 0);
  9. /* Allocate the color white (red, green and blue all maximum). */
  10. white = gdImageColorAllocate(im, 255, 255, 255);
  11. /* Allocate the color red. */
  12. red = gdImageColorAllocate(im, 255, 0, 0);
  13. /* Inscribe an ellipse in the image. */
  14. gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
  15. /* Flood-fill the ellipse. Fill color is red, border color is white (ellipse). */
  16. gdImageFillToBorder(im, 50, 50, white, red);
  17. /* ... Do something with the image, such as saving it to a file... */
  18. /* Destroy it */
  19. gdImageDestroy(im);

void gdImageFill(gdImagePtr im, int x, int y, int color)

gdImageFill floods a portion of the image with the specified color, beginning at the specified point and flooding the surrounding region of the same color as the starting point. For a way of flooding a region defined by a specific border color rather than by its interior color, see gdImageFillToBorder.

The fill color can be gdTiled, resulting in a tile fill using another image as the tile. However, the tile image cannot be transparent. If the image you wish to fill with has a transparent color index, call gdImageTransparent on the tile image and set the transparent color index to -1 to turn off its transparency.

Note that gdImageFill is recursive. It is not the most naive implementation possible, and the implementation is expected to improve, but there will always be degenerate cases in which the stack can become very deep. This can be a problem in MSDOS and MS Windows environments. (Of course, in a Unix or Windows 95/98/NT environment with a proper stack, this is not a problem at all.)

  1. /* ... inside a function ... */
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. int red;
  6. im = gdImageCreate(100, 50);
  7. /* Background color (first allocated) */
  8. black = gdImageColorAllocate(im, 0, 0, 0);
  9. /* Allocate the color white (red, green and blue all maximum). */
  10. white = gdImageColorAllocate(im, 255, 255, 255);
  11. /* Allocate the color red. */
  12. red = gdImageColorAllocate(im, 255, 0, 0);
  13. /* Inscribe an ellipse in the image. */
  14. gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
  15. /* Flood-fill the ellipse. Fill color is red, and will replace the
  16. black interior of the ellipse. */
  17. gdImageFill(im, 50, 50, red);
  18. /* ... Do something with the image, such as
  19. saving it to a file... */
  20. /* Destroy it */
  21. gdImageDestroy(im);

void gdImageSetAntiAliased(gdImagePtr im, int c)

"Antialiasing" is a process by which jagged edges associated with line drawing can be reduced by blending the foreground color with an appropriate percentage of the background, depending on how much of the pixel in question is actually within the boundaries of the line being drawn. All line-drawing functions, such as gdImageLine, gdImageOpenPolygon and gdImagePolygon, will draw antialiased lines if the special "color" gdAntiAliased is used when calling them.

gdImageSetAntiAliased is used to specify the actual foreground color to be used when drawing antialiased lines. You may set any color to be the foreground, however as of version 2.0.12 an alpha channel component is not supported.

Antialiased lines can be drawn on both truecolor and palette-based images. However, attempts to draw antialiased lines on highly complex palette-based backgrounds may not give satisfactory results, due to the limited number of colors available in the palette. Antialiased line-drawing on simple backgrounds should work well with palette-based images; otherwise create or fetch a truecolor image instead.

You need not take any special action when you are finished with antialised line drawing.

  1. /*... inside a function ...*/
  2. gdImagePtr im, brush;
  3. int black;
  4. int blue;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. blue = gdImageColorAllocate(im, 0, 0, 255);
  9. gdImageSetAntiAliased(im, blue);
  10. /* Draw a smooth line from the upper left corner to the
  11. lower right corner. */
  12. gdImageLine(im, 0, 0, 99, 99, gdAntiAliased);
  13. /* ... Do something with the image, such as
  14. saving it to a file... */
  15. /* Destroy it */
  16. gdImageDestroy(im);

See also gdAntiAliased and gdSetAntiAliasedDontBlend.

void gdImageSetAntiAliasedDontBlend(gdImagePtr im, int c)

Normally, when drawing lines with the special gdAntiAliased "color," blending with the background to reduce jagged edges is the desired behavior. However, when it is desired that lines not be blended with one particular color when it is encountered in the background, the gdImageSetAntiAliasedDontBlend function can be used to indicate the special color that the foreground should stand out more clearly against.

  1. /*... inside a function ...*/
  2. gdImagePtr im, brush;
  3. int black;
  4. int blue;
  5. int white;
  6. im = gdImageCreate(100, 100);
  7. /* Background color (first allocated) */
  8. black = gdImageColorAllocate(im, 0, 0, 0);
  9. blue = gdImageColorAllocate(im, 0, 0, 255);
  10. white = gdImageColorAllocate(im, 255, 255, 255);
  11. gdImageSetAntiAliased(im, blue);
  12. /* The portion of the line that crosses this white rectangle will
  13. not be blended smoothly */
  14. gdImageSetAntiAliasedDontBlend(im, white);
  15. gdImageFilledRectangle(im, 25, 25, 75, 75, white);
  16. /* Draw a smooth line from the upper left corner
  17. to the lower right corner. */
  18. gdImageLine(im, 0, 0, 99, 99, gdAntiAliased);
  19. /* ... Do something with the image, such as
  20. saving it to a file... */
  21. /* Destroy it */
  22. gdImageDestroy(im);

See also gdAntiAliased and gdSetAntiAliased.

void gdImageSetBrush(gdImagePtr im, gdImagePtr brush)

A "brush" is an image used to draw wide, shaped strokes in another image. Just as a paintbrush is not a single point, a brush image need not be a single pixel. Any gd image can be used as a brush, and by setting the transparent color index of the brush image with gdImageColorTransparent, a brush of any shape can be created. All line-drawing functions, such as gdImageLine, gdImageOpenPolygon and gdImagePolygon, will use the current brush if the special "color" gdBrushed or gdStyledBrushed is used when calling them.

gdImageSetBrush is used to specify the brush to be used in a particular image. You can set any image to be the brush. If the brush image does not have the same color map as the first image, any colors missing from the first image will be allocated. If not enough colors can be allocated, the closest colors already available will be used. This allows arbitrary PNGs to be used as brush images. It also means, however, that you should not set a brush unless you will actually use it; if you set a rapid succession of different brush images, you can quickly fill your color map, and the results will not be optimal.

You need not take any special action when you are finished with a brush. As for any other image, if you will not be using the brush image for any further purpose, you should call gdImageDestroy. You must not use the color gdBrushed if the current brush has been destroyed; you can of course set a new brush to replace it.

  1. /*... inside a function ...*/
  2. gdImagePtr im, brush;
  3. FILE *in;
  4. int black;
  5. im = gdImageCreate(100, 100);
  6. /* Open the brush PNG. For best results, portions of the
  7. brush that should be transparent (ie, not part of the
  8. brush shape) should have the transparent color index. */
  9. in = fopen("star.png", "rb");
  10. brush = gdImageCreateFromPng(in);
  11. /* Background color (first allocated) */
  12. black = gdImageColorAllocate(im, 0, 0, 0);
  13. gdImageSetBrush(im, brush);
  14. /* Draw a line from the upper left corner to the lower right corner
  15. using the brush. */
  16. gdImageLine(im, 0, 0, 99, 99, gdBrushed);
  17. /* ... Do something with the image, such as
  18. saving it to a file... */
  19. /* Destroy it */
  20. gdImageDestroy(im);
  21. /* Destroy the brush image */
  22. gdImageDestroy(brush);

void gdImageSetTile(gdImagePtr im, gdImagePtr tile)

A "tile" is an image used to fill an area with a repeated pattern. Any gd image can be used as a tile, and by setting the transparent color index of the tile image with gdImageColorTransparent, a tile that allows certain parts of the underlying area to shine through can be created. All region-filling functions, such as gdImageFill and gdImageFilledPolygon, will use the current tile if the special "color" gdTiled is used when calling them.

gdImageSetTile is used to specify the tile to be used in a particular image. You can set any image to be the tile. If the tile image does not have the same color map as the first image, any colors missing from the first image will be allocated. If not enough colors can be allocated, the closest colors already available will be used. This allows arbitrary PNGs to be used as tile images. It also means, however, that you should not set a tile unless you will actually use it; if you set a rapid succession of different tile images, you can quickly fill your color map, and the results will not be optimal.

You need not take any special action when you are finished with a tile. As for any other image, if you will not be using the tile image for any further purpose, you should call gdImageDestroy. You must not use the color gdTiled if the current tile has been destroyed; you can of course set a new tile to replace it.

  1. /*... inside a function ...*/
  2. gdImagePtr im, tile;
  3. FILE *in;
  4. int black;
  5. im = gdImageCreate(100, 100);
  6. /* Open the tile PNG. For best results, portions of the
  7. tile that should be transparent (ie, allowing the
  8. background to shine through) should have the transparent
  9. color index. */
  10. in = fopen("star.png", "rb");
  11. tile = gdImageCreateFromPng(in);
  12. /* Background color (first allocated) */
  13. black = gdImageColorAllocate(im, 0, 0, 0);
  14. gdImageSetTile(im, tile);
  15. /* Fill an area using the tile. */
  16. gdImageFilledRectangle(im, 25, 25, 75, 75, gdTiled);
  17. /* ... Do something with the image, such as
  18. saving it to a file... */
  19. /* Destroy it */
  20. gdImageDestroy(im);
  21. /* Destroy the tile image */
  22. gdImageDestroy(tile);

void gdImageSetStyle(gdImagePtr im, int *style, int styleLength)

It is often desirable to draw dashed lines, dotted lines, and other variations on a broken line. gdImageSetStyle can be used to set any desired series of colors, including a special color that leaves the background intact, to be repeated during the drawing of a line.

To use gdImageSetStyle, create an array of integers and assign them the desired series of color values to be repeated. You can assign the special color value gdTransparent to indicate that the existing color should be left unchanged for that particular pixel (allowing a dashed line to be attractively drawn over an existing image).

Then, to draw a line using the style, use the normal gdImageLine function with the special color value gdStyled.

As of version 1.1.1, the style array is copied when you set the style, so you need not be concerned with keeping the array around indefinitely. This should not break existing code that assumes styles are not copied.

You can also combine styles and brushes to draw the brush image at intervals instead of in a continuous stroke. When creating a style for use with a brush, the style values are interpreted differently: zero (0) indicates pixels at which the brush should not be drawn, while one (1) indicates pixels at which the brush should be drawn. To draw a styled, brushed line, you must use the special color value gdStyledBrushed. For an example of this feature in use, see gddemo.c (provided in the distribution).

  1. gdImagePtr im;
  2. int styleDotted[2], styleDashed[6];
  3. FILE *in;
  4. int black;
  5. int red;
  6. im = gdImageCreate(100, 100);
  7. /* Background color (first allocated) */
  8. black = gdImageColorAllocate(im, 0, 0, 0);
  9. red = gdImageColorAllocate(im, 255, 0, 0);
  10. /* Set up dotted style. Leave every other pixel alone. */
  11. styleDotted[0] = red;
  12. styleDotted[1] = gdTransparent;
  13. /* Set up dashed style. Three on, three off. */
  14. styleDashed[0] = red;
  15. styleDashed[1] = red;
  16. styleDashed[2] = red;
  17. styleDashed[3] = gdTransparent;
  18. styleDashed[4] = gdTransparent;
  19. styleDashed[5] = gdTransparent;
  20. /* Set dotted style. Note that we have to specify how many pixels are
  21. in the style! */
  22. gdImageSetStyle(im, styleDotted, 2);
  23. /* Draw a line from the upper left corner to the lower right corner. */
  24. gdImageLine(im, 0, 0, 99, 99, gdStyled);
  25. /* Now the dashed line. */
  26. gdImageSetStyle(im, styleDashed, 6);
  27. gdImageLine(im, 0, 99, 0, 99, gdStyled);
  28. /* ... Do something with the image, such as
  29. saving it to a file ... */
  30. /* Destroy it */
  31. gdImageDestroy(im);

void gdImageSetThickness(gdImagePtr im, int thickness)

gdImageSetThickness determines the width of lines drawn by the gdImageLine, gdImagePolygon, gdImageOpenPolygon and related functions, in pixels.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = gdImageColorAllocate(im, 255, 255, 255);
  10. /* Set thickness. */
  11. gdImageSetThickness(im, 4);
  12. /* Draw a fat line from the upper left corner to the lower right corner. */
  13. gdImageLine(im, 0, 0, 99, 99, white);
  14. /* ... Do something with the image, such as
  15. saving it to a file... */
  16. /* Destroy it */
  17. gdImageDestroy(im);

void gdImageAlphaBlending(gdImagePtr im, int blending)

The gdImageAlphaBlending function allows for two different modes of drawing on truecolor images. In blending mode, which is on by default (gd 2.0.2 and above), the alpha channel component of the color supplied to all drawing functions, such as gdImageSetPixel, determines how much of the underlying color should be allowed to shine through. As a result, gd automatically blends the existing color at that point with the drawing color, and stores the result in the image. The resulting pixel is opaque. In non-blending mode, the drawing color is copied literally with its alpha channel information, replacing the destination pixel. Blending mode is not available when drawing on palette images.

  1. gdImagePtr im;
  2. int red, blue;
  3. im = gdImageCreateTrueColor(100, 100);
  4. /* Background color */
  5. red = gdTrueColor(255, 0, 0);
  6. gdImageFilledRectangle(im, 0, 0, 100, 100, red);
  7. /* Drawing color. Full transparency would be an alpha channel value
  8. of 127 (gd has a 7 bit alpha chnanel). 0 is opaque,
  9. 127 is transparent. So cut gdAlphaTransparent in half to get
  10. 50% blending. */
  11. blue = gdTrueColorAlpha(0, 0, 255, gdAlphaTransparent / 2);
  12. /* Draw with blending. Result will be 50% red, 50% blue: yellow
  13. (emitted light, remember, not reflected light. What you learned
  14. in Kindergarten is wrong here). */
  15. gdImageAlphaBlending(im, 1);
  16. gdImageFilledRectangle(im, 0, 0, 25, 25, blue);
  17. /* Draw without blending. Result will be 50% blue, 50%
  18. the background color of the image viewer or web browser
  19. used; results in browsers that don't support
  20. semi-transparent pixels are unpredictable! */
  21. gdImageAlphaBlending(im, 0);
  22. gdImageFilledRectangle(im, 75, 75, 25, 25, blue);
  23. /* Write the image to disk, etc. */

void gdImageSaveAlpha(gdImagePtr im, int saveFlag)

By default, gd 2.0.2 and above do not attempt to save full alpha channel information (as opposed to single-color transparency) when saving PNG images. (PNG is currently the only output format supported by gd which can accommodate alpa channel information.) This saves space in the output file. If you wish to create an image with alpha channel information for use with tools that support it, call gdImageSaveAlpha(im, 1) to turn on saving of such information, and call gdImageAlphaBlending(im, 0) to turn off alpha blending within the library so that alpha channel information is actually stored in the image rather than being composited immediately at the time that drawing functions are invoked.

void gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2)

Establishes a clipping rectangle. Once gdImageSetClip has been called, all future drawing operations will remain within the specified clipping area, until a new gdImageSetClip call takes place. For instance, if a clipping rectangle of 25, 25, 75, 75 has been set within a 100x100 image, a diagonal line from 0,0 to 99,99 will appear only between 25,25 and 75,75.

If gdImageSetClip is never called, the clipping area will be the entire image.

The parameters passed to gdImageSetClip are checked against the dimensions of the image and limited to "safe" values.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black;
  4. int white;
  5. im = gdImageCreate(100, 100);
  6. /* Background color (first allocated) */
  7. black = gdImageColorAllocate(im, 0, 0, 0);
  8. /* Allocate the color white (red, green and blue all maximum). */
  9. white = gdImageColorAllocate(im, 255, 255, 255);
  10. /* Set the clipping rectangle. */
  11. gdImageSetClip(im, 25, 25, 75, 75);
  12. /* Draw a line from the upper left corner to the lower right corner.
  13. Only the part within the clipping rectangle will appear. */
  14. gdImageLine(im, 0, 0, 99, 99, white);
  15. /* ... Do something with the image, such as
  16. saving it to a file ... */
  17. /* Destroy it */
  18. gdImageDestroy(im);

See also gdImageGetClip.

void gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P)

Fetches the boundaries of the current clipping rectangle.

  1. /*... Inside a function ... */
  2. gdImagePtr im = gdImageCreateTrueColor(100, 100);
  3. int x1, y1, x2, y2;
  4. gdImageSetClip(im, 25, 25, 75, 75);
  5. gdImageGetClip(im, &x1, &y1, &x2, &y2);
  6. printf("%d %d %d %d\n", x1, y1, x2, y2);

The above code would print:

   25 25 75 75

See also gdImageSetClip.





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

This library was originally developed by Thomas Boutell