wolffd@0: wolffd@0: wolffd@0: gd 2.0.34 wolffd@0: wolffd@0: wolffd@0: wolffd@0:

gd 2.0.33

wolffd@0:

A graphics library for fast image creation

wolffd@0:

Follow this link to the wolffd@0: latest version wolffd@0: of this document.

wolffd@0:
wolffd@0: UPGRADING UNIX USERS: READ THIS FIRST! wolffd@0: Modern versions of gd install by default to /usr/local/lib and wolffd@0: /usr/local/include. If you already have an older version of gd wolffd@0: in /usr/lib and /usr/include, you may wish to use: wolffd@0:
wolffd@0: ./configure --prefix=/usr
wolffd@0: To ensure that your wolffd@0: new installation overwrites the old. wolffd@0:

wolffd@0: GIF support has been restored in gd 2.0.28 and above. wolffd@0: The well-known patents on LZW compression held by Unisys wolffd@0: have expired in all countries. British Telecom and IBM may hold related wolffd@0: patents but have never chosen to require royalties for GIF applications, wolffd@0: to the best of my knowledge. I am not a lawyer and cannot give wolffd@0: legal advice regarding this issue. PNG remains a superior format especially wolffd@0: if lossless truecolor images are needed. wolffd@0:

wolffd@0: When building from soruce, gd 2.0.33 requires that the wolffd@0: following libraries also be installed, in order to produce the related wolffd@0: image formats. The win32 binary release (bgd) already contains the wolffd@0: appropriate libraries. wolffd@0: You may skip libraries associated with formats you do not use: wolffd@0:

wolffd@0: libpng (see the libpng home page), if you want PNG wolffd@0:

wolffd@0: zlib (see the info-zip home page), if you want PNG wolffd@0:

wolffd@0: jpeg-6b or later, if desired (see the Independent JPEG Group home page), if you want JPEG wolffd@0:

wolffd@0: If you want to use the TrueType font support, you must also wolffd@0: install the FreeType 2.x library, including wolffd@0: the header files. See the Freetype wolffd@0: Home Page, or SourceForge. wolffd@0: No, I cannot explain why that site is down on a particular day, and no, I wolffd@0: can't send you a copy. wolffd@0:

wolffd@0: If you want to use the Xpm color bitmap loading support, you must also wolffd@0: have the X Window System and the Xpm library installed (Xpm is often wolffd@0: included in modern X distributions). Most of the time you won't wolffd@0: need Xpm. wolffd@0:

wolffd@0: Please read the documentation and install the required libraries. wolffd@0: Do not send email asking why png.h is not found. wolffd@0: Do not send email asking why libgd.so is not found, either. wolffd@0: See the requirements section for more wolffd@0: information. Thank you! wolffd@0:

wolffd@0:

Table of Contents

wolffd@0: wolffd@0:

wolffd@0: Up to the LibGD Homepage wolffd@0:

Credits and license terms

wolffd@0:

wolffd@0: In order to resolve any possible confusion regarding the authorship wolffd@0: of gd, the following copyright statement covers all of the authors wolffd@0: who have required such a statement. If you are aware of any oversights wolffd@0: in this copyright notice, please contact Pierre-A. Joye who will be wolffd@0: pleased to correct them. wolffd@0:

wolffd@0: COPYRIGHT STATEMENT FOLLOWS THIS LINE
wolffd@0: 
wolffd@0:
wolffd@0: wolffd@0: Portions copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 by Cold Spring wolffd@0: Harbor Laboratory. Funded under Grant P41-RR02188 by the National wolffd@0: Institutes of Health. wolffd@0:

wolffd@0: Portions copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 by Boutell.Com, Inc. wolffd@0:

wolffd@0: Portions relating to GD2 format copyright 1999, 2000, 2001, 2002, 2003, 2004 Philip Warner. wolffd@0:

wolffd@0: Portions relating to PNG copyright 1999, 2000, 2001, 2002, 2003, 2004 Greg Roelofs. wolffd@0:

wolffd@0: Portions relating to gdttf.c copyright 1999, 2000, 2001, 2002, 2003, 2004 John Ellson (ellson@graphviz.org). wolffd@0:

wolffd@0: Portions relating to gdft.c copyright 2001, 2002, 2003, 2004 John Ellson (ellson@graphviz.org). wolffd@0:

wolffd@0: Portions copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Pierre-Alain Joye (pierre@libgd.org). wolffd@0: wolffd@0:

wolffd@0: Portions relating to JPEG and to color quantization copyright 2000, 2001, 2002, 2003, 2004, Doug Becker and copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Thomas G. Lane. This software is based wolffd@0: in part on the work of the Independent JPEG Group. See the file wolffd@0: README-JPEG.TXT for more information. wolffd@0:

wolffd@0: Portions relating to GIF compression copyright 1989 by Jef wolffd@0: Poskanzer and David Rowley, with modifications for thread safety wolffd@0: by Thomas Boutell. wolffd@0:

wolffd@0: Portions relating to GIF decompression copyright 1990, 1991, 1993 wolffd@0: by David Koblas, with modifications for thread safety by wolffd@0: Thomas Boutell. wolffd@0:

wolffd@0: Portions relating to WBMP copyright 2000, 2001, 2002, 2003, 2004 Maurice Szmurlo and Johan Van wolffd@0: den Brande. wolffd@0:

wolffd@0: Portions relating to GIF animations copyright 2004 Jaakko Hyvätti (jaakko.hyvatti@iki.fi) wolffd@0:

wolffd@0: Permission has been granted to copy, distribute and modify gd in any wolffd@0: context without fee, including a commercial application, provided that this notice wolffd@0: is present in user-accessible supporting documentation. wolffd@0:

wolffd@0: This does not affect your ownership of the derived work itself, and the intent wolffd@0: is to assure proper credit for the authors of gd, not to interfere wolffd@0: with your productive use of gd. If you have questions, ask. wolffd@0: "Derived works" includes all programs that utilize the library. wolffd@0: Credit must be given in user-accessible documentation. wolffd@0:

wolffd@0: This software is provided "AS IS." wolffd@0: The copyright holders disclaim all warranties, either express or implied, wolffd@0: including but not limited to implied warranties of merchantability and wolffd@0: fitness for a particular purpose, with respect to this code and accompanying wolffd@0: documentation. wolffd@0:

wolffd@0: Although their code does not appear in the current release, the authors wolffd@0: also wish to thank Hutchison Avenue Software Corporation for their wolffd@0: prior contributions. wolffd@0:

wolffd@0:
wolffd@0: END OF COPYRIGHT STATEMENT
wolffd@0: 
wolffd@0:

What is gd?

wolffd@0:

wolffd@0: gd is a graphics library. It allows your code to quickly wolffd@0: draw images complete with lines, arcs, text, multiple wolffd@0: colors, cut and paste from other images, and flood fills, and wolffd@0: write out the result as a PNG or JPEG file. This is particularly wolffd@0: useful in World Wide Web applications, where PNG and JPEG are two wolffd@0: of the formats accepted for inline images by most browsers. wolffd@0:

wolffd@0: gd is not a paint program. wolffd@0: If you are looking for a paint program, you are looking in wolffd@0: the wrong place. If you are not a programmer, you are looking wolffd@0: in the wrong place, unless you are installing a required wolffd@0: library in order to run an application. wolffd@0:

wolffd@0: gd does not provide for every possible desirable graphics wolffd@0: operation. It is not necessary or desirable for gd to become wolffd@0: a kitchen-sink graphics package, but version 2.0 does include wolffd@0: most frequently requested features, including both truecolor and wolffd@0: palette images, resampling (smooth resizing of truecolor images) wolffd@0: and so forth. wolffd@0:

wolffd@0:

What if I want to use another programming wolffd@0: language?

wolffd@0: Not all of these tools are necessarily up to date and fully compatible wolffd@0: with 2.0.33. wolffd@0:

PHP

wolffd@0: A variant of gd 2.x is included in PHP 4.3.0. It is also possible wolffd@0: to patch PHP 4.2.3 for use with gd 2.0.33; see the wolffd@0: gd home page for a link to wolffd@0: that information. It would be a Good Idea to merge all of the things wolffd@0: that are better in mainstream gd and all of the things that are wolffd@0: better in PHP gd at some point in the near future. wolffd@0:

Perl

wolffd@0: gd can also be used from Perl, courtesy of wolffd@0: Lincoln Stein's wolffd@0: wolffd@0: GD.pm library, which uses gd as the basis for a set of wolffd@0: Perl 5.x classes. Highly recommended. wolffd@0:

OCaml

wolffd@0: gd can be used from OCaml, thanks to wolffd@0: Matt Gushee's GD4O project. wolffd@0:

Tcl

wolffd@0: gd can be used from Tcl with John Ellson's wolffd@0: Gdtclft wolffd@0: dynamically loaded extension package. wolffd@0:

Pascal

wolffd@0: Pascal enthusiasts should look into the wolffd@0: freepascal project, a wolffd@0: free Pascal compiler that includes gd support. wolffd@0:

REXX

wolffd@0: A wolffd@0: gd interface wolffd@0: for the REXX language is available. wolffd@0:

Any Language

wolffd@0: The "fly" interpreter performs gd operations specified in a text file. wolffd@0: You can output the desired commands to a simple wolffd@0: text file from whatever scripting language you prefer to use, then wolffd@0: invoke the interpreter. wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.34?

wolffd@0:

wolffd@0: From 2.0.34 and later, please check the ISSUES and ChangeLog as well as wolffd@0: the releases announcements. wolffd@0:

wolffd@0:

What's new in version 2.0.33?

wolffd@0:

wolffd@0: Version 2.0.33 restores compatibility with older releases wolffd@0: of Freetype 2.x in addition to the latest release. Thanks to wolffd@0: John Ellson and the graphviz project. wolffd@0:

wolffd@0:

What's new in version 2.0.32?

wolffd@0:

wolffd@0: Version 2.0.32 restores correct detection of Unicode character sets wolffd@0: for freetype fonts, which repairs a bug that prevented umlauts from wolffd@0: displaying properly. Thanks to John Ellson and the graphviz project. wolffd@0: Also, version 2.0.32 builds all test programs wolffd@0: smoothly in the absence of libpng. wolffd@0:

wolffd@0:

What's new in version 2.0.31?

wolffd@0:

wolffd@0: A minor type naming conflict prevented bgd.dll from compiling, and it wolffd@0: was left out of the distribution as a result. This has been corrected. wolffd@0:

wolffd@0:

What's new in version 2.0.30?

wolffd@0:

wolffd@0: 2.0.29 did not compile correctly when freetype was not available. wolffd@0: This has been corrected. Thanks to Alessandro Ranellucci. wolffd@0:

wolffd@0:

What's new in version 2.0.29?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.28?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.27?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.26?

wolffd@0:

wolffd@0: The following enhancements and fixes: wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.25?

wolffd@0:

wolffd@0: Owing to an oversight while making changes to better accommodate the use wolffd@0: of gd as a DLL, the extern qualifier was dropped from the wolffd@0: declarations of font pointers in 2.0.24. This has been corrected. wolffd@0: Thanks to Richard ("OpenMacNews"). wolffd@0:

wolffd@0:

What's new in version 2.0.24?

wolffd@0:

wolffd@0: Windows DLL now uses __stdcall calling convention. Existing wolffd@0: applications will require a recompile, using the new version of gd.h, wolffd@0: in order to use this version of the DLL. However, Visual BASIC and other wolffd@0: non-C programmers will now be able to use the DLL, which is an enormous wolffd@0: benefit and justifies the one-time inconvenience to existing DLL users. wolffd@0:

wolffd@0: The elaborate #ifdef test for older versions of Freetype without wolffd@0: FT_ENCODING_MS_SYMBOL was needed in a second place also. Thanks to wolffd@0: David R. Morrison. wolffd@0:

wolffd@0: An off-by-one error in gdImageToPalette caused transparency to be applied wolffd@0: to the wrong pixels. Thanks to "Super Pikeman." wolffd@0:

wolffd@0:

What's new in version 2.0.23?

wolffd@0:

wolffd@0: Output dpi specification option added to the wolffd@0: gdFTStringExtra structure, thanks to wolffd@0: Mark Shackelford. See gdImageStringFTEx. wolffd@0:

wolffd@0:

What's new in version 2.0.22?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.21?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.20?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.19?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.18?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.17?

wolffd@0:

wolffd@0: Minor compilation and packaging problems with 2.0.16 were corrected. wolffd@0: If 2.0.16 compiled without errors for you, then you don't need wolffd@0: to upgrade to 2.0.17. wolffd@0:

wolffd@0:

What's new in version 2.0.16?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.15?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.14?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.13?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.12?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.11?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.10?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.9?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.8?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.7?

wolffd@0:

wolffd@0: Version 2.0.7 corrects a problem which caused 'configure' to complain wolffd@0: that the directory NONE was not found, in various places, causing wolffd@0: the configuration process to stop. There are no code changes. wolffd@0:

wolffd@0:

What's new in version 2.0.6?

wolffd@0:

wolffd@0:

wolffd@0:

wolffd@0:

What's new in version 2.0.5?

wolffd@0: wolffd@0:

wolffd@0:

What's new in version 2.0.4?

wolffd@0: The following contributions from John Ellson: wolffd@0: wolffd@0: And the following additional fixes: wolffd@0: wolffd@0:

What's new in version 2.0.3?

wolffd@0: wolffd@0:

wolffd@0:

What's new in version 2.0.2?

wolffd@0: wolffd@0:

What's new in version 2.0.1?

wolffd@0: wolffd@0:

What's new in version 2.0?

wolffd@0: wolffd@0:

What's new in version 1.8.4?

wolffd@0: wolffd@0:

What's new in version 1.8.3?

wolffd@0: wolffd@0:

wolffd@0:

What's new in version 1.8.2?

wolffd@0: wolffd@0:

What's new in version 1.8.1?

wolffd@0: wolffd@0:

What's new in version 1.8?

wolffd@0: wolffd@0:

Additional JPEG Information

wolffd@0: Support for reading and writing JPEG-format images is courtesy wolffd@0: of Doug Becker and the Independent JPEG Group / Thomas G. Lane. You wolffd@0: can get the latest version of the IJG JPEG software from ftp://ftp.uu.net/graphics/jpeg/ wolffd@0: (e.g., the jpegsrc.v6b.tar.gz wolffd@0: file). You must use wolffd@0: version 6b or later of the IJG JPEG software. You might also consult wolffd@0: the JPEG FAQ at wolffd@0: http://www.faqs.org/faqs/jpeg-faq/. wolffd@0:

What's new in version 1.7.3?

wolffd@0: Another attempt at Makefile fixes to permit wolffd@0: linking with all libraries required on platforms with order- wolffd@0: dependent linkers. Perhaps it will work this time. wolffd@0:

What's new in version 1.7.2?

wolffd@0: An uninitialized-pointer bug in gdtestttf.c was corrected. wolffd@0: This bug caused crashes at the end of each call to gdImageStringTTF on wolffd@0: some platforms. Thanks to Wolfgang Haefelinger. wolffd@0:

wolffd@0: Documentation fixes. Thanks to Dohn Arms. wolffd@0:

wolffd@0: Makefile fixes to permit wolffd@0: linking with all libraries required on platforms with order- wolffd@0: dependent linkers. wolffd@0:

What's new in version 1.7.1?

wolffd@0: A minor buglet in the Makefile was corrected, as well as an inaccurate wolffd@0: error message in gdtestttf.c. Thanks to Masahito Yamaga. wolffd@0:

What's new in version 1.7?

wolffd@0: Version 1.7 contains the following changes: wolffd@0: wolffd@0:

What's new in version 1.6.3?

wolffd@0: Version 1.6.3 corrects a memory leak in gd_png.c. This leak caused wolffd@0: a significant amount of memory to be allocated and not freed when wolffd@0: writing a PNG image. wolffd@0:

What's new in version 1.6.2?

wolffd@0: Version 1.6.2 from John Ellson adds two new functions: wolffd@0: wolffd@0:

wolffd@0: Also in this release the build process has been converted to wolffd@0: GNU autoconf/automake/libtool conventions so that both (or either) wolffd@0: static and shared libraries can be built. wolffd@0:

What's new in version 1.6.1?

wolffd@0: Version 1.6.1 incorporates superior PNG reading and writing code wolffd@0: from Greg Roelofs, with minor modifications by Tom Boutell. wolffd@0: Specifically, I altered his code to read non-palette images wolffd@0: (converting them to palette images badly, by dithering them), wolffd@0: and to tolerate palette images with types of transparency that wolffd@0: gd doesn't actually support (it just ignores the advanced wolffd@0: transparency features). Any bugs in this area are therefore my wolffd@0: fault, not Greg's. wolffd@0:

wolffd@0: Unlike gd 1.6, users should have no trouble linking with wolffd@0: gd 1.6.1 if they follow the instructions and install all of wolffd@0: the pieces. However, If you get undefined symbol errors, wolffd@0: be sure to check for older versions of libpng in your wolffd@0: library directories! wolffd@0:

What's new in version 1.6?

wolffd@0: Version 1.6 features the following changes: wolffd@0:

wolffd@0: Support for 8-bit palette PNG images has been added. wolffd@0: Support for GIF has been removed. This step was taken wolffd@0: to completely avoid the legal controversy regarding the LZW wolffd@0: compression algorithm used in GIF. Unisys holds a patent which wolffd@0: is relevant to LZW compression. PNG is a superior image format wolffd@0: in any case. Now that PNG is supported by both Microsoft wolffd@0: Internet Explorer and Netscape (in their recent releases), wolffd@0: we highly recommend that GD users upgrade in order to get wolffd@0: well-compressed images in a format which is legally unemcumbered. wolffd@0: wolffd@0:

What's new in version 1.5?

wolffd@0: wolffd@0: Version 1.5 featured the following changes: wolffd@0: wolffd@0:
wolffd@0:
New GD2 format wolffd@0:
An improvement over the GD format, the GD2 format uses the zlib wolffd@0: compression library to compress the image in chunks. This results wolffd@0: in file sizes comparable to GIFs, with the ability to access parts wolffd@0: of large images without having to read the entire image into memory. wolffd@0:

wolffd@0: This format also supports version numbers and rudimentary validity wolffd@0: checks, so it should be more 'supportable' than the previous GD format. wolffd@0:

wolffd@0:

Re-arranged source files wolffd@0:
gd.c has been broken into constituant parts: io, gif, gd, gd2 and wolffd@0: graphics functions are now in separate files. wolffd@0:

wolffd@0:

Extended I/O capabilities. wolffd@0:
The source/sink feature has been extended to support GD2 file formats (which wolffd@0: require seek/tell functions; seek must return 1 for success, 0 for failure), and to allow more general non-file I/O. wolffd@0:

wolffd@0:

Better support for Lincoln Stein's Perl Module wolffd@0:
The new gdImage*Ptr function returns the chosen format stored in a block of memory. wolffd@0: This can be directly used by the GD perl module. wolffd@0:

wolffd@0:

Added functions wolffd@0:
gdImageCreateFromGd2Part - allows retrieval of part of an image (good for huge images, like maps), wolffd@0:
gdImagePaletteCopy - Copies a palette from one image to another, doing it's best to match the colors in the target image to the colors in the source palette. wolffd@0:
gdImageGd2, gdImageCreateFromGd2 - Support for new format wolffd@0:
gdImageCopyMerge - Merges two images (useful to highlight part of an image) wolffd@0:
gdImageCopyMergeGray - Similar to gdImageCopyMerge, but tries to preserve source image hue. wolffd@0:
gdImagePngPtr, gdImageJpegPtr, gdImageWBMPPtr, gdImageGdPtr, gdImageGd2Ptr - return memory blocks for each type of image. wolffd@0:
gdImageCreateFromPngCtx, gdImageCreateFromGdCtx, gdImageCreateFromGd2Ctx, gdImageCreateFromGd2PartCtx - Support for new I/O context. wolffd@0: wolffd@0:
wolffd@0: wolffd@0: NOTE: In fairness to Thomas Boutell, any bug/problems with any of the above features should wolffd@0: probably be reported to Philip Warner. wolffd@0: wolffd@0:

What's new in version 1.4?

wolffd@0: wolffd@0: Version 1.4 features the following changes: wolffd@0:
wolffd@0:
Fixed polygon fill routine (again) wolffd@0:
Thanks to Kirsten Schulz, version 1.4 is able to fill wolffd@0: numerous types of polygons that caused problems with wolffd@0: previous releases, including version 1.3. wolffd@0:
Support for alternate data sources wolffd@0:
Programmers who wish to load a GIF from something other wolffd@0: than a stdio FILE * stream can use the new wolffd@0: gdImageCreateFromPngSource function. wolffd@0:
Support for alternate data destinations wolffd@0:
Programmers who wish to write a GIF to something other wolffd@0: than a stdio FILE * stream can use the new wolffd@0: gdImagePngToSink function. wolffd@0:
More tolerant when reading GIFs wolffd@0:
wolffd@0: Version 1.4 does not crash when reading certain animated GIFs, wolffd@0: although it still only reads the first frame. Version 1.4 also has wolffd@0: overflow testing code to prevent crashes when reading wolffd@0: damaged GIFs. wolffd@0:
wolffd@0:

What's new in version 1.3?

wolffd@0: Version 1.3 features the following changes: wolffd@0:
wolffd@0:
Non-LZW-based GIF compression code wolffd@0:
wolffd@0: Version 1.3 contained GIF compression code that uses simple Run Length wolffd@0: Encoding instead of LZW compression, while still retaining compatibility wolffd@0: with normal LZW-based GIF decoders (your browser will still like your GIFs). wolffd@0: LZW compression is patented by Unisys. We are currently reevaluating wolffd@0: the approach taken by gd 1.3. The current release of gd does not support wolffd@0: this approach. We recommend that you use the current release, and generate wolffd@0: PNG images. Thanks to wolffd@0: Hutchison Avenue Software Corporation for contributing wolffd@0: the RLE GIF code. wolffd@0:
8-bit fonts, and 8-bit font support wolffd@0:
This improves support for European languages. Thanks are due wolffd@0: to Honza Pazdziora and also to wolffd@0: Jan Pazdziora . Also see the provided bdftogd wolffd@0: Perl script if you wish to convert fixed-width X11 fonts wolffd@0: to gd fonts. wolffd@0:
16-bit font support (no fonts provided) wolffd@0:
Although no such fonts are provided in the distribution, wolffd@0: fonts containing more than 256 characters should work if the wolffd@0: gdImageString16 and gdImageStringUp16 routines are used. wolffd@0:
Improvements to the "webpng" example/utility wolffd@0:
The "webpng" utility is now a slightly more useful application. Thanks to wolffd@0: Brian Dowling for this code. wolffd@0:
Corrections to the color resolution field of GIF output wolffd@0:
Thanks to Bruno Aureli. wolffd@0:
Fixed polygon fills wolffd@0:
A one-line patch for the infamous polygon fill bug, courtesy wolffd@0: of Jim Mason. I believe this fix is sufficient. However, if you wolffd@0: find a situation where polygon fills still fail to behave properly, wolffd@0: please send code that demonstrates the problem, and a fix if wolffd@0: you have one. Verifying the fix is important. wolffd@0:
Row-major, not column-major wolffd@0:
Internally, gd now represents the array of pixels as wolffd@0: an array of rows of pixels, rather than an array of columns wolffd@0: of pixels. This improves the performance of compression and wolffd@0: decompression routines slightly, because horizontally adjacent wolffd@0: pixels are now next to each other in memory. This should wolffd@0: not affect properly written gd applications, but applications that wolffd@0: directly manipulate the pixels array will require wolffd@0: changes. wolffd@0:
wolffd@0:

What else do I need to use gd?

wolffd@0:

wolffd@0: To use gd, you will need an ANSI C compiler. All popular wolffd@0: Windows 95 and NT C compilers are ANSI C compliant. Any wolffd@0: full-ANSI-standard C compiler should be adequate. The cc wolffd@0: compiler released with SunOS 4.1.3 is not an ANSI C compiler. wolffd@0: Most Unix users who do not already have gcc should get it. wolffd@0: gcc is free, ANSI compliant and a de facto industry standard. wolffd@0: Ask your ISP why it is missing. wolffd@0:

wolffd@0: As of version 1.6, you also need the zlib compression library, wolffd@0: and the libpng library. As of version 1.6.2, you can draw text wolffd@0: using antialiased TrueType fonts if you also have the libttf wolffd@0: library installed, but this is not mandatory. wolffd@0: zlib is available for a variety of platforms from wolffd@0: the zlib web site. wolffd@0: libpng is available for a variety of platforms from wolffd@0: the PNG web site. wolffd@0: wolffd@0:

wolffd@0: You will also want a PNG viewer, if you do not already have wolffd@0: one for your system, since you will need a good way to check the wolffd@0: results of your work. Netscape 4.04 and higher, and Microsoft wolffd@0: Internet Explorer 4.0 or higher, both support PNG. wolffd@0: Not every PNG-compatible viewer supports alpha channel wolffd@0: transparency, which is why gd 2.0.2 and above do alpha wolffd@0: blending in the library by default; it is possible to turn on the wolffd@0: saving of alpha channel information to the file instead. wolffd@0:

wolffd@0:

How do I get gd?

wolffd@0:

Binaries (DLL for Windows programmers):

wolffd@0: wolffd@0:

Source Code:

wolffd@0: wolffd@0:

wolffd@0:

How do I build gd?

wolffd@0:
wolffd@0: Win32 DLL users: if you are using MSVC, use the provided batch file wolffd@0: makemsvcimport.bat to make a bgd.lib import library wolffd@0: corresponding to the provided bgd.dll. Copy bgd.dll to your wolffd@0: application directory, or to your Windows sytem directory. In the wolffd@0: settings of your MSVC project, you MUST choose the wolffd@0: "multithreaded DLL" library option under "code generation." wolffd@0: mingw32 and cygwin users can simply link with the provided libbgd.a wolffd@0: stub library in order to use the DLL. wolffd@0:
wolffd@0: Building gd From the Source wolffd@0:

wolffd@0: In order to build gd, you must first unpack the archive you have wolffd@0: downloaded. If you are not familiar with tar and wolffd@0: gunzip (Unix) or ZIP (Windows), please wolffd@0: consult with an experienced user of your system. Sorry, we cannot wolffd@0: answer questions about basic Internet skills. wolffd@0:

wolffd@0: Unpacking the archive will produce a directory called "gd-2.0.33". wolffd@0:

wolffd@0:

For Unix

wolffd@0: cd to the 2.0.33 directory and type: wolffd@0:

wolffd@0: ./configure wolffd@0:

wolffd@0:

wolffd@0: NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN wolffd@0: /usr/local/lib and the include files are wolffd@0: installed in /usr/local/include. IF YOU ARE wolffd@0: UPGRADING, you may wish to use: wolffd@0:
wolffd@0: ./configure --prefix=/usr
wolffd@0: 
wolffd@0: Rather than just ./configure, before typing wolffd@0: make and make install. wolffd@0:
wolffd@0:

wolffd@0: If all goes well, this will create a Makefile. If all does not go well -- wolffd@0: for instance, if neither the the JPEG nor the PNG and ZLIB libraries wolffd@0: are found -- you will need to install those libraries, then come back wolffd@0: and run configure again. wolffd@0:

wolffd@0: If necessary, make changes to the resulting Makefile. Then, wolffd@0: type "make". If there are no errors, follow this with "make install". wolffd@0: Because gd 2.0 and above installs as a shared library, it is necessary to wolffd@0: install the library properly before running gd-based programs. wolffd@0:

wolffd@0: If you get errors, type ./configure --help for more wolffd@0: information about the available options. In the unlikely event wolffd@0: that the GNU autoconf-produced configure script does not work well wolffd@0: for you, you may wish to try configure.pl, a wolffd@0: simple Perl script with similar but less complete capabilities. wolffd@0: If all else fails, try renaming makefile.sample wolffd@0: to Makefile. However, ./configure is wolffd@0: almost always your best bet. wolffd@0:

wolffd@0:

For Windows

wolffd@0: Use the DLL version! See the paragraph at the beginning of this sectino. wolffd@0: If you really want to compile it yourself for some strange reason, read on. wolffd@0:

wolffd@0: Create a project using your favorite programming environment. wolffd@0: Copy all of the gd files to the project directory. Add gd.c wolffd@0: to your project. Add other source files as appropriate. Learning the wolffd@0: basic skills of creating projects with your chosen C environment wolffd@0: is up to you. Alternatively, use the free mingw32 wolffd@0: or cygwin tools, which may prove to be compatible wolffd@0: with the provided configure script. wolffd@0:

wolffd@0:

wolffd@0: If you wish to test the library, type "make test" AFTER you have wolffd@0: successfully executed "make install". This will build wolffd@0: several test programs, including "gddemo". (Not all of these wolffd@0: programs are expected to print completely successful messages, wolffd@0: depending on the nature of the image formats with which some of wolffd@0: the tests are tried; for instance, WBMP is a black and white wolffd@0: format, so loss of color information is expected there.) wolffd@0: Run gddemo to see some of the capabilities of gd. Run wolffd@0: gdtestft to play with the freetype support, if you have built wolffd@0: gd with it and have access to truetype fonts. wolffd@0:

wolffd@0: gddemo should execute without incident, creating the file wolffd@0: demoout.png. (Note there is also a file named demoin.png, wolffd@0: which is provided in the package as part of the demonstration.) wolffd@0:

wolffd@0: Display demoout.png in your PNG viewer. The image should wolffd@0: be 128x128 pixels and should contain an image of the wolffd@0: space shuttle with quite a lot of graphical elements drawn wolffd@0: on top of it. wolffd@0:

wolffd@0: (If you are missing the demoin.png file, the other items wolffd@0: should appear anyway.) wolffd@0:

wolffd@0: Look at demoin.png to see the original space shuttle wolffd@0: image which was scaled and copied into the output image. wolffd@0:

wolffd@0:

gd basics: using gd in your program

wolffd@0: gd lets you create PNG or JPEG images on the fly. To use gd in your wolffd@0: program, include the file gd.h, and link with the gd wolffd@0: library and the other required libraries; the syntax for wolffd@0: most Unix flavors is: wolffd@0:
wolffd@0: -lgd -lpng -lz -ljpeg -lfreetype -lm
wolffd@0: 
wolffd@0: Assuming that all of these libraries are available. wolffd@0:

wolffd@0: If you want to use the provided simple fonts, include wolffd@0: gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For wolffd@0: more impressive results, install FreeType 2.x and use the wolffd@0: gdImageStringFT wolffd@0: function. If you are not using the provided Makefile and/or a wolffd@0: library-based approach, be sure to include the source modules as well in your wolffd@0: project. (They may be too large for 16-bit memory models, wolffd@0: that is, 16-bit DOS and Windows.) wolffd@0:

wolffd@0: Here is a short example program. (For a more advanced example, wolffd@0: see gddemo.c, included in the distribution. gddemo.c is NOT the same program; wolffd@0: it demonstrates additional features!) wolffd@0:

wolffd@0:

wolffd@0: /* Bring in gd library functions */
wolffd@0: #include "gd.h"
wolffd@0: 
wolffd@0: /* Bring in standard I/O so we can output the PNG to a file */
wolffd@0: #include <stdio.h>
wolffd@0: 
wolffd@0: int main() {
wolffd@0:   /* Declare the image */
wolffd@0:   gdImagePtr im;
wolffd@0:   /* Declare output files */
wolffd@0:   FILE *pngout, *jpegout;
wolffd@0:   /* Declare color indexes */
wolffd@0:   int black;
wolffd@0:   int white;
wolffd@0: 
wolffd@0:   /* Allocate the image: 64 pixels across by 64 pixels tall */
wolffd@0:   im = gdImageCreate(64, 64);
wolffd@0: 
wolffd@0:   /* Allocate the color black (red, green and blue all minimum).
wolffd@0:     Since this is the first color in a new image, it will
wolffd@0:     be the background color. */
wolffd@0:   black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: 
wolffd@0:   /* Allocate the color white (red, green and blue all maximum). */
wolffd@0:   white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0:   
wolffd@0:   /* Draw a line from the upper left to the lower right,
wolffd@0:     using white color index. */
wolffd@0:   gdImageLine(im, 0, 0, 63, 63, white);  
wolffd@0: 
wolffd@0:   /* Open a file for writing. "wb" means "write binary", important
wolffd@0:     under MSDOS, harmless under Unix. */
wolffd@0:   pngout = fopen("test.png", "wb");
wolffd@0: 
wolffd@0:   /* Do the same for a JPEG-format file. */
wolffd@0:   jpegout = fopen("test.jpg", "wb");
wolffd@0: 
wolffd@0:   /* Output the image to the disk file in PNG format. */
wolffd@0:   gdImagePng(im, pngout);
wolffd@0: 
wolffd@0:   /* Output the same image in JPEG format, using the default
wolffd@0:     JPEG quality setting. */
wolffd@0:   gdImageJpeg(im, jpegout, -1);
wolffd@0: 
wolffd@0:   /* Close the files. */
wolffd@0:   fclose(pngout);
wolffd@0:   fclose(jpegout);
wolffd@0: 
wolffd@0:   /* Destroy the image in memory. */
wolffd@0:   gdImageDestroy(im);
wolffd@0: }
wolffd@0: 
wolffd@0: When executed, this program creates an image, allocates wolffd@0: two colors (the first color allocated becomes the background wolffd@0: color), draws a diagonal line (note that 0, 0 is the upper wolffd@0: left corner), writes the image to PNG and JPEG files, and wolffd@0: destroys the image. wolffd@0:

wolffd@0: The above example program should wolffd@0: give you an idea of how the package works. wolffd@0: gd provides many additional functions, which are listed wolffd@0: in the following reference chapters, complete with code wolffd@0: snippets demonstrating each. There is also an wolffd@0: alphabetical index. wolffd@0:

Webpng: a more powerful gd example

wolffd@0: Webpng is a simple utility program to manipulate PNGs from the wolffd@0: command line. It is written for Unix and similar command-line wolffd@0: systems, but should be easily adapted for other environments. wolffd@0: Webpng allows you to set transparency and interlacing and wolffd@0: output interesting information about the PNG in question. wolffd@0:

wolffd@0: webpng.c is provided in the distribution. Unix users can wolffd@0: simply type "make webpng" to compile the program. Type wolffd@0: "webpng" with no arguments to see the available options. wolffd@0:

Function and type reference

wolffd@0: wolffd@0:

Types

wolffd@0:
wolffd@0:
gdImage(TYPE) wolffd@0:
wolffd@0: The data structure in which gd stores images. wolffd@0: gdImageCreate, gdImageCreateTrueColor wolffd@0: and the various image file-loading functions return wolffd@0: a pointer to this type, and the other functions expect to receive wolffd@0: a pointer to this type as their first argument. It is reasonably safe to wolffd@0: examine any of the members of this structure. It is also reasonably wolffd@0: safe to modify individual pixels within the pixels wolffd@0: or tpixels arrays. If the trueColor flag wolffd@0: is set, the tpixels array is valid; otherwise the wolffd@0: pixels array is valid. wolffd@0:

wolffd@0: The colorsTotal, red, green, wolffd@0: blue, alpha and open arrays wolffd@0: manage the palette. They are valid only when the trueColor wolffd@0: flag is not set. wolffd@0: The transparent value contains the palette index of the first wolffd@0: transparent color as read-only information for backwards compatibility; wolffd@0: gd 2.0 stores this information in the alpha array so that wolffd@0: variable transparency can be supported for each palette entry. However, wolffd@0: for truecolor images, transparent represents a single wolffd@0: RGB color which is always 100% transparent, and this wolffd@0: feature is generally supported by browsers which do not support wolffd@0: full alpha channels. wolffd@0:

wolffd@0: typedef struct {
wolffd@0:   /* Palette-based image pixels */
wolffd@0:   unsigned char ** pixels;
wolffd@0:   int sx;
wolffd@0:   int sy;
wolffd@0:   /* These are valid in palette images only. See also
wolffd@0:   /* 'alpha', which appears later in the structure to
wolffd@0:     preserve binary backwards compatibility */
wolffd@0:   int colorsTotal;
wolffd@0:   int red[gdMaxColors];
wolffd@0:   int green[gdMaxColors];
wolffd@0:   int blue[gdMaxColors]; 
wolffd@0:   int open[gdMaxColors];
wolffd@0:   /* For backwards compatibility, this is set to the
wolffd@0:     first palette entry with 100% transparency,
wolffd@0:     and is also set and reset by the 
wolffd@0:     gdImageColorTransparent function. Newer
wolffd@0:     applications can allocate palette entries
wolffd@0:     with any desired level of transparency; however,
wolffd@0:     bear in mind that many viewers, notably
wolffd@0:     many web browsers, fail to implement
wolffd@0:     full alpha channel for PNG and provide
wolffd@0:     support for full opacity or transparency only. */
wolffd@0:   int transparent;
wolffd@0:   int *polyInts;
wolffd@0:   int polyAllocated;
wolffd@0:   struct gdImageStruct *brush;
wolffd@0:   struct gdImageStruct *tile;  
wolffd@0:   int brushColorMap[gdMaxColors];
wolffd@0:   int tileColorMap[gdMaxColors];
wolffd@0:   int styleLength;
wolffd@0:   int stylePos;
wolffd@0:   int *style;
wolffd@0:   int interlace;
wolffd@0:   /* New in 2.0: alpha channel for palettes. Note that only
wolffd@0:     Macintosh Internet Explorer and (possibly) Netscape 6
wolffd@0:     really support multiple levels of transparency in
wolffd@0:     palettes, to my knowledge, as of 2/15/01. Most
wolffd@0:     common browsers will display 100% opaque and
wolffd@0:     100% transparent correctly, and do something 
wolffd@0:     unpredictable and/or undesirable for levels
wolffd@0:     in between. TBB */
wolffd@0:   int alpha[gdMaxColors]; 
wolffd@0:   /* Truecolor flag and pixels. New 2.0 fields appear here at the
wolffd@0:     end to minimize breakage of existing object code. */
wolffd@0:   int trueColor;
wolffd@0:   int ** tpixels;
wolffd@0:   /* Should alpha channel be copied, or applied, each time a
wolffd@0:     pixel is drawn? This applies to truecolor images only.
wolffd@0:     No attempt is made to alpha-blend in palette images,
wolffd@0:     even if semitransparent palette entries exist. 
wolffd@0:     To do that, build your image as a truecolor image,
wolffd@0:     then quantize down to 8 bits. */
wolffd@0:   int alphaBlendingFlag;
wolffd@0:   /* Should the alpha channel of the image be saved? This affects
wolffd@0:     PNG at the moment; other future formats may also
wolffd@0:     have that capability. JPEG doesn't. */
wolffd@0:   int saveAlphaFlag;
wolffd@0: } gdImage;
wolffd@0: 
wolffd@0:

wolffd@0: The order of the structure members may appear confusing, but was chosen wolffd@0: deliberately to increase backwards compatibility with existing gd 1.x-based wolffd@0: binary code that references particular structure members. wolffd@0:

gdImagePtr (TYPE) wolffd@0:
wolffd@0: A pointer to an image structure. gdImageCreate wolffd@0: returns this type, and the other functions expect it as the first wolffd@0: argument. wolffd@0:
gdIOCtx (TYPE) wolffd@0:
wolffd@0: Most of the gd functions that read and write files, such as wolffd@0: gdImagePng and , wolffd@0: also have variants that accept a gdIOCtx structure; see wolffd@0: gdImagePngCtx and wolffd@0: gdImageCreateFromJpegCtx. Those who wish to provide wolffd@0: their own custom routines to read and write images can populate a wolffd@0: gdIOCtx structure with functions of their own devising to wolffd@0: to read and write data. For image reading, the only mandatory wolffd@0: functions are getC and getBuf, which must return the number of wolffd@0: characters actually read, or a negative value on error or EOF. wolffd@0: These functions must read the number of characters requested wolffd@0: unless at the end of the file. For image writing, the only mandatory wolffd@0: functions are putC and putBuf, which return the number of wolffd@0: characters written; these functions must write the number of wolffd@0: characters requested except in the event of an error. The seek wolffd@0: and tell functions are only required in conjunction with the wolffd@0: gd2 file format, which supports quick loading of wolffd@0: partial images. The gd_free function will not be invoked when wolffd@0: calling the standard Ctx functions; it is an implementation wolffd@0: convenience when adding new data types to gd. For examples, wolffd@0: see gd_png.c, gd_gd2.c, gd_jpeg.c, etc., all of which rely wolffd@0: on gdIOCtx to implement the standard image read and write functions. wolffd@0: wolffd@0:
wolffd@0: typedef struct gdIOCtx
wolffd@0: {
wolffd@0:   int (*getC) (struct gdIOCtx *);
wolffd@0:   int (*getBuf) (struct gdIOCtx *, void *, int wanted);
wolffd@0: 
wolffd@0:   void (*putC) (struct gdIOCtx *, int);
wolffd@0:   int (*putBuf) (struct gdIOCtx *, const void *, int wanted);
wolffd@0: 
wolffd@0:   /* seek must return 1 on SUCCESS, 0 on FAILURE. Unlike fseek! */
wolffd@0:   int (*seek) (struct gdIOCtx *, const int);
wolffd@0: 
wolffd@0:   long (*tell) (struct gdIOCtx *);
wolffd@0: 
wolffd@0:   void (*gd_free) (struct gdIOCtx *);
wolffd@0: 
wolffd@0: } gdIOCtx;
wolffd@0: 
wolffd@0: wolffd@0: wolffd@0:
gdFont (TYPE) wolffd@0:
wolffd@0: A font structure. Used to declare the characteristics of a font. wolffd@0: Please see the files gdfontl.c and gdfontl.h for an example of the wolffd@0: proper declaration of this structure. You can provide your wolffd@0: own font data by providing such a structure and the associated wolffd@0: pixel array. You can determine the width and height of a single wolffd@0: character in a font by examining the w and h members of the wolffd@0: structure. If you will not be creating your own fonts, you will wolffd@0: not need to concern yourself with the rest of the components of this wolffd@0: structure. wolffd@0:
wolffd@0: typedef struct {
wolffd@0:   /* # of characters in font */
wolffd@0:   int nchars;
wolffd@0:   /* First character is numbered... (usually 32 = space) */
wolffd@0:   int offset;
wolffd@0:   /* Character width and height */
wolffd@0:   int w;
wolffd@0:   int h;
wolffd@0:   /* Font data; array of characters, one row after another.
wolffd@0:     Easily included in code, also easily loaded from
wolffd@0:     data files. */
wolffd@0:   char *data;
wolffd@0: } gdFont;
wolffd@0: 
wolffd@0:
gdFontPtr (TYPE) wolffd@0:
wolffd@0: A pointer to a font structure. Text-output functions expect these wolffd@0: as their second argument, following the wolffd@0: gdImagePtr argument. Two such pointers are declared in the wolffd@0: provided include files gdfonts.h and gdfontl.h. wolffd@0:
gdPoint (TYPE) wolffd@0:
wolffd@0: Represents a point in the coordinate space of the image; used wolffd@0: by gdImagePolygon, wolffd@0: gdImageOpenPolygon and wolffd@0: gdImageFilledPolygon. wolffd@0:
wolffd@0: typedef struct {
wolffd@0:         int x, y;
wolffd@0: } gdPoint, *gdPointPtr;
wolffd@0: 
wolffd@0:
gdPointPtr (TYPE) wolffd@0:
wolffd@0: A pointer to a gdPoint structure; passed wolffd@0: as an argument to gdImagePolygon, wolffd@0: gdImageOpenPolygon wolffd@0: and gdImageFilledPolygon. wolffd@0:
wolffd@0:
gdFTStringExtra (TYPE) wolffd@0:
wolffd@0: A structure used to pass additional parameters to the wolffd@0: gdImageStringFTEx function. See wolffd@0: gdImageStringFTEx for the wolffd@0: structure definition. wolffd@0:
wolffd@0:
gdFTStringExtraPtr (TYPE) wolffd@0:
wolffd@0: A pointer to a structure used to pass additional parameters to the wolffd@0: gdImageStringFTEx function. See wolffd@0: gdImageStringFTEx for the wolffd@0: structure definition. wolffd@0:
wolffd@0:
gdSource (TYPE) wolffd@0:
wolffd@0:
wolffd@0: typedef struct {
wolffd@0:         int (*source) (void *context, char *buffer, int len);
wolffd@0:         void *context;
wolffd@0: } gdSource, *gdSourcePtr;
wolffd@0: 
wolffd@0: Represents a source from which a PNG can be read. wolffd@0: Programmers who do not wish to read PNGs from a file can provide wolffd@0: their own alternate input mechanism, using the wolffd@0: gdImageCreateFromPngSource function. wolffd@0: See the documentation of that function for an example of the wolffd@0: proper use of this type. wolffd@0:
gdSink (TYPE) wolffd@0:
wolffd@0:
wolffd@0: typedef struct {
wolffd@0:         int (*sink) (void *context, char *buffer, int len);
wolffd@0:         void *context;
wolffd@0: } gdSink, *gdSinkPtr;
wolffd@0: 
wolffd@0: Represents a "sink" (destination) to which a PNG can be written. wolffd@0: Programmers who do not wish to write PNGs to a file can provide wolffd@0: their own alternate output mechanism, using the wolffd@0: gdImagePngToSink function. wolffd@0: See the documentation of that function for an example of the wolffd@0: proper use of this type. wolffd@0:

Image creation, destruction, loading and saving

wolffd@0:
wolffd@0:
gdImageCreate(sx, sy) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCreate is called to create palette-based images, with no wolffd@0: more than 256 colors. Invoke gdImageCreate wolffd@0: with the x and y dimensions of the desired image. gdImageCreate wolffd@0: returns a gdImagePtr to the new image, or wolffd@0: NULL if unable to wolffd@0: allocate the image. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: im = gdImageCreate(64, 64);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdImageCreateTrueColor(sx, sy) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCreateTrueColor is called to create truecolor images, with wolffd@0: an essentially unlimited number of colors. Invoke gdImageCreateTrueColor wolffd@0: with the x and y dimensions of the desired image. gdImageCreateTrueColor wolffd@0: returns a gdImagePtr to the new image, or wolffd@0: NULL if unable to wolffd@0: allocate the image. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:

wolffd@0: Truecolor images are always filled with black at creation time. wolffd@0: There is no concept of a "background" color index. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: im = gdImageCreateTrueColor(64, 64);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdImageCreateFromJpeg(FILE *in) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCreateFromJpegPtr(int size, void *data) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCreateFromJpegCtx(gdIOCtx *in) wolffd@0: (FUNCTION) wolffd@0:

wolffd@0:

wolffd@0: gdImageCreateFromJpeg is called to load truecolor images from JPEG format files. wolffd@0: Invoke gdImageCreateFromJpeg with an already opened pointer to a file wolffd@0: containing the desired image. wolffd@0: gdImageCreateFromJpeg wolffd@0: returns a gdImagePtr to the new wolffd@0: truecolor image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain a JPEG image). gdImageCreateFromJpeg does not wolffd@0: close the file. You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). The wolffd@0: returned image is always a truecolor image. wolffd@0:

wolffd@0: If you already have the wolffd@0: image file in memory, pass the size of the file and a pointer to the wolffd@0: file's data to gdImageCreateFromJpegPtr, which is otherwise identical wolffd@0: to gdImageCreateFromJpeg. wolffd@0:

wolffd@0:

wolffd@0: gdImagePtr im;
wolffd@0: ... inside a function ...
wolffd@0: FILE *in;
wolffd@0: in = fopen("myjpeg.jpg", "rb");
wolffd@0: im = gdImageCreateFromJpeg(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdImageCreateFromPng(FILE *in) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromPngPtr(int size, void *data) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromPngCtx(gdIOCtx *in) wolffd@0: (FUNCTION) wolffd@0:

wolffd@0:

wolffd@0: gdImageCreateFromPng is called to load images from PNG format files. wolffd@0: Invoke gdImageCreateFromPng with an already opened pointer to a file wolffd@0: containing the desired image. wolffd@0: gdImageCreateFromPng wolffd@0: returns a gdImagePtr to the new image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain a PNG image). gdImageCreateFromPng does not wolffd@0: close the file. You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:

wolffd@0: If you already have the wolffd@0: image file in memory, pass the size of the file and a pointer to the wolffd@0: file's data to gdImageCreateFromPngPtr, which is otherwise identical wolffd@0: to gdImageCreateFromPng. wolffd@0:

wolffd@0: If the PNG image being loaded is a truecolor image, the resulting wolffd@0: gdImagePtr will refer to a truecolor image. If the PNG image wolffd@0: being loaded is a palette or grayscale image, the resulting wolffd@0: gdImagePtr will refer to a palette image. gd retains only 8 bits wolffd@0: of resolution for each of the red, green and blue channels, and wolffd@0: only 7 bits of resolution for the alpha channel. The former wolffd@0: restriction affects only a handful of very rare 48-bit color wolffd@0: and 16-bit grayscale PNG images. The second restriction affects wolffd@0: all semitransparent PNG images, but the difference is essentially wolffd@0: invisible to the eye. 7 bits of alpha channel resolution is, wolffd@0: in practice, quite a lot. wolffd@0:

wolffd@0: gdImagePtr im;
wolffd@0: ... inside a function ...
wolffd@0: FILE *in;
wolffd@0: in = fopen("mypng.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdImageCreateFromPngSource(gdSourcePtr in) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Deprecated in favor of wolffd@0: gdImageCreateFromPngCtx. Should wolffd@0: not be used in new applications. wolffd@0:

wolffd@0: gdImageCreateFromPngSource is called to load a PNG from wolffd@0: a data source other than a file. Usage is very similar to wolffd@0: the gdImageCreateFromPng function, wolffd@0: except that the programmer provides a custom data source. wolffd@0:

wolffd@0: The programmer must write an input function which accepts wolffd@0: a context pointer, a buffer, and a number of bytes to be wolffd@0: read as arguments. This function must read the number of wolffd@0: bytes requested, unless the end of the file has been reached, wolffd@0: in which case the function should return zero, or an error wolffd@0: has occurred, in which case the function should return wolffd@0: -1. The programmer then creates a wolffd@0: gdSource structure and sets wolffd@0: the source pointer to the input function and wolffd@0: the context pointer to any value which is useful to the wolffd@0: programmer. wolffd@0:

wolffd@0: The example below wolffd@0: implements gdImageCreateFromPng wolffd@0: by creating a custom data source and invoking gdImageCreateFromPngSource. wolffd@0:

wolffd@0: static int freadWrapper(void *context, char *buf, int len);
wolffd@0: 
wolffd@0: gdImagePtr gdImageCreateFromPng(FILE *in)
wolffd@0: {
wolffd@0:         gdSource s;
wolffd@0:         s.source = freadWrapper;
wolffd@0:         s.context = in;
wolffd@0:         return gdImageCreateFromPngSource(&s);
wolffd@0: }
wolffd@0: 
wolffd@0: static int freadWrapper(void *context, char *buf, int len)
wolffd@0: {
wolffd@0:         int got = fread(buf, 1, len, (FILE *) context);
wolffd@0:         return got;
wolffd@0: }
wolffd@0: 
wolffd@0:
gdImageCreateFromGif(FILE *in) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGifPtr(int size, void *data) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGifCtx(gdIOCtx *in) wolffd@0: (FUNCTION) wolffd@0:

wolffd@0:

wolffd@0: gdImageCreateFromGif is called to load images from GIF format files. wolffd@0: Invoke gdImageCreateFromGif with an already opened pointer to a file wolffd@0: containing the desired image. wolffd@0: gdImageCreateFromGif wolffd@0: returns a gdImagePtr to the new image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain a GIF image). gdImageCreateFromGif does not wolffd@0: close the file. You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:

wolffd@0: If you already have the wolffd@0: image file in memory, pass the size of the file and a pointer to the wolffd@0: file's data to gdImageCreateFromGifPtr, which is otherwise identical wolffd@0: to gdImageCreateFromGif. wolffd@0:

wolffd@0: gdImagePtr im;
wolffd@0: ... inside a function ...
wolffd@0: FILE *in;
wolffd@0: in = fopen("mygif.gif", "rb");
wolffd@0: im = gdImageCreateFromGif(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdImageCreateFromGd(FILE *in) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGdPtr(int size, void *data) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGdCtx(gdIOCtx *in) wolffd@0: (FUNCTION) wolffd@0:

wolffd@0:

wolffd@0: gdImageCreateFromGd is called to load images from gd format files. wolffd@0: Invoke gdImageCreateFromGd wolffd@0: with an already opened pointer to a file containing the desired image wolffd@0: in the gd file format, which is specific to wolffd@0: gd and intended for very fast loading. (It is not intended for wolffd@0: compression; for compression, use PNG or JPEG.) wolffd@0:

wolffd@0: If you already have the wolffd@0: image file in memory, pass the size of the file and a pointer to the wolffd@0: file's data to gdImageCreateFromGdPtr, which is otherwise identical wolffd@0: to gdImageCreateFromGd. wolffd@0:

wolffd@0: gdImageCreateFromGd wolffd@0: returns a gdImagePtr to the new image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain a gd format image). gdImageCreateFromGd does not wolffd@0: close the file. You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: FILE *in;
wolffd@0: in = fopen("mygd.gd", "rb");
wolffd@0: im = gdImageCreateFromGd(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: wolffd@0:
gdImageCreateFromGd2(FILE *in) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGd2Ptr(int size, void *data) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGd2Ctx(gdIOCtx *in) wolffd@0: (FUNCTION) wolffd@0:

wolffd@0: wolffd@0:

wolffd@0: gdImageCreateFromGd2 is called to load images from gd2 format files. wolffd@0: Invoke gdImageCreateFromGd2 wolffd@0: with an already opened pointer to a file containing the desired image wolffd@0: in the gd2 file format, which is specific to wolffd@0: gd2 and intended for fast loading of parts of large images. wolffd@0: (It is a compressed format, but generally not as good as maximum wolffd@0: compression of the entire image would be.) wolffd@0:

wolffd@0: If you already have the wolffd@0: image file in memory, pass the size of the file and a pointer to the wolffd@0: file's data to gdImageCreateFromGd2Ptr, which is otherwise identical wolffd@0: to gdImageCreateFromGd2. wolffd@0:

wolffd@0: gdImageCreateFromGd2 wolffd@0: returns a gdImagePtr to the new image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain a gd format image). gdImageCreateFromGd2 does not wolffd@0: close the file. You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: FILE *in;
wolffd@0: in = fopen("mygd.gd2", "rb");
wolffd@0: im = gdImageCreateFromGd2(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: wolffd@0:
gdImageCreateFromGd2Part(FILE *in, int srcX, int srcY, int w, int h) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGd2PartPtr(int size, void *data, int srcX, int srcY, int w, int h) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromGd2PartCtx(gdIOCtx *in) wolffd@0: (FUNCTION) wolffd@0:

wolffd@0: wolffd@0:

wolffd@0: gdImageCreateFromGd2Part is called to load parts of images from gd2 format files. wolffd@0: Invoked in the same way as gdImageCreateFromGd2, wolffd@0: but with extra parameters wolffd@0: indicating the source (x, y) and width/height of the desired image. wolffd@0: gdImageCreateFromGd2Part returns a gdImagePtr to the wolffd@0: new image, or NULL if unable to load the image. wolffd@0: The image must eventually be destroyed using gdImageDestroy(). wolffd@0:

wolffd@0: If you already have the image file in memory, you may use wolffd@0: gdImageCreateFromGd2PartPtr. Pass the size of the image file, wolffd@0: in bytes, as the first argument and the pointer to the image file data wolffd@0: as the second argument. wolffd@0:

wolffd@0:

gdImageCreateFromWBMP(FILE *in) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromWBMPPtr(int size, void *data) wolffd@0: (FUNCTION) wolffd@0:
gdImageCreateFromWBMPCtx(gdIOCtx *in) wolffd@0: (FUNCTION) wolffd@0:

wolffd@0:

wolffd@0: gdImageCreateFromWBMP is called to load images from WBMP format files. wolffd@0: Invoke gdImageCreateFromWBMP with an already opened pointer to a file wolffd@0: containing the desired image. wolffd@0: gdImageCreateFromWBMP wolffd@0: returns a gdImagePtr to the new image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain a PNG image). gdImageCreateFromWBMP does not wolffd@0: close the file. You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:

wolffd@0: If you already have the wolffd@0: image file in memory, pass the size of the file and a pointer to the wolffd@0: file's data to gdImageCreateFromWBMPPtr, which is otherwise identical wolffd@0: to gdImageCreateFromWBMP. wolffd@0:

wolffd@0: gdImagePtr im;
wolffd@0: ... inside a function ...
wolffd@0: FILE *in;
wolffd@0: in = fopen("mywbmp.wbmp", "rb");
wolffd@0: im = gdImageCreateFromWBMP(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:

wolffd@0:

gdImageCreateFromXbm(FILE *in) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCreateFromXbm is called to load images from X bitmap format wolffd@0: files. Invoke gdImageCreateFromXbm wolffd@0: with an already opened pointer to a file containing the desired image. wolffd@0: gdImageCreateFromXbm wolffd@0: returns a gdImagePtr to the new image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain an X bitmap format image). gdImageCreateFromXbm does wolffd@0: not close the file. You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: FILE *in;
wolffd@0: in = fopen("myxbm.xbm", "rb");
wolffd@0: im = gdImageCreateFromXbm(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdImageCreateFromXpm(char *filename) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCreateFromXbm is called to load images from XPM X Window System wolffd@0: color bitmap format files. This function is available only if HAVE_XPM wolffd@0: is selected in the Makefile and the Xpm library is linked with the wolffd@0: application. Unlike most gd file functions, the Xpm functions require wolffd@0: filenames, not file pointers. wolffd@0: gdImageCreateFromXpm wolffd@0: returns a gdImagePtr to the new image, or NULL wolffd@0: if unable to load the image (most often because the file is corrupt or wolffd@0: does not contain an XPM bitmap format image). You can inspect the sx and sy members of the wolffd@0: image to determine its size. The image must eventually be destroyed wolffd@0: using gdImageDestroy(). wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: FILE *in;
wolffd@0: in = fopen("myxpm.xpm", "rb");
wolffd@0: im = gdImageCreateFromXpm(in);
wolffd@0: fclose(in);
wolffd@0: /* ... Use the image ... */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdImageDestroy(gdImagePtr im) (FUNCTION) wolffd@0:
gdImageDestroy is used to free the memory associated with wolffd@0: an image. It is important to invoke gdImageDestroy before wolffd@0: exiting your program or assigning a new image to wolffd@0: a gdImagePtr variable. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: im = gdImageCreate(10, 10);
wolffd@0: /* ... Use the image ... */
wolffd@0: /* Now destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageJpeg(gdImagePtr im, FILE *out, int quality) wolffd@0: (FUNCTION)
wolffd@0: void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality) wolffd@0: (FUNCTION)
wolffd@0:
wolffd@0: gdImageJpeg outputs the specified image to the specified wolffd@0: file in JPEG format. The file must be open for writing. Under MSDOS wolffd@0: and all versions of Windows, it is important to use "wb" as opposed wolffd@0: to simply "w" as the mode when opening the file, and under Unix there wolffd@0: is no penalty for doing so. gdImageJpeg does not wolffd@0: close the file; your code must do so. wolffd@0:

wolffd@0: If quality is negative, the default IJG JPEG quality value (which wolffd@0: should yield a good general quality / size tradeoff for most wolffd@0: situations) is used. Otherwise, for practical purposes, quality wolffd@0: should be a value in the range 0-95, higher quality values usually wolffd@0: implying both higher quality and larger image sizes. wolffd@0:

wolffd@0: If you have set image interlacing using wolffd@0: gdImageInterlace, this function will wolffd@0: interpret that to mean you wish to output a progressive JPEG. Some wolffd@0: programs (e.g., Web browsers) can display progressive JPEGs wolffd@0: incrementally; this can be useful when browsing over a relatively slow wolffd@0: communications link, for example. Progressive JPEGs can also be wolffd@0: slightly smaller than sequential (non-progressive) JPEGs. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black, white;
wolffd@0: FILE *out;
wolffd@0: /* Create the image */
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Allocate background */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: /* Allocate drawing color */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);
wolffd@0: /* Draw rectangle */
wolffd@0: gdImageRectangle(im, 0, 0, 99, 99, black);
wolffd@0: /* Open output file in binary mode */
wolffd@0: out = fopen("rect.jpg", "wb");
wolffd@0: /* Write JPEG using default quality */
wolffd@0: gdImageJpeg(im, out, -1);
wolffd@0: /* Close file */
wolffd@0: fclose(out);
wolffd@0: /* Destroy image */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void* gdImageJpegPtr(gdImagePtr im, int *size, int quality) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImageJpeg except that it returns a pointer to a memory wolffd@0: area with the JPEG data. This memory must be freed by the caller when it is wolffd@0: no longer needed. The caller must invoke gdFree(), not free(), wolffd@0: unless the caller is absolutely certain that the same implementations of wolffd@0: malloc, free, etc. are used both at library build time and at application wolffd@0: build time. The 'size' parameter receives the total size of the block wolffd@0: of memory. wolffd@0:
wolffd@0: void gdImageGif(gdImagePtr im, FILE *out) wolffd@0:
wolffd@0: wolffd@0: void gdImageGifCtx(gdImagePtr im, gdIOCtx *out) wolffd@0: wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageGif outputs the specified image to the specified wolffd@0: file in GIF format. The file must be open for writing. Under MSDOS wolffd@0: and all versions of Windows, it is important to use "wb" as opposed wolffd@0: to simply "w" as the mode when opening the file, and under Unix there wolffd@0: is no penalty for doing so. gdImageGif does not wolffd@0: close the file; your code must do so. wolffd@0:

wolffd@0: GIF does not support true color; GIF images can contain a maximum wolffd@0: of 256 colors. If the image to be written is a wolffd@0: truecolor image, such as those created with wolffd@0: gdImageCreateTrueColor or loaded wolffd@0: from a JPEG or a truecolor PNG image file, a palette-based wolffd@0: temporary image will automatically be created internally using the wolffd@0: gdImageCreatePaletteFromTrueColor function. The original image pixels are not modified. This conversion wolffd@0: produces high quality palettes but does require some CPU time. If you are wolffd@0: regularly converting truecolor to palette in this way, you should consider wolffd@0: creating your image as a palette-based image in the first place. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black, white;
wolffd@0: FILE *out;
wolffd@0: /* Create the image */
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Allocate background */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: /* Allocate drawing color */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);
wolffd@0: /* Draw rectangle */
wolffd@0: gdImageRectangle(im, 0, 0, 99, 99, black);
wolffd@0: /* Open output file in binary mode */
wolffd@0: out = fopen("rect.gif", "wb");
wolffd@0: /* Write GIF */
wolffd@0: gdImageGif(im, out);
wolffd@0: /* Close file */
wolffd@0: fclose(out);
wolffd@0: /* Destroy image */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void* gdImageGifPtr(gdImagePtr im, int *size) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImageGif except that it returns a pointer to a memory wolffd@0: area with the GIF data. This memory must be freed by the caller when it is wolffd@0: no longer needed. The caller must invoke gdFree(), not free(), wolffd@0: unless the caller is absolutely certain that the same implementations of wolffd@0: malloc, free, etc. are used both at library build time and at application wolffd@0: build time. The 'size' parameter receives the total size of the block wolffd@0: of memory. wolffd@0: wolffd@0:
wolffd@0: void gdImageGifAnimBegin(gdImagePtr im, FILE *out, int GlobalCM, int Loops) wolffd@0:
wolffd@0: wolffd@0: void gdImageGifAnimBeginCtx(gdImagePtr im, gdIOCtx *out, int GlobalCM, int Loops) wolffd@0: wolffd@0: (FUNCTION) wolffd@0: wolffd@0:
This function must be called as the first function when creating a wolffd@0: GIF animation. It writes the correct GIF file headers to selected wolffd@0: file output, and prepares for frames to be added for the animation. wolffd@0: The image argument is not used to produce an image frame to the file, wolffd@0: it is only used to establish the GIF animation frame size, interlacing wolffd@0: options and the color palette. gdImageGifAnimAdd is used to wolffd@0: add the first and subsequent frames to the animation, and the animation wolffd@0: must be terminated by writing a semicolon character (;) to it or by using wolffd@0: gdImageGifAnimEnd to do that. wolffd@0:

wolffd@0: wolffd@0: The GlobalCM flag indicates if a global color map (or palette) is used wolffd@0: in the GIF89A header. A nonzero value specifies that a global color wolffd@0: map should be used to reduce the size of the animation. wolffd@0: Of course, if the color maps of wolffd@0: individual frames differ greatly, a global color map may not be a good idea. wolffd@0: GlobalCM=1 means write global color map, GlobalCM=0 means do not, and wolffd@0: GlobalCM=-1 means to do the default, which currently is to use a global wolffd@0: color map. wolffd@0: wolffd@0:

wolffd@0: wolffd@0: If Loops is 0 or greater, the Netscape 2.0 extension for animation wolffd@0: loop count is written. 0 means infinite loop count. -1 means that wolffd@0: the extension is not added which results in no looping. -1 is the wolffd@0: default. wolffd@0: wolffd@0:

wolffd@0: void* gdImageGifAnimBeginPtr(gdImagePtr im, int *size, int GlobalCM, int Loops) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImageGifAnimBegin except that it returns a pointer wolffd@0: to a memory area with the GIF data. This memory must be freed by the wolffd@0: caller when it is no longer needed. The caller must invoke wolffd@0: gdFree(), not free(), unless the caller is absolutely certain that the wolffd@0: same implementations of malloc, free, etc. are used both at library wolffd@0: build time and at application build time. The 'size' wolffd@0: parameter receives the total size of the block of memory. wolffd@0: wolffd@0:
wolffd@0: void gdImageGifAnimAdd(gdImagePtr im, FILE *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) wolffd@0:
wolffd@0: wolffd@0: void gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtx *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) wolffd@0: wolffd@0: (FUNCTION) wolffd@0: wolffd@0:
This function writes GIF animation frames to GIF animation, which wolffd@0: was initialized with gdImageGifAnimBegin. With LeftOfs and wolffd@0: TopOfs you can place this frame in different offset than (0,0) inside wolffd@0: the image screen as defined in gdImageGifAnimBegin. Delay between the wolffd@0: previous frame and this frame is in 1/100s units. Disposal is usually wolffd@0: gdDisposalNone, meaning that the pixels changed by this wolffd@0: frame should remain on the display when the next frame begins to render, but wolffd@0: can also be gdDisposalUnknown (not recommended), wolffd@0: gdDisposalRestoreBackground (restores the first wolffd@0: allocated color of the global palette), or wolffd@0: gdDisposalRestorePrevious (restores the appearance of the wolffd@0: affected area before the frame was rendered). Only wolffd@0: gdDisposalNone is a sensible choice for the first frame. wolffd@0: If previm is wolffd@0: passed, the built-in GIF optimizer will always use gdDisposalNone wolffd@0: regardless of the Disposal parameter. wolffd@0:

wolffd@0: Setting the LocalCM flag to 1 adds a local palette for this image to the wolffd@0: animation. Otherwise the global palette is assumed and the user must make wolffd@0: sure the palettes match. Use gdImagePaletteCopy to do that. wolffd@0: wolffd@0:

wolffd@0: wolffd@0: Automatic optimization is activated by giving the previous image as a wolffd@0: parameter. This function then compares the images and only writes the changed wolffd@0: pixels to the new frame in animation. The Disposal parameter for wolffd@0: optimized animations must be set to 1, also for the first frame. wolffd@0: LeftOfs and TopOfs parameters are ignored for optimized frames. To wolffd@0: achieve good optimization, it is usually best to use a single global wolffd@0: color map. To allow gdImageGifAnimAdd to compress unchanged pixels via wolffd@0: the use of a transparent color, the image must include a transparent color. wolffd@0: wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im, im2, im3;
wolffd@0: int black, white, trans;
wolffd@0: FILE *out;
wolffd@0: /* Create the image */
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Allocate background */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: /* Allocate drawing color */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);
wolffd@0: /* Allocate transparent color for animation compression */
wolffd@0: trans = gdImageColorAllocate(im, 1, 1, 1);
wolffd@0: /* Draw rectangle */
wolffd@0: gdImageRectangle(im, 0, 0, 10, 10, black);
wolffd@0: /* Open output file in binary mode */
wolffd@0: out = fopen("anim.gif", "wb");
wolffd@0: /* Write GIF header.  Use global color map.  Loop a few times */
wolffd@0: gdImageGifAnimBegin(im, out, 1, 3);
wolffd@0: /* Write the first frame.  No local color map.  Delay = 1s */
wolffd@0: gdImageGifAnimAdd(im, out, 0, 0, 0, 100, 1, NULL);
wolffd@0: /* construct the second frame */
wolffd@0: im2 = gdImageCreate(100, 100);
wolffd@0: /* Allocate background to make it white */
wolffd@0: (void)gdImageColorAllocate(im2, 255, 255, 255);
wolffd@0: /* Make sure the palette is identical */
wolffd@0: gdImagePaletteCopy (im2, im);
wolffd@0: /* Draw something */
wolffd@0: gdImageRectangle(im2, 0, 0, 15, 15, black);
wolffd@0: /* Allow animation compression with transparent pixels */
wolffd@0: gdImageColorTransparent (im2, trans);
wolffd@0: /* Add the second frame */
wolffd@0: gdImageGifAnimAdd(im2, out, 0, 0, 0, 100, 1, im);
wolffd@0: /* construct the second frame */
wolffd@0: im3 = gdImageCreate(100, 100);
wolffd@0: /* Allocate background to make it white */
wolffd@0: (void)gdImageColorAllocate(im3, 255, 255, 255);
wolffd@0: /* Make sure the palette is identical */
wolffd@0: gdImagePaletteCopy (im3, im);
wolffd@0: /* Draw something */
wolffd@0: gdImageRectangle(im3, 0, 0, 15, 20, black);
wolffd@0: /* Allow animation compression with transparent pixels */
wolffd@0: gdImageColorTransparent (im3, trans);
wolffd@0: /* Add the third frame, compressing against the second one */
wolffd@0: gdImageGifAnimAdd(im3, out, 0, 0, 0, 100, 1, im2);
wolffd@0: /* Write the end marker */
wolffd@0: /* gdImageGifAnimEnd(out); is the same as the following: */
wolffd@0: putc (';', out);
wolffd@0: /* Close file */
wolffd@0: fclose(out);
wolffd@0: /* Destroy images */
wolffd@0: gdImageDestroy(im);
wolffd@0: gdImageDestroy(im2);
wolffd@0: gdImageDestroy(im3);
wolffd@0: 
wolffd@0: wolffd@0:
wolffd@0: void* gdImageGifAnimAddPtr(gdImagePtr im, int *size, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImageGifAnimAdd except that it returns a pointer wolffd@0: to a memory area with the GIF data. This memory must be freed by the wolffd@0: caller when it is no longer needed. The caller must invoke wolffd@0: gdFree(), not free(), unless the caller is absolutely certain that the wolffd@0: same implementations of malloc, free, etc. are used both at library wolffd@0: build time and at application build time. The 'size' wolffd@0: parameter receives the total size of the block of memory. wolffd@0: wolffd@0:
wolffd@0: void gdImageGifAnimEnd(FILE *out) wolffd@0:
wolffd@0: wolffd@0: void gdImageGifAnimEndCtx(gdIOCtx *out) wolffd@0: wolffd@0: (FUNCTION) wolffd@0: wolffd@0:
Writes semicolon character (;) to the output file. This wolffd@0: terminates the GIF file properly. You can omit the call to wolffd@0: gdImageGifAnimEnd and just print out the semicolon. wolffd@0: wolffd@0:
wolffd@0: void* gdImageGifAnimEndPtr(int *size) wolffd@0: (FUNCTION) wolffd@0: wolffd@0:
Returns a one byte string containing the semicolon character (;). wolffd@0: Returns a pointer to a memory area with that string. This memory must wolffd@0: be freed by the caller when it is no longer needed. The caller wolffd@0: must invoke gdFree(), not free(), unless the caller is absolutely wolffd@0: certain that the same implementations of malloc, free, etc. are used wolffd@0: both at library build time and at application build time. The wolffd@0: 'size' parameter receives the total size of the block of memory. The wolffd@0: string ";" can be used in place of this function. wolffd@0: wolffd@0:
wolffd@0: void gdImagePng(gdImagePtr im, FILE *out) wolffd@0:
wolffd@0: wolffd@0: void gdImagePngCtx(gdImagePtr im, gdIOCtx *out) wolffd@0: wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImagePng outputs the specified image to the specified wolffd@0: file in PNG format. The file must be open for writing. Under MSDOS wolffd@0: and all versions of Windows, it is important to use "wb" as opposed wolffd@0: to simply "w" as the mode when opening the file, and under Unix there wolffd@0: is no penalty for doing so. gdImagePng does not wolffd@0: close the file; your code must do so. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black, white;
wolffd@0: FILE *out;
wolffd@0: /* Create the image */
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Allocate background */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: /* Allocate drawing color */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);
wolffd@0: /* Draw rectangle */
wolffd@0: gdImageRectangle(im, 0, 0, 99, 99, black);
wolffd@0: /* Open output file in binary mode */
wolffd@0: out = fopen("rect.png", "wb");
wolffd@0: /* Write PNG */
wolffd@0: gdImagePng(im, out);
wolffd@0: /* Close file */
wolffd@0: fclose(out);
wolffd@0: /* Destroy image */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImagePngEx(gdImagePtr im, FILE *out, int level) wolffd@0:
wolffd@0: wolffd@0: void gdImagePngCtxEx(gdImagePtr im, gdIOCtx *out, int level) wolffd@0: wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Like gdImagePng, gdImagePngEx outputs the wolffd@0: specified image to the specified file in PNG format. In addition, wolffd@0: gdImagePngEx allows the level of compression to be specified. A compression wolffd@0: level of 0 means "no compression." A compression level of 1 means wolffd@0: "compressed, but as quickly as possible." A compression level of 9 wolffd@0: means "compressed as much as possible to produce the smallest possible wolffd@0: file." A compression level of -1 will use the default compression level wolffd@0: at the time zlib was compiled on your system. wolffd@0:

wolffd@0: For more information, see gdImagePng. wolffd@0:

wolffd@0: void* gdImagePngPtr(gdImagePtr im, int *size) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImagePng except that it returns a pointer to a memory wolffd@0: area with the PNG data. This memory must be freed by the caller when it is wolffd@0: no longer needed. The caller must invoke gdFree(), not free(), wolffd@0: unless the caller is absolutely certain that the same implementations of wolffd@0: malloc, free, etc. are used both at library build time and at application wolffd@0: build time. The 'size' parameter receives the total size of the block wolffd@0: of memory. wolffd@0:
wolffd@0: void* gdImagePngPtrEx(gdImagePtr im, int *size, int level) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Like gdImagePngPtr, gdImagePngPtrEx returns a wolffd@0: pointer to a PNG image in allocated memory. wolffd@0: In addition, gdImagePngPtrEx allows the level of compression to be wolffd@0: specified. A compression level of 0 means "no compression." A compression level of 1 means wolffd@0: "compressed, but as quickly as possible." A compression level of 9 wolffd@0: means "compressed as much as possible to produce the smallest possible wolffd@0: file." A compression level of -1 will use the default compression level wolffd@0: at the time zlib was compiled on your system. wolffd@0:

wolffd@0: For more information, see gdImagePngPtr. wolffd@0:

gdImagePngToSink(gdImagePtr im, gdSinkPtr out) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImagePngToSink is called to write a PNG to wolffd@0: a data "sink" (destination) other than a file. Usage is very similar to wolffd@0: the gdImagePng function, wolffd@0: except that the programmer provides a custom data sink. wolffd@0:

wolffd@0: The programmer must write an output function which accepts wolffd@0: a context pointer, a buffer, and a number of bytes to be wolffd@0: written as arguments. This function must write the number of wolffd@0: bytes requested and return that number, unless an error wolffd@0: has occurred, in which case the function should return wolffd@0: -1. The programmer then creates a wolffd@0: gdSink structure and sets wolffd@0: the sink pointer to the output function and wolffd@0: the context pointer to any value which is useful to the wolffd@0: programmer. wolffd@0:

wolffd@0: The example below wolffd@0: implements gdImagePng wolffd@0: by creating a custom data source and invoking gdImagePngFromSink. wolffd@0:

wolffd@0: static int stdioSink(void *context, char *buffer, int len)
wolffd@0: {
wolffd@0:   return fwrite(buffer, 1, len, (FILE *) context);
wolffd@0: }
wolffd@0: 
wolffd@0: void gdImagePng(gdImagePtr im, FILE *out)
wolffd@0: {
wolffd@0:   gdSink mySink;
wolffd@0:   mySink.context = (void *) out;
wolffd@0:   mySink.sink = stdioSink;
wolffd@0:   gdImagePngToSink(im, &mySink);
wolffd@0: }
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageWBMP(gdImagePtr im, int fg, FILE *out) wolffd@0:
gdImageWBMPCtx(gdIOCtx *out) wolffd@0: (FUNCTION)(FUNCTION) wolffd@0:
wolffd@0: gdImageWBMP outputs the specified image to the specified wolffd@0: file in WBMP format. The file must be open for writing. Under MSDOS wolffd@0: and all versions of Windows, it is important to use "wb" as opposed wolffd@0: to simply "w" as the mode when opening the file, and under Unix there wolffd@0: is no penalty for doing so. gdImageWBMP does not wolffd@0: close the file; your code must do so. wolffd@0:

wolffd@0: WBMP file support is black and white only. The color index wolffd@0: specified by the fg argument is the "foreground," and only pixels wolffd@0: of this color will be set in the WBMP file. All other pixels wolffd@0: will be considered "background." wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black, white;
wolffd@0: FILE *out;
wolffd@0: /* Create the image */
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Allocate background */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: /* Allocate drawing color */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);
wolffd@0: /* Draw rectangle */
wolffd@0: gdImageRectangle(im, 0, 0, 99, 99, black);
wolffd@0: /* Open output file in binary mode */
wolffd@0: out = fopen("rect.wbmp", "wb");
wolffd@0: /* Write WBMP, with black as foreground */
wolffd@0: gdImageWBMP(im, black, out);
wolffd@0: /* Close file */
wolffd@0: fclose(out);
wolffd@0: /* Destroy image */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void* gdImageWBMPPtr(gdImagePtr im, int *size) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImageWBMP except that it returns a pointer to a memory wolffd@0: area with the WBMP data. This memory must be freed by the caller when it is wolffd@0: no longer needed. The caller must invoke gdFree(), not free(), wolffd@0: unless the caller is absolutely certain that the same implementations of wolffd@0: malloc, free, etc. are used both at library build time and at application wolffd@0: build time. The 'size' parameter receives the total size of the block wolffd@0: of memory. wolffd@0:
wolffd@0: void gdImageGd(gdImagePtr im, FILE *out) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageGd outputs the specified image to the specified wolffd@0: file in the gd image format. The file must wolffd@0: be open for writing. Under MSDOS and all versions of Windows, it is wolffd@0: important to use "wb" as wolffd@0: opposed to simply "w" as the mode when opening the file, and under wolffd@0: Unix there is no penalty for doing so. gdImagePng does not wolffd@0: close the file; your code must do so. wolffd@0:

wolffd@0: The gd image format is intended for fast reads and writes of wolffd@0: images your program will need frequently to build other wolffd@0: images. It is not a compressed format, and is not intended wolffd@0: for general use. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black, white;
wolffd@0: FILE *out;
wolffd@0: /* Create the image */
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Allocate background */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: /* Allocate drawing color */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);
wolffd@0: /* Draw rectangle */
wolffd@0: gdImageRectangle(im, 0, 0, 99, 99, black);
wolffd@0: /* Open output file in binary mode */
wolffd@0: out = fopen("rect.gd", "wb");
wolffd@0: /* Write gd format file */
wolffd@0: gdImageGd(im, out);
wolffd@0: /* Close file */
wolffd@0: fclose(out);
wolffd@0: /* Destroy image */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: wolffd@0:
wolffd@0: void* gdImageGdPtr(gdImagePtr im, int *size) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImageGd except that it returns a pointer to a memory wolffd@0: area with the GD data. This memory must be freed by the caller when it is wolffd@0: no longer needed. The caller must invoke gdFree(), not free(), wolffd@0: unless the caller is absolutely certain that the same implementations of wolffd@0: malloc, free, etc. are used both at library build time and at application wolffd@0: build time. The 'size' parameter receives the total size of the block wolffd@0: of memory. wolffd@0: wolffd@0:
wolffd@0: void gdImageGd2(gdImagePtr im, FILE *out, int chunkSize, int fmt) wolffd@0:
wolffd@0: wolffd@0: void gdImageGd2Ctx(gdImagePtr im, gdIOCtx *out, int chunkSize, int fmt) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageGd2 outputs the specified image to the specified wolffd@0: file in the gd2 image format. The file must wolffd@0: be open for writing. Under MSDOS and all versions of Windows, it is wolffd@0: important to use "wb" as wolffd@0: opposed to simply "w" as the mode when opening the file, and under wolffd@0: Unix there is no penalty for doing so. gdImageGd2 does not wolffd@0: close the file; your code must do so. wolffd@0:

wolffd@0: The gd2 image format is intended for fast reads and writes of wolffd@0: parts of images. wolffd@0: It is a compressed format, and well suited to retrieving smll sections of wolffd@0: much larger images. wolffd@0: wolffd@0: The third and fourth parameters are the 'chunk size' and format resposectively. wolffd@0:

wolffd@0: The file is stored as a series of compressed subimages, and the wolffd@0: Chunk Size determines the sub-image size - a value of wolffd@0: zero causes the GD library to use the default. wolffd@0:

wolffd@0: It is also possible to store GD2 files in an uncompressed format, in which case the wolffd@0: fourth parameter should be GD2_FMT_RAW. wolffd@0: wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black, white;
wolffd@0: FILE *out;
wolffd@0: /* Create the image */
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Allocate background */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: /* Allocate drawing color */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);
wolffd@0: /* Draw rectangle */
wolffd@0: gdImageRectangle(im, 0, 0, 99, 99, black);
wolffd@0: /* Open output file in binary mode */
wolffd@0: out = fopen("rect.gd", "wb");
wolffd@0: /* Write gd2 format file */
wolffd@0: gdImageGd2(im, out, 0, GD2_FMT_COMPRESSED);
wolffd@0: /* Close file */
wolffd@0: fclose(out);
wolffd@0: /* Destroy image */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: wolffd@0:
wolffd@0: void* gdImageGd2Ptr(gdImagePtr im, int chunkSize, int fmt, int *size) wolffd@0: (FUNCTION) wolffd@0:
Identical to gdImageGd2 except that it returns a pointer to a memory wolffd@0: area with the GD2 data. This memory must be freed by the caller when it is wolffd@0: no longer needed. The caller must invoke gdFree(), not free(), wolffd@0: unless the caller is absolutely certain that the same implementations of wolffd@0: malloc, free, etc. are used both at library build time and at application wolffd@0: build time. The 'size' parameter receives the total size of the block wolffd@0: of memory. wolffd@0:
wolffd@0: void gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted) wolffd@0:
wolffd@0: wolffd@0: gdImagePtr gdImageCreatePaletteFromTrueColor(gdImagePtr im, int ditherFlag, int colorsWanted) wolffd@0: wolffd@0: (FUNCTION) wolffd@0:
wolffd@0:
wolffd@0: gdImageCreatePaletteFromTrueColor returns a new wolffd@0: image. gdImageTrueColorToPalette permanently converts the wolffd@0: existing image. The two functions are otherwise identical. wolffd@0:
wolffd@0:

wolffd@0: The function converts a truecolor image to a palette-based image, wolffd@0: using a high-quality two-pass quantization routine. wolffd@0: If ditherFlag is set, the image will be wolffd@0: dithered to approximate colors better, at the expense wolffd@0: of some obvious "speckling." colorsWanted can be wolffd@0: anything up to 256. If the original source image wolffd@0: includes photographic information or anything that wolffd@0: came out of a JPEG, 256 is strongly recommended. wolffd@0: 100% transparency of a single transparent color in the wolffd@0: original truecolor image will be preserved. There is no other wolffd@0: support for preservation of alpha channel or transparency in wolffd@0: the destination image. wolffd@0:

wolffd@0: For best results, don't use this function -- write real wolffd@0: truecolor PNGs and JPEGs. The disk space gain of wolffd@0: conversion to palette is not great (for small images wolffd@0: it can be negative) and the quality loss is ugly. However, wolffd@0: the version of this function included in version 2.0.12 and later does wolffd@0: do a better job than the version included prior to 2.0.12. wolffd@0:

wolffd@0:

Drawing Functions

wolffd@0:
wolffd@0:
void gdImageSetPixel(gdImagePtr im, int x, int y, int color) (FUNCTION) wolffd@0:
gdImageSetPixel sets a pixel to a particular color index. Always use wolffd@0: this function or one of the other drawing functions to access pixels; wolffd@0: do not access the pixels of the gdImage structure wolffd@0: directly. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Set a pixel near the center. */
wolffd@0: gdImageSetPixel(im, 50, 50, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageLine is used to draw a line between two endpoints (x1,y1 and x2, y2). wolffd@0: The line is drawn using the color index specified. Note that the color wolffd@0: index can be an actual color returned by wolffd@0: gdImageColorAllocate or one of gdStyled, wolffd@0: gdBrushed or wolffd@0: gdStyledBrushed. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green 
wolffd@0:   and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a line from the upper left corner to the 
wolffd@0:   lower right corner. */
wolffd@0: gdImageLine(im, 0, 0, 99, 99, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageDashedLine is provided solely for backwards compatibility wolffd@0: with gd 1.0. New programs should draw dashed lines using wolffd@0: the normal gdImageLine function and the wolffd@0: new gdImageSetStyle function. wolffd@0:

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

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue 
wolffd@0:   all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a dashed line from the upper left corner 
wolffd@0:   to the lower right corner. */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImagePolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImagePolygon is used to draw a polygon with the verticies wolffd@0: (at least 3) specified, using the color index specified. wolffd@0: See also gdImageFilledPolygon. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: /* Points of polygon */
wolffd@0: gdPoint points[3];
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and 
wolffd@0:   blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a triangle. */
wolffd@0: points[0].x = 50;
wolffd@0: points[0].y = 0;
wolffd@0: points[1].x = 99;
wolffd@0: points[1].y = 99;
wolffd@0: points[2].x = 0;
wolffd@0: points[2].y = 99;
wolffd@0: gdImagePolygon(im, points, 3, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageOpenPolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageOpenPolygon is used to draw a sequence of lines with the verticies wolffd@0: (at least 3) specified, using the color index specified. Unlike wolffd@0: gdImagePolygon, the enpoints of the line wolffd@0: sequence are not connected to a closed polygon. wolffd@0:
void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageRectangle is used to draw a rectangle with the two corners wolffd@0: (upper left first, then lower right) specified, using the wolffd@0: color index specified. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a rectangle occupying the central area. */
wolffd@0: gdImageRectangle(im, 25, 25, 74, 74, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageFilledPolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageFilledPolygon is used to fill a polygon with the verticies wolffd@0: (at least 3) specified, using the color index specified. wolffd@0: See also gdImagePolygon. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: int red;
wolffd@0: /* Points of polygon */
wolffd@0: gdPoint points[3];
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Allocate the color red. */
wolffd@0: red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0: /* Draw a triangle. */
wolffd@0: points[0].x = 50;
wolffd@0: points[0].y = 0;
wolffd@0: points[1].x = 99;
wolffd@0: points[1].y = 99;
wolffd@0: points[2].x = 0;
wolffd@0: points[2].y = 99;
wolffd@0: /* Paint it in white */
wolffd@0: gdImageFilledPolygon(im, points, 3, white);
wolffd@0: /* Outline it in red; must be done second */
wolffd@0: gdImagePolygon(im, points, 3, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageFilledRectangle is used to draw a solid rectangle with the two corners wolffd@0: (upper left first, then lower right) specified, using the wolffd@0: color index specified. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = int gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a filled rectangle occupying the central area. */
wolffd@0: gdImageFilledRectangle(im, 25, 25, 74, 74, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageArc is used to draw a partial ellipse centered at the given point, wolffd@0: with the specified width and height in pixels. The arc begins at wolffd@0: the position in degrees specified by s and ends at wolffd@0: the position specified by e. The arc is drawn in wolffd@0: the color specified by the last argument. A circle can be drawn wolffd@0: by beginning from 0 degrees and ending at 360 degrees, with wolffd@0: width and height being equal. e must be greater than s. Values greater wolffd@0: than 360 are interpreted modulo 360. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 50);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Inscribe an ellipse in the image. */
wolffd@0: gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageFilledArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color, int style) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageFilledArc is used to draw a partial ellipse centered at the given point, wolffd@0: with the specified width and height in pixels. The arc begins at wolffd@0: the position in degrees specified by s and ends at wolffd@0: the position specified by e. The arc is filled in wolffd@0: the color specified by the second to last argument. A circle can be drawn wolffd@0: by beginning from 0 degrees and ending at 360 degrees, with wolffd@0: width and height being equal. e must be greater than s. Values greater wolffd@0: than 360 are interpreted modulo 360. The last argument is a bitwise wolffd@0: OR of the following possibilities: wolffd@0:
    wolffd@0:
  • gdArc wolffd@0:
  • gdChord wolffd@0:
  • gdPie (synonym for gdChord) wolffd@0:
  • gdNoFill wolffd@0:
  • gdEdged wolffd@0:
wolffd@0: gdArc and gdChord are mutually exclusive; wolffd@0: gdChord just connects the starting and ending wolffd@0: angles with a straight line, while gdArc produces wolffd@0: a rounded edge. gdPie is a synonym for gdArc. wolffd@0: gdNoFill indicates that the arc or chord should be wolffd@0: outlined, not filled. gdEdged, used together with wolffd@0: gdNoFill, indicates that the beginning and ending wolffd@0: angles should be connected to the center; this is wolffd@0: a good way to outline (rather than fill) a wolffd@0: 'pie slice'. wolffd@0: wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 50);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Inscribe a filled pie slice in the image. */
wolffd@0: gdImageFilledArc(im, 50, 25, 98, 48, 0, 45, white, gdArc);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageFilledEllipse is used to draw an ellipse centered at the given point, wolffd@0: with the specified width and height in pixels. The ellipse is filled in wolffd@0: the color specified by the last argument. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 50);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Inscribe a filled ellipse in the image. */
wolffd@0: gdImageFilledEllipse(im, 50, 25, 98, 48, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageFillToBorder floods a portion of the image with the specified wolffd@0: color, beginning at the specified point and stopping at wolffd@0: the specified border color. For a way of flooding an wolffd@0: area defined by the color of the starting point, see wolffd@0: gdImageFill. wolffd@0:

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

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

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: int red;
wolffd@0: im = gdImageCreate(100, 50);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Allocate the color red. */
wolffd@0: red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0: /* Inscribe an ellipse in the image. */
wolffd@0: gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
wolffd@0: /* Flood-fill the ellipse. Fill color is red, border color is
wolffd@0:   white (ellipse). */
wolffd@0: gdImageFillToBorder(im, 50, 50, white, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageFill(gdImagePtr im, int x, int y, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageFill floods a portion of the image with the specified wolffd@0: color, beginning at the specified point and flooding the wolffd@0: surrounding region of the same color as the starting point. wolffd@0: For a way of flooding a region defined by a specific border wolffd@0: color rather than by its interior color, see wolffd@0: gdImageFillToBorder. wolffd@0:

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

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

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: int red;
wolffd@0: im = gdImageCreate(100, 50);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Allocate the color red. */
wolffd@0: red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0: /* Inscribe an ellipse in the image. */
wolffd@0: gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
wolffd@0: /* Flood-fill the ellipse. Fill color is red, and will replace the
wolffd@0:   black interior of the ellipse. */
wolffd@0: gdImageFill(im, 50, 50, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageSetAntiAliased(gdImagePtr im, int c) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: "Antialiasing" is a process by which jagged edges associated with line wolffd@0: drawing can be reduced by blending the foreground color with an appropriate wolffd@0: percentage of the background, depending on how much of the pixel in question wolffd@0: is actually within the boundaries of the line being drawn. wolffd@0: All line-drawing functions, wolffd@0: such as gdImageLine, wolffd@0: gdImageOpenPolygon and wolffd@0: gdImagePolygon, will draw antialiased lines wolffd@0: if the special "color" wolffd@0: gdAntiAliased is used when calling them. wolffd@0:

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

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

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

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im, brush;
wolffd@0: int black;
wolffd@0: int blue;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: blue = gdImageColorAllocate(im, 0, 0, 255);
wolffd@0: gdImageSetAntiAliased(im, blue);
wolffd@0: /* Draw a smooth line from the upper left corner to the 
wolffd@0:   lower right corner. */
wolffd@0: gdImageLine(im, 0, 0, 99, 99, gdAntiAliased);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: See also gdAntiAliased and wolffd@0: gdSetAntiAliasedDontBlend. wolffd@0:
void gdImageSetAntiAliasedDontBlend(gdImagePtr im, int c) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Normally, when drawing lines with the special wolffd@0: gdAntiAliased "color," blending with the wolffd@0: background to reduce jagged edges is the desired behavior. However, when wolffd@0: it is desired that lines not be blended with one particular color when wolffd@0: it is encountered in the background, the wolffd@0: gdImageSetAntiAliasedDontBlend function can be used to indicate the wolffd@0: special color that the foreground should stand out more clearly against. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im, brush;
wolffd@0: int black;
wolffd@0: int blue;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: blue = gdImageColorAllocate(im, 0, 0, 255);
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);
wolffd@0: 
wolffd@0: gdImageSetAntiAliased(im, blue);
wolffd@0: /* The portion of the line that crosses this white rectangle will
wolffd@0:   not be blended smoothly */
wolffd@0: gdImageSetAntiAliasedDontBlend(im, white);
wolffd@0: gdImageFilledRectangle(im, 25, 25, 75, 75, white);
wolffd@0: /* Draw a smooth line from the upper left corner 
wolffd@0:   to the lower right corner. */
wolffd@0: gdImageLine(im, 0, 0, 99, 99, gdAntiAliased);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: See also gdAntiAliased and wolffd@0: gdSetAntiAliased. wolffd@0:
void gdImageSetBrush(gdImagePtr im, gdImagePtr brush) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: A "brush" is an image used to draw wide, shaped strokes in another image. Just wolffd@0: as a paintbrush is not a single point, a brush image need not be wolffd@0: a single pixel. Any gd image can be used as a brush, and by wolffd@0: setting the transparent color index of the brush image with wolffd@0: gdImageColorTransparent, wolffd@0: a brush of any shape can be created. All line-drawing functions, wolffd@0: such as gdImageLine, wolffd@0: gdImageOpenPolygon and wolffd@0: gdImagePolygon, will use the wolffd@0: current brush if the special "color" wolffd@0: gdBrushed or gdStyledBrushed wolffd@0: is used when calling them. wolffd@0:

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

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

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im, brush;
wolffd@0: FILE *in;
wolffd@0: int black;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Open the brush PNG. For best results, portions of the
wolffd@0:   brush that should be transparent (ie, not part of the
wolffd@0:   brush shape) should have the transparent color index. */
wolffd@0: in = fopen("star.png", "rb");
wolffd@0: brush = gdImageCreateFromPng(in);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: gdImageSetBrush(im, brush);
wolffd@0: /* Draw a line from the upper left corner to the lower right corner
wolffd@0:   using the brush. */
wolffd@0: gdImageLine(im, 0, 0, 99, 99, gdBrushed);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: /* Destroy the brush image */
wolffd@0: gdImageDestroy(brush);
wolffd@0: 
wolffd@0:
void gdImageSetTile(gdImagePtr im, gdImagePtr tile) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: A "tile" is an image used to fill an area with a repeated pattern. wolffd@0: Any gd image can be used as a tile, and by wolffd@0: setting the transparent color index of the tile image with wolffd@0: gdImageColorTransparent, wolffd@0: a tile that allows certain parts of the underlying area to shine wolffd@0: through can be created. All region-filling functions, wolffd@0: such as gdImageFill and wolffd@0: gdImageFilledPolygon, will use the wolffd@0: current tile if the special "color" wolffd@0: gdTiled is used when calling them. wolffd@0:

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

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

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im, tile;
wolffd@0: FILE *in;
wolffd@0: int black;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Open the tile PNG. For best results, portions of the
wolffd@0:   tile that should be transparent (ie, allowing the
wolffd@0:   background to shine through) should have the transparent
wolffd@0:   color index. */
wolffd@0: in = fopen("star.png", "rb");
wolffd@0: tile = gdImageCreateFromPng(in);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: gdImageSetTile(im, tile);
wolffd@0: /* Fill an area using the tile. */
wolffd@0: gdImageFilledRectangle(im, 25, 25, 75, 75, gdTiled);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: /* Destroy the tile image */
wolffd@0: gdImageDestroy(tile);
wolffd@0: 
wolffd@0:
void gdImageSetStyle(gdImagePtr im, int *style, int styleLength) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: It is often desirable to draw dashed lines, dotted lines, and other wolffd@0: variations on a broken line. gdImageSetStyle can be used to set wolffd@0: any desired series of colors, including a special color that wolffd@0: leaves the background intact, to be repeated during the drawing wolffd@0: of a line. wolffd@0:

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

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

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

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

wolffd@0: gdImagePtr im;
wolffd@0: int styleDotted[2], styleDashed[6];
wolffd@0: FILE *in;
wolffd@0: int black;
wolffd@0: int red;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0: /* Set up dotted style. Leave every other pixel alone. */
wolffd@0: styleDotted[0] = red;
wolffd@0: styleDotted[1] = gdTransparent;
wolffd@0: /* Set up dashed style. Three on, three off. */
wolffd@0: styleDashed[0] = red;
wolffd@0: styleDashed[1] = red;
wolffd@0: styleDashed[2] = red;
wolffd@0: styleDashed[3] = gdTransparent;
wolffd@0: styleDashed[4] = gdTransparent;
wolffd@0: styleDashed[5] = gdTransparent;
wolffd@0: /* Set dotted style. Note that we have to specify how many pixels are
wolffd@0:   in the style! */
wolffd@0: gdImageSetStyle(im, styleDotted, 2);
wolffd@0: /* Draw a line from the upper left corner to the lower right corner. */
wolffd@0: gdImageLine(im, 0, 0, 99, 99, gdStyled);
wolffd@0: /* Now the dashed line. */
wolffd@0: gdImageSetStyle(im, styleDashed, 6);
wolffd@0: gdImageLine(im, 0, 99, 0, 99, gdStyled);
wolffd@0: 
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file ... */
wolffd@0: 
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageSetThickness(gdImagePtr im, int thickness) (FUNCTION) wolffd@0:
gdImageSetThickness determines the width of lines drawn by the wolffd@0: gdImageLine, gdImagePolygon, gdImageOpenPolygon wolffd@0: and related functions, in pixels. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Set thickness. */
wolffd@0: gdImageSetThickness(im, 4);
wolffd@0: /* Draw a fat line from the upper left corner to the lower right corner. */
wolffd@0: gdImageLine(im, 0, 0, 99, 99, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
void gdImageAlphaBlending(gdImagePtr im, int blending) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: The gdImageAlphaBlending wolffd@0: function allows for two different modes of drawing on truecolor wolffd@0: images. In blending mode, which is on by default (gd 2.0.2 wolffd@0: and above), the alpha channel component of the color wolffd@0: supplied to all drawing functions, such as wolffd@0: gdImageSetPixel, determines how much of wolffd@0: the underlying color should be allowed to shine through. As a result, wolffd@0: gd automatically blends the existing color at that point with the wolffd@0: drawing color, and stores the result in the image. The resulting pixel wolffd@0: is opaque. In non-blending mode, the drawing color is copied literally wolffd@0: with its alpha channel information, replacing the destination pixel. wolffd@0: Blending mode is not available when drawing on palette images. wolffd@0:
wolffd@0: gdImagePtr im;
wolffd@0: int red, blue;
wolffd@0: im = gdImageCreateTrueColor(100, 100);
wolffd@0: /* Background color */
wolffd@0: red = gdTrueColor(255, 0, 0);  
wolffd@0: gdImageFilledRectangle(im, 0, 0, 100, 100, red);
wolffd@0: /* Drawing color. Full transparency would be an alpha channel value
wolffd@0:   of 127 (gd has a 7 bit alpha chnanel). 0 is opaque,
wolffd@0:   127 is transparent. So cut gdAlphaTransparent in half to get
wolffd@0:   50% blending. */
wolffd@0: blue = gdTrueColorAlpha(0, 0, 255, gdAlphaTransparent / 2);  
wolffd@0: /* Draw with blending. Result will be 50% red, 50% blue: yellow 
wolffd@0:   (emitted light, remember, not reflected light. What you learned 
wolffd@0:   in Kindergarten is wrong here). */
wolffd@0: gdImageAlphaBlending(im, 1);
wolffd@0: gdImageFilledRectangle(im, 0, 0, 25, 25, blue);
wolffd@0: /* Draw without blending. Result will be 50% blue, 50%
wolffd@0:   the background color of the image viewer or web browser
wolffd@0:   used; results in browsers that don't support
wolffd@0:   semi-transparent pixels are unpredictable! */
wolffd@0: gdImageAlphaBlending(im, 0);
wolffd@0: gdImageFilledRectangle(im, 75, 75, 25, 25, blue);
wolffd@0: /* Write the image to disk, etc. */
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageSaveAlpha(gdImagePtr im, int saveFlag) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: By default, gd 2.0.2 and above do not attempt to save full alpha channel information wolffd@0: (as opposed to single-color transparency) when saving PNG images. (PNG wolffd@0: is currently the only output format supported by gd which can accommodate wolffd@0: alpa channel information.) This saves space in the output file. If you wish wolffd@0: to create an image with alpha channel information for use with tools that wolffd@0: support it, call gdImageSaveAlpha(im, 1) to turn on saving of such wolffd@0: information, and call gdImageAlphaBlending(im, 0) wolffd@0: to turn off alpha blending within the library so that alpha channel wolffd@0: information is actually stored in the image rather than being composited wolffd@0: immediately at the time that drawing functions are invoked. wolffd@0:
wolffd@0: void gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Establishes a clipping rectangle. Once gdImageSetClip has been called, wolffd@0: all future drawing operations will remain within the specified clipping wolffd@0: area, until a new gdImageSetClip call takes place. For instance, wolffd@0: if a clipping rectangle of 25, 25, 75, 75 has been set within a wolffd@0: 100x100 image, a diagonal line from 0,0 to 99,99 will appear only wolffd@0: between 25,25 and 75,75. wolffd@0:

wolffd@0: If gdImageSetClip is never called, the clipping area will be the wolffd@0: entire image. wolffd@0:

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

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Set the clipping rectangle. */
wolffd@0: gdImageSetClip(im, 25, 25, 75, 75);
wolffd@0: /* Draw a line from the upper left corner to the lower right corner. 
wolffd@0:   Only the part within the clipping rectangle will appear. */
wolffd@0: gdImageLine(im, 0, 0, 99, 99, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file ... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: See also gdImageGetClip. wolffd@0:
wolffd@0: void gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Fetches the boundaries of the current clipping rectangle. wolffd@0:
wolffd@0: ... Inside a function ... 
wolffd@0: gdImagePtr im = gdImageCreateTrueColor(100, 100);
wolffd@0: int x1, y1, x2, y2;
wolffd@0: gdImageSetClip(im, 25, 25, 75, 75);
wolffd@0: gdImageGetClip(im, &x1, &y1, &x2, &y2);
wolffd@0: printf("%d %d %d %d\n", x1, y1, x2, y2);
wolffd@0: 
wolffd@0: The above code would print: wolffd@0:
wolffd@0: 25 25 75 75
wolffd@0: 
wolffd@0: See also gdImageSetClip. wolffd@0:
wolffd@0:

Query Functions

wolffd@0:
wolffd@0:
wolffd@0: int gdImageAlpha(gdImagePtr im, int color) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageAlpha is a macro which returns the alpha channel component of wolffd@0: the specified color index. Alpha channel values vary between wolffd@0: 0 (gdAlphaOpaque), which does not blend at all with the background, wolffd@0: through 127 (gdAlphaTransparent), which allows the background to wolffd@0: shine through 100%. Use this macro rather than accessing the wolffd@0: structure members directly. wolffd@0: int gdImageBlue(gdImagePtr im, int color) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageBlue is a macro which returns the blue component of wolffd@0: the specified color index. Use this macro rather than accessing the wolffd@0: structure members directly. wolffd@0:
int gdImageGetPixel(gdImagePtr im, int x, int y) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageGetPixel() retrieves the color index of a particular wolffd@0: pixel. Always use this function to query pixels; wolffd@0: do not access the pixels of the gdImage structure wolffd@0: directly. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: FILE *in;
wolffd@0: gdImagePtr im;
wolffd@0: int c;
wolffd@0: in = fopen("mypng.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: c = gdImageGetPixel(im, gdImageSX(im) / 2, gdImageSY(im) / 2);
wolffd@0: printf("The value of the center pixel is %d; RGB values are %d,%d,%d\n",
wolffd@0:   c, im->red[c], im->green[c], im->blue[c]);
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageBoundsSafe(gdImagePtr im, int x, int y) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageBoundsSafe returns true (1) if the specified point is within the wolffd@0: current clipping rectangle, false (0) if not. The clipping rectangle is wolffd@0: set by gdImageSetClip and defaults wolffd@0: to the entire image. This function is intended primarily for wolffd@0: use by those who wish to add functions to gd. All of the gd drawing wolffd@0: functions already clip safely using this function or its macro wolffd@0: equivalent in gd.c, gdImageBoundsSafeMacro. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: if (gdImageBoundsSafe(im, 50, 50)) {
wolffd@0:   printf("50, 50 is within the image bounds\n");
wolffd@0: } else {
wolffd@0:   printf("50, 50 is outside the image bounds\n");
wolffd@0: }
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageGreen(gdImagePtr im, int color) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageGreen is a macro which returns the green component of wolffd@0: the specified color index. Use this macro rather than accessing the wolffd@0: structure members directly. wolffd@0:
wolffd@0: int gdImageRed(gdImagePtr im, int color) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageRed is a macro which returns the red component of wolffd@0: the specified color index. Use this macro rather than accessing the wolffd@0: structure members directly. wolffd@0:
wolffd@0: int gdImageSX(gdImagePtr im) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageSX is a macro which returns the width of the image wolffd@0: in pixels. Use this macro rather than accessing the wolffd@0: structure members directly. wolffd@0:
wolffd@0: int gdImageSY(gdImagePtr im) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageSY is a macro which returns the height of the image wolffd@0: in pixels. Use this macro rather than accessing the wolffd@0: structure members directly. wolffd@0:
wolffd@0:

Fonts and text-handling functions

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

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

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

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

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

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

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

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

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

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

wolffd@0: void gdImageChar(gdImagePtr im, gdFontPtr font, int x, int y, wolffd@0: int c, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageChar is used to draw single characters on the image. wolffd@0: (To draw multiple characters, use wolffd@0: gdImageString or wolffd@0: gdImageString16. wolffd@0: See also gdImageStringFT for a high quality wolffd@0: solution.) wolffd@0: The second argument is a pointer to a font definition structure; five fonts are wolffd@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, wolffd@0: gdFontLarge, and gdFontGiant. wolffd@0:

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

wolffd@0:

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

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

wolffd@0: #include "gd.h"
wolffd@0: #include "gdfontl.h"
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a character. */
wolffd@0: gdImageChar(im, gdFontGetLarge(), 0, 0, 'Q', white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageCharUp(gdImagePtr im, gdFontPtr font, int x, int y, wolffd@0: int c, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCharUp is used to draw single characters on the image, wolffd@0: rotated 90 degrees. wolffd@0: (To draw multiple characters, use wolffd@0: gdImageStringUp or wolffd@0: gdImageStringUp16.) The second argument is a wolffd@0: pointer to a font definition structure; five fonts are wolffd@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, wolffd@0: gdFontLarge, and gdFontGiant. You must wolffd@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", wolffd@0: "gdfontl.h" and "gdfontg.h" respectively wolffd@0: and (if you are not using a library-based approach) link with the wolffd@0: corresponding .c files to use the provided fonts. wolffd@0:

wolffd@0:

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

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

wolffd@0: #include "gd.h"
wolffd@0: #include "gdfontl.h"
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a character upwards so it rests against the top of the image. */
wolffd@0: gdImageCharUp(im, gdFontGetLarge(),
wolffd@0:   0, gdFontGetLarge()->h, 'Q', white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageString(gdImagePtr im, gdFontPtr font, int x, int y, wolffd@0: unsigned char *s, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageString is used to draw multiple characters on the image. wolffd@0: (To draw single characters, use wolffd@0: gdImageChar.) The second argument is a wolffd@0: pointer to a font definition structure; five fonts are wolffd@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, wolffd@0: gdFontLarge, and gdFontGiant. You must wolffd@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", wolffd@0: "gdfontl.h" and "gdfontg.h" respectively wolffd@0: and (if you are not using a library-based approach) link with the wolffd@0: corresponding .c files to use the provided fonts. wolffd@0:

wolffd@0:

wolffd@0: Windows DLL users: although you can use wolffd@0: these DLL-exported pointers directly, you cannot easily assign them to other wolffd@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you wolffd@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), wolffd@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to wolffd@0: obtain pointers to the fonts under Windows. wolffd@0:
wolffd@0: The null-terminated C string specified wolffd@0: by the fifth argument is drawn from left to right in the specified wolffd@0: color. (See gdImageStringUp for a way wolffd@0: of drawing vertical text. wolffd@0: See also gdImageStringFT for a high wolffd@0: quality solution.) wolffd@0: Pixels not set by a particular character retain their previous color. wolffd@0:
wolffd@0: #include "gd.h"
wolffd@0: #include "gdfontl.h"
wolffd@0: #include <string.h>
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: /* String to draw. */
wolffd@0: char *s = "Hello.";
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a centered string. */
wolffd@0: gdImageString(im, gdFontGetLarge(),
wolffd@0:   im->sx / 2 - (strlen(s) * gdFontGetLarge()->w / 2),
wolffd@0:   im->sy / 2 - gdFontGetLarge()->h / 2,
wolffd@0:   s, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageString16(gdImagePtr im, gdFontPtr font, int x, int y, wolffd@0: unsigned short *s, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageString16 is used to draw multiple 16-bit characters on the image. wolffd@0: (To draw single characters, use wolffd@0: gdImageChar16.) The second argument is a wolffd@0: pointer to a font definition structure; no 16-bit fonts are wolffd@0: provided with gd as standard equipment and there does not seem to be wolffd@0: much momentum to create them although the bdftogd script can do so. The wolffd@0: preferred solution is gdImageStringFT, which wolffd@0: uses freetype to provide truetype font support. wolffd@0:

wolffd@0:

wolffd@0: Windows DLL users: although you can use wolffd@0: these DLL-exported pointers directly, you cannot easily assign them to other wolffd@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you wolffd@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), wolffd@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to wolffd@0: obtain pointers to the fonts under Windows. wolffd@0:
wolffd@0: The null-terminated string of characters represented as 16-bit unsigned wolffd@0: short integers specified by the fifth argument is drawn from left to right wolffd@0: in the specified wolffd@0: color. (See gdImageStringUp16 for a way wolffd@0: of drawing vertical text.) Pixels not wolffd@0: set by a particular character retain their previous color. wolffd@0:

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

wolffd@0: void gdImageStringUp(gdImagePtr im, gdFontPtr font, int x, int y, wolffd@0: unsigned char *s, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageStringUp is used to draw multiple characters on the image, wolffd@0: rotated 90 degrees. wolffd@0: (To draw single characters, use wolffd@0: gdImageCharUp.) The second argument is a wolffd@0: pointer to a font definition structure; five fonts are wolffd@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, wolffd@0: gdFontLarge, and gdFontGiant. You must wolffd@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", wolffd@0: "gdfontl.h" and "gdfontg.h" respectively wolffd@0: and (if you are not using a library-based approach) link with the wolffd@0: corresponding .c files to use the provided fonts. wolffd@0:
wolffd@0: Windows DLL users: although you can use wolffd@0: these DLL-exported pointers directly, you cannot easily assign them to other wolffd@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you wolffd@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), wolffd@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to wolffd@0: obtain pointers to the fonts under Windows. wolffd@0:
wolffd@0: wolffd@0: The null-terminated C string specified wolffd@0: by the fifth argument is drawn from bottom to top (rotated wolffd@0: 90 degrees) in the specified color. (See wolffd@0: gdImageString for a way wolffd@0: of drawing horizontal text.) Pixels not wolffd@0: set by a particular character retain their previous color. wolffd@0:
wolffd@0: #include "gd.h"
wolffd@0: #include "gdfontl.h"
wolffd@0: #include <string.h>
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: /* String to draw. */
wolffd@0: char *s = "Hello.";
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color white (red, green and blue all maximum). */
wolffd@0: white = gdImageColorAllocate(im, 255, 255, 255);  
wolffd@0: /* Draw a centered string going upwards. Axes are reversed,
wolffd@0:   and Y axis is decreasing as the string is drawn. */
wolffd@0: gdImageStringUp(im, gdFontGetLarge(),
wolffd@0:   im->w / 2 - gdFontGetLarge()->h / 2,
wolffd@0:   im->h / 2 + (strlen(s) * gdFontGetLarge()->w / 2),
wolffd@0:   s, white);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageStringUp16(gdImagePtr im, gdFontPtr font, int x, int y, wolffd@0: unsigned short *s, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageString is used to draw multiple 16-bit characters vertically on wolffd@0: the image. (To draw single characters, use wolffd@0: gdImageChar.) The second argument is a wolffd@0: pointer to a font definition structure; five fonts are wolffd@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, wolffd@0: gdFontLarge, and gdFontGiant. You must wolffd@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", wolffd@0: "gdfontl.h" and "gdfontg.h" respectively wolffd@0: and (if you are not using a library-based approach) link with the wolffd@0: corresponding .c files to use the provided fonts. wolffd@0:
wolffd@0: Windows DLL users: although you can use wolffd@0: these DLL-exported pointers directly, you cannot easily assign them to other wolffd@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you wolffd@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), wolffd@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to wolffd@0: obtain pointers to the fonts under Windows. wolffd@0:
wolffd@0: The null-terminated string of characters represented as 16-bit unsigned wolffd@0: short integers specified by the fifth argument is drawn from bottom to top wolffd@0: in the specified color. wolffd@0: (See gdImageStringUp16 for a way wolffd@0: of drawing horizontal text.) Pixels not wolffd@0: set by a particular character retain their previous color. wolffd@0:

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

int gdFTUseFontConfig(int flag) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: GD 2.0.29 introduced the ability to use wolffd@0: fontconfig patterns wolffd@0: rather than font file names as parameters to wolffd@0: gdImageStringFT, wolffd@0: gdImageStringFTEx and wolffd@0: gdImageStringFTCircle. wolffd@0: For backwards compatibility reasons, the fontlist parameter to those wolffd@0: functions is still expected to be a full or partial font file path name wolffd@0: or list thereof by default. However, as a convenience, a single call wolffd@0: to gdFTUseFontConfig with a nonzero parameter configures gd to expect wolffd@0: the fontlist parameter to be a fontconfig pattern. Regardless of whether wolffd@0: the flag argument is nonzero, this function returns true when the wolffd@0: fontconfig library is available and false when it is not. When fontconfig wolffd@0: is not available, the fontlist parameter always behaves as in previous wolffd@0: versions of GD. wolffd@0:
wolffd@0: #include "gd.h"
wolffd@0: #include <string.h>
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: int brect[8];
wolffd@0: int x, y;
wolffd@0: char *err;
wolffd@0: 
wolffd@0: char *s = "Hello."; /* String to draw. */
wolffd@0: double sz = 40.;
wolffd@0: char *fc = "times:bold:italic"; /* fontconfig pattern */
wolffd@0: 
wolffd@0: /* Signal that all freetype font calls in this program will receive
wolffd@0:   fontconfig patterns rather than filenames of font files */
wolffd@0: gdUseFontConfig(1);
wolffd@0: 
wolffd@0: /* obtain brect so that we can size the image */
wolffd@0: err = gdImageStringFT(NULL,&brect[0],0,fc,sz,0.,0,0,s);
wolffd@0: if (err) {fprintf(stderr,err); return 1;}
wolffd@0: 
wolffd@0: /* create an image big enough for the string plus a little whitespace */
wolffd@0: x = brect[2]-brect[6] + 6;
wolffd@0: y = brect[3]-brect[7] + 6;
wolffd@0: im = gdImageCreate(x,y);
wolffd@0: 
wolffd@0: /* Background color (first allocated) */
wolffd@0: white = gdImageColorResolve(im, 255, 255, 255);
wolffd@0: black = gdImageColorResolve(im, 0, 0, 0);
wolffd@0: 
wolffd@0: /* render the string, offset origin to center string*/
wolffd@0: /* note that we use top-left coordinate for adjustment
wolffd@0:  * since gd origin is in top-left with y increasing downwards. */
wolffd@0: x = 3 - brect[6];
wolffd@0: y = 3 - brect[7];
wolffd@0: err = gdImageStringFT(im,&brect[0],black,fc,sz,0.0,x,y,s);
wolffd@0: if (err) {fprintf(stderr,err); return 1;}
wolffd@0: 
wolffd@0:
wolffd@0: char *gdImageStringFT(gdImagePtr im, int *brect, wolffd@0: int fg, char *fontname, double ptsize, double angle, wolffd@0: int x, int y, char *string) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: RECOMMENDED. New in 1.8.4. gdImageStringFT draws text using the wolffd@0: FreeType 2.x library. wolffd@0:

wolffd@0: gdImageStringFT draws a string of anti-aliased characters on the image using wolffd@0: the FreeType wolffd@0: library to render user-supplied TrueType fonts. We do not provide wolffd@0: TrueType fonts (.ttf and .ttc files). Obtaining them is entirely up to wolffd@0: you. The string is anti-aliased, meaning that there should be wolffd@0: fewer "jaggies" visible. The fontname is the full pathname to a TrueType wolffd@0: font file, or a font face name if the GDFONTPATH environment variable wolffd@0: 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). wolffd@0:

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

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

wolffd@0: The user-supplied int brect[8] array is filled on return from gdImageStringFT wolffd@0: with the 8 elements representing the 4 corner coordinates of the wolffd@0: bounding rectangle (the smallest rectangle that completely surrounds the wolffd@0: rendered string and does not intersect any pixel of the rendered string). wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0:
0 wolffd@0: lower left corner, X position
1 wolffd@0: lower left corner, Y position
2 wolffd@0: lower right corner, X position
3 wolffd@0: lower right corner, Y position
4 wolffd@0: upper right corner, X position
5 wolffd@0: upper right corner, Y position
6 wolffd@0: upper left corner, X position
7 wolffd@0: upper left corner, Y position
wolffd@0:

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

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

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

wolffd@0: The string may contain UTF-8 sequences like: "&#192;" wolffd@0:

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

wolffd@0: #include "gd.h"
wolffd@0: #include <string.h>
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int white;
wolffd@0: int brect[8];
wolffd@0: int x, y;
wolffd@0: char *err;
wolffd@0: 
wolffd@0: char *s = "Hello."; /* String to draw. */
wolffd@0: double sz = 40.;
wolffd@0: char *f = "/usr/local/share/ttf/Times.ttf";  /* User supplied font */
wolffd@0: 
wolffd@0: /* obtain brect so that we can size the image */
wolffd@0: err = gdImageStringFT(NULL,&brect[0],0,f,sz,0.,0,0,s);
wolffd@0: if (err) {fprintf(stderr,err); return 1;}
wolffd@0: 
wolffd@0: /* create an image big enough for the string plus a little whitespace */
wolffd@0: x = brect[2]-brect[6] + 6;
wolffd@0: y = brect[3]-brect[7] + 6;
wolffd@0: im = gdImageCreate(x,y);
wolffd@0: 
wolffd@0: /* Background color (first allocated) */
wolffd@0: white = gdImageColorResolve(im, 255, 255, 255);
wolffd@0: black = gdImageColorResolve(im, 0, 0, 0);
wolffd@0: 
wolffd@0: /* render the string, offset origin to center string*/
wolffd@0: /* note that we use top-left coordinate for adjustment
wolffd@0:  * since gd origin is in top-left with y increasing downwards. */
wolffd@0: x = 3 - brect[6];
wolffd@0: y = 3 - brect[7];
wolffd@0: err = gdImageStringFT(im,&brect[0],black,f,sz,0.0,x,y,s);
wolffd@0: if (err) {fprintf(stderr,err); return 1;}
wolffd@0: 
wolffd@0: /* Write img to stdout */
wolffd@0: gdImagePng(im, stdout);
wolffd@0: 
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0: See also gdImageStringFTEx. wolffd@0:
wolffd@0: char *gdImageStringFTEx(gdImagePtr im, int *brect, wolffd@0: int fg, char *fontname, double ptsize, double angle, wolffd@0: int x, int y, char *string, gdFTStringExtraPtr strex) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: New in 2.0.5, also found in common third-party versions wolffd@0: of gd. gdImageStringFTEx extends the capabilities of wolffd@0: gdImageStringFT by providing a wolffd@0: way to pass additional parameters. wolffd@0:

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

wolffd@0: typedef struct {
wolffd@0:        /* logical OR of gdFTEX_ values */
wolffd@0:        int flags; 
wolffd@0:        /* fine tune line spacing for '\n' */
wolffd@0:        double linespacing; 
wolffd@0:        /* Preferred character mapping */
wolffd@0:        int charmap;
wolffd@0:        /* Rendering resolution */
wolffd@0:        int hdpi;
wolffd@0:        int vdpi;
wolffd@0:        char *xshow;
wolffd@0:        char *fontpath;
wolffd@0: } gdFTStringExtra, *gdFTStringExtraPtr;
wolffd@0: 
wolffd@0: To output multiline text with a specific line spacing, wolffd@0: include gdFTEX_LINESPACE in the setting of wolffd@0: flags: wolffd@0:
wolffd@0: flags |= gdFTEX_LINESPACE;
wolffd@0: 
wolffd@0: And also set linespacing to the desired spacing, expressed as a wolffd@0: multiple of the font height. Thus a line spacing of 1.0 is the wolffd@0: minimum to guarantee that lines of text do not collide. wolffd@0:

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

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

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

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

wolffd@0: flags | gdFTEX_RESOLUTION;
wolffd@0: 
wolffd@0: GD 2.0.29 and later will normally attempt to apply kerning tables, if wolffd@0: fontconfig is available, to adjust the relative positions of consecutive wolffd@0: characters more ideally for that pair of characters. This can be turn off by wolffd@0: specifying the gdFTEX_DISABLE_KERNING flag: wolffd@0:
wolffd@0: flags | gdFTEX_DISABLE_KERNING;
wolffd@0: 
wolffd@0: GD 2.0.29 and later can return a vector of individual character wolffd@0: position advances, occasionally useful in applications that must know wolffd@0: exactly where each character begins. This is returned in the xshow wolffd@0: element of the gdFTStringExtra structure if the gdFTEX_XSHOW wolffd@0: flag is set: wolffd@0:
wolffd@0: flags | gdFTEX_XSHOW;
wolffd@0: 
wolffd@0: The caller is responsible for calling gdFree() on the xshow wolffd@0: element after the call if gdFTEX_XSHOW is set. wolffd@0:

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

wolffd@0: flags | gdFTEX_RETURNFONTPATHNAME;
wolffd@0: 
wolffd@0: The caller is responsible for calling gdFree() on the fontpath wolffd@0: element after the call if gdFTEX_RETURNFONTPATHNAME is set. wolffd@0:

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

wolffd@0: flags | gdFTEX_FONTPATHNAME;
wolffd@0: 
wolffd@0:

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

wolffd@0: flags | gdFTEX_FONTCONFIG;
wolffd@0: 
wolffd@0: GD 2.0.29 and above can use fontconfig to resolve wolffd@0: font names, including fontconfig patterns, if the gdFTEX_FONTCONFIG wolffd@0: flag is set. As a convenience, this behavior can be made the default wolffd@0: by calling gdFTUseFontConfig with wolffd@0: a nonzero value. In that situation it is not necessary to set the wolffd@0: gdFTEX_FONTCONFIG flag on every call; however explicit font path names wolffd@0: can still be used if the gdFTEX_FONTPATHNAME flag is set: wolffd@0:
wolffd@0: flags | gdFTEX_FONTPATHNAME;
wolffd@0: 
wolffd@0: For more information, see gdImageStringFT. wolffd@0:
wolffd@0: char *gdImageStringFTCircle(gdImagePtr im, wolffd@0: int cx, wolffd@0: int cy, wolffd@0: double radius, wolffd@0: double textRadius, wolffd@0: double fillPortion, wolffd@0: char *font, wolffd@0: double points, wolffd@0: char *top, wolffd@0: char *bottom, wolffd@0: int fgcolor) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Draws the text strings specified by top and bottom wolffd@0: on im, curved along the edge of a circle of radius wolffd@0: radius, with its center at cx and cy. wolffd@0: top is written clockwise wolffd@0: along the top; bottom is written counterclockwise wolffd@0: along the bottom. textRadius determines the "height" wolffd@0: of each character; if textRadius is 1/2 of wolffd@0: radius, wolffd@0: characters extend halfway from the edge to the center. wolffd@0: fillPortion varies from 0 to 1.0, with useful values wolffd@0: from about 0.4 to 0.9, and determines how much of the wolffd@0: 180 degrees of arc assigned to each section of text wolffd@0: is actually occupied by text; 0.9 looks better than wolffd@0: 1.0 which is rather crowded. font is a freetype wolffd@0: font; see gdImageStringFT. points is passed to the wolffd@0: freetype engine and has an effect on hinting; although wolffd@0: the size of the text is determined by radius, wolffd@0: textRadius, and fillPortion, you should wolffd@0: pass a point size that wolffd@0: "hints" appropriately -- if you know the text will be wolffd@0: large, pass a large point size such as 24.0 to get the wolffd@0: best results. fgcolor can be any color, and may have wolffd@0: an alpha component, do blending, etc. wolffd@0:

wolffd@0: Returns 0 on success, or an error string otherwise. wolffd@0:

wolffd@0: #include <stdio.h>
wolffd@0: #include <gd.h>
wolffd@0: 
wolffd@0: int main (int argc, char *argv[])
wolffd@0: {
wolffd@0:         FILE *in;
wolffd@0:         FILE *out;
wolffd@0:         gdImagePtr im;
wolffd@0:         int radius;
wolffd@0:         /* Create an image of text on a circle, with an
wolffd@0:                 alpha channel so that we can copy it onto a
wolffd@0:                 background */
wolffd@0:         in = fopen("mypicture.jpg", "rb");
wolffd@0:         if (!in) {
wolffd@0:                 im = gdImageCreateTrueColor(300, 300);
wolffd@0:         } else {
wolffd@0:                 im = gdImageCreateFromJpeg(in);
wolffd@0:                 fclose(in);
wolffd@0:         }
wolffd@0:         if (gdImageSX(im) < gdImageSY(im)) {
wolffd@0:                 radius = gdImageSX(im) / 2;
wolffd@0:         } else {
wolffd@0:                 radius = gdImageSY(im) / 2;
wolffd@0:         }
wolffd@0:         gdStringFTCircle(
wolffd@0:                 im,
wolffd@0:                 gdImageSX(im) / 2,
wolffd@0:                 gdImageSY(im) / 2,
wolffd@0:                 radius,
wolffd@0:                 radius / 2,
wolffd@0:                 0.8,
wolffd@0:                 "arial",
wolffd@0:                 24,
wolffd@0:                 "top text",
wolffd@0:                 "bottom text",
wolffd@0:                 gdTrueColorAlpha(240, 240, 255, 32));
wolffd@0:         out = fopen("gdfx.png", "wb");
wolffd@0:         if (!out) {
wolffd@0:                 fprintf(stderr, "Can't create gdfx.png\n");
wolffd@0:                 return 1;
wolffd@0:         }
wolffd@0:         gdImagePng(im, out);
wolffd@0:         fclose(out);
wolffd@0:         gdImageDestroy(im);
wolffd@0:         return 0;
wolffd@0: }
wolffd@0: 
wolffd@0: wolffd@0:

wolffd@0: For more information, see gdImageStringFTEx wolffd@0: and gdImageSquareToCircle. wolffd@0:

wolffd@0: char *gdImageStringTTF(gdImagePtr im, int *brect, wolffd@0: int fg, char *fontname, double ptsize, double angle, wolffd@0: int x, int y, char *string) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: DEPRECATED. This function simply invokes wolffd@0: gdImageStringFT for backwards wolffd@0: compatibility with old code that was written with FreeType 1.x. wolffd@0:
wolffd@0: int gdFontCacheSetup(void) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: This function initializes the font cache for freetype text output wolffd@0: functions such as gdImageStringFTEx. wolffd@0: If this function is not called by the programmer, it is invoked wolffd@0: automatically on the first truetype text output call, which is wolffd@0: perfectly safe unless the application is multithreaded. wolffd@0: Multithreaded applications should directly invoke this function before wolffd@0: allowing any thread to use freetype text output. Returns 0 on success, wolffd@0: nonzero if the freetype library fails to initialize. wolffd@0:
wolffd@0: void gdFontCacheShutdown(void) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: This function releases the memory used by the freetype font cache wolffd@0: and the text output mutex. Applications that use gd for their wolffd@0: entire lifetime, then exit, need not call this function. wolffd@0:
wolffd@0:

Color-handling functions

wolffd@0:
wolffd@0:
wolffd@0: int gdImageColorAllocate(gdImagePtr im, int r, int g, int b) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorAllocate finds the first available color index in wolffd@0: the image specified, sets its RGB values to those requested wolffd@0: (255 is the maximum for each), wolffd@0: and returns the index of the new color table entry, or an RGBA wolffd@0: value in the case of a truecolor image; in either case you can wolffd@0: then use the returned value as a parameter to drawing functions. When wolffd@0: creating a new palette-based image, the first time you invoke this function, wolffd@0: you are setting the background color for that image. wolffd@0:

wolffd@0: In the event that all gdMaxColors colors wolffd@0: (256) have already been allocated, gdImageColorAllocate will wolffd@0: return -1 to indicate failure. (This is not uncommon when wolffd@0: working with existing PNG files that already use 256 colors.) wolffd@0: Note that gdImageColorAllocate wolffd@0: does not check for existing colors that match your request; wolffd@0: see gdImageColorExact, wolffd@0: gdImageColorClosest and wolffd@0: gdImageColorClosestHWB wolffd@0: for ways to locate existing colors that approximate the wolffd@0: color desired in situations where a new color is not available. wolffd@0: Also see gdImageColorResolve, wolffd@0: new in gd-1.6.2. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int red;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color red. */
wolffd@0: red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0: /* Draw a dashed line from the upper left corner 
wolffd@0:   to the lower right corner. */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as saving 
wolffd@0:   it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorAllocateAlpha(gdImagePtr im, int r, int g, int b, int a) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorAllocateAlpha finds the first available color index in wolffd@0: the image specified, sets its RGBA values to those requested wolffd@0: (255 is the maximum for red, green and blue, and 127 represents wolffd@0: full transparency for alpha), wolffd@0: and returns the index of the new color table entry, or an RGBA wolffd@0: value in the case of a truecolor image; in either case you can wolffd@0: then use the returned value as a parameter to drawing functions. When wolffd@0: creating a new palette-based image, the first time you invoke this function, wolffd@0: you are setting the background color for that image. wolffd@0:

wolffd@0: In the event that all gdMaxColors colors wolffd@0: (256) have already been allocated, gdImageColorAllocate will wolffd@0: return -1 to indicate failure. (This is not uncommon when wolffd@0: working with existing palette-based PNG files that already use 256 colors.) wolffd@0: Note that gdImageColorAllocateAlpha wolffd@0: does not check for existing colors that match your request; wolffd@0: see gdImageColorExactAlpha and wolffd@0: gdImageColorClosestAlpha wolffd@0: for ways to locate existing colors that approximate the wolffd@0: color desired in situations where a new color is not available. wolffd@0: Also see gdImageColorResolveAlpha. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: int red;
wolffd@0: im = gdImageCreate(100, 100);
wolffd@0: /* Background color (first allocated) */
wolffd@0: black = gdImageColorAllocate(im, 0, 0, 0);  
wolffd@0: /* Allocate the color red, 50% transparent. */
wolffd@0: red = gdImageColorAllocateAlpha(im, 255, 0, 0, 64);  
wolffd@0: /* Draw a dashed line from the upper left corner to the lower right corner. */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorClosest(gdImagePtr im, int r, int g, int b) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorClosest searches the colors which have been wolffd@0: defined thus far in the image specified and returns the wolffd@0: index of the color with RGB values closest to those of the wolffd@0: request. (Closeness is determined by Euclidian distance, wolffd@0: which is used to determine the distance in three-dimensional color wolffd@0: space between colors.) wolffd@0:

wolffd@0: If no colors have yet been allocated in the image, wolffd@0: gdImageColorClosest returns -1. wolffd@0:

wolffd@0: When applied to a truecolor image, this function always wolffd@0: succeeds in returning the desired color. wolffd@0:

wolffd@0: This function is most useful as a backup method for choosing wolffd@0: a drawing color when an image already contains wolffd@0: gdMaxColors (256) colors and wolffd@0: no more can be allocated. (This is not uncommon when wolffd@0: working with existing PNG files that already use many colors.) wolffd@0: See gdImageColorExact wolffd@0: for a method of locating exact matches only. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: FILE *in;
wolffd@0: int red;
wolffd@0: /* Let's suppose that photo.png is a scanned photograph with
wolffd@0:   many colors. */
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Try to allocate red directly */
wolffd@0: red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0: /* If we fail to allocate red... */
wolffd@0: if (red == (-1)) {
wolffd@0:   /* Find the closest color instead. */
wolffd@0:   red = gdImageColorClosest(im, 255, 0, 0);
wolffd@0: }
wolffd@0: /* Draw a dashed line from the upper left corner to the lower right corner */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorClosestAlpha(gdImagePtr im, int r, int g, int b, int a) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorClosest searches the colors which have been wolffd@0: defined thus far in the image specified and returns the wolffd@0: index of the color with RGBA values closest to those of the wolffd@0: request. (Closeness is determined by Euclidian distance, wolffd@0: which is used to determine the distance in four-dimensional color/alpha wolffd@0: space between colors.) wolffd@0:

wolffd@0: If no colors have yet been allocated in the image, wolffd@0: gdImageColorClosestAlpha returns -1. wolffd@0:

wolffd@0: When applied to a truecolor image, this function always wolffd@0: succeeds in returning the desired color. wolffd@0:

wolffd@0: This function is most useful as a backup method for choosing wolffd@0: a drawing color when a palette-based image already contains wolffd@0: gdMaxColors (256) colors and wolffd@0: no more can be allocated. (This is not uncommon when wolffd@0: working with existing palette-based PNG files that already use many colors.) wolffd@0: See gdImageColorExactAlpha wolffd@0: for a method of locating exact matches only. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: FILE *in;
wolffd@0: int red;
wolffd@0: /* Let's suppose that photo.png is a scanned photograph with
wolffd@0:   many colors. */
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Try to allocate red, 50% transparent, directly */
wolffd@0: red = gdImageColorAllocateAlpha(im, 255, 0, 0, 64);  
wolffd@0: /* If we fail to allocate red... */
wolffd@0: if (red == (-1)) {
wolffd@0:   /* Find the closest color instead. */
wolffd@0:   red = gdImageColorClosestAlpha(im, 255, 0, 0, 64);
wolffd@0: }
wolffd@0: /* Draw a dashed line from the upper left corner to the lower right corner */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorClosestHWB(gdImagePtr im, int r, int g, int b) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorClosestHWB searches the colors which have been wolffd@0: defined thus far in the image specified and returns the wolffd@0: index of the color with hue, whiteness and blackness closest to the wolffd@0: requested color. This scheme is typically superior to the wolffd@0: Euclidian distance scheme used by wolffd@0: gdImageColorClosest. wolffd@0:

wolffd@0: If no colors have yet been allocated in the image, wolffd@0: gdImageColorClosestHWB returns -1. wolffd@0:

wolffd@0: When applied to a truecolor image, this function always wolffd@0: succeeds in returning the desired color. wolffd@0:

wolffd@0: This function is most useful as a backup method for choosing wolffd@0: a drawing color when an image already contains wolffd@0: gdMaxColors (256) colors and wolffd@0: no more can be allocated. (This is not uncommon when wolffd@0: working with existing PNG files that already use many colors.) wolffd@0: See gdImageColorExact wolffd@0: for a method of locating exact matches only. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: FILE *in;
wolffd@0: int red;
wolffd@0: /* Let's suppose that photo.png is a scanned photograph with
wolffd@0:   many colors. */
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Try to allocate red directly */
wolffd@0: red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0: /* If we fail to allocate red... */
wolffd@0: if (red == (-1)) {
wolffd@0:   /* Find the closest color instead. */
wolffd@0:   red = gdImageColorClosestHWB(im, 255, 0, 0);
wolffd@0: }
wolffd@0: /* Draw a dashed line from the upper left corner to the lower right corner */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorExact(gdImagePtr im, int r, int g, int b) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorExact searches the colors which have been wolffd@0: defined thus far in the image specified and returns the wolffd@0: index of the first color with RGB values which exactly wolffd@0: match those of the request. If no allocated color matches the wolffd@0: request precisely, gdImageColorExact returns -1. wolffd@0: See gdImageColorClosest wolffd@0: for a way to find the color closest to the color requested. wolffd@0:

wolffd@0: When applied to a truecolor image, this function always wolffd@0: succeeds in returning the desired color. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int red;
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* The image may already contain red; if it does, we'll save a slot
wolffd@0:   in the color table by using that color. */
wolffd@0: /* Try to allocate red directly */
wolffd@0: red = gdImageColorExact(im, 255, 0, 0);
wolffd@0: /* If red isn't already present... */
wolffd@0: if (red == (-1)) {
wolffd@0:   /* Second best: try to allocate it directly. */
wolffd@0:   red = gdImageColorAllocate(im, 255, 0, 0);  
wolffd@0:   /* Out of colors, so find the closest color instead. */
wolffd@0:   red = gdImageColorClosest(im, 255, 0, 0);
wolffd@0: }
wolffd@0: /* Draw a dashed line from the upper left corner to the lower right corner */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorResolve(gdImagePtr im, int r, int g, int b) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorResolve searches the colors which have been wolffd@0: defined thus far in the image specified and returns the wolffd@0: index of the first color with RGB values which exactly wolffd@0: match those of the request. If no allocated color matches the wolffd@0: request precisely, then gdImageColorResolve tries to allocate the wolffd@0: exact color. If there is no space left in the color table then wolffd@0: gdImageColorResolve returns the closest color (as in gdImageColorClosest). wolffd@0: This function always returns an index of a color. wolffd@0:

wolffd@0: When applied to a truecolor image, this function always wolffd@0: succeeds in returning the desired color. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int red;
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* The image may already contain red; if it does, we'll save a slot
wolffd@0:   in the color table by using that color. */
wolffd@0: /* Get index of red, or color closest to red */
wolffd@0: red = gdImageColorResolve(im, 255, 0, 0);
wolffd@0: /* Draw a dashed line from the upper left corner to the lower right corner */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorResolveAlpha(gdImagePtr im, int r, int g, int b, int a) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorResolveAlpha searches the colors which have been wolffd@0: defined thus far in the image specified and returns the wolffd@0: index of the first color with RGBA values which exactly wolffd@0: match those of the request. If no allocated color matches the wolffd@0: request precisely, then gdImageColorResolveAlpha tries to allocate the wolffd@0: exact color. If there is no space left in the color table then wolffd@0: gdImageColorResolveAlpha returns the closest color (as in gdImageColorClosestAlpha). wolffd@0: This function always returns an index of a color. wolffd@0:

wolffd@0: When applied to a truecolor image, this function always wolffd@0: succeeds in returning the desired color. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int red;
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* The image may already contain red; if it does, 
wolffd@0:   we'll save a slot in the color table by using that color. */
wolffd@0: /* Get index of red, 50% transparent, or the next best thing */
wolffd@0: red = gdImageColorResolveAlpha(im, 255, 0, 0, 64);
wolffd@0: /* Draw a dashed line from the upper left corner to the lower right corner */
wolffd@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
wolffd@0: /* ... Do something with the image, such as saving 
wolffd@0:   it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: int gdImageColorsTotal(gdImagePtr im) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageColorsTotal is a macro which returns the number of wolffd@0: colors currently allocated in a palette image. For truecolor wolffd@0: images, the result of this call is undefined and should not wolffd@0: be used. wolffd@0:
wolffd@0: int gdImageRed(gdImagePtr im, int c) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageRed is a macro which returns the red portion wolffd@0: of the specified color in the image. This macro works wolffd@0: for both palette and truecolor images. wolffd@0:
wolffd@0: int gdImageGreen(gdImagePtr im, int c) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageGreen is a macro which returns the green portion wolffd@0: of the specified color in the image. This macro works wolffd@0: for both palette and truecolor images. wolffd@0:
wolffd@0: int gdImageBlue(gdImagePtr im, int c) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageBlue is a macro which returns the blue portion wolffd@0: of the specified color in the image. This macro works wolffd@0: for both palette and truecolor images. wolffd@0:
wolffd@0: int gdImageGetInterlaced(gdImagePtr im) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageGetInterlaced is a macro which returns true (1) wolffd@0: if the image is interlaced, false (0) if not. wolffd@0: Use this macro to obtain this information; do not wolffd@0: access the structure directly. wolffd@0: See gdImageInterlace for wolffd@0: a means of interlacing images. wolffd@0:
wolffd@0: int gdImageGetTransparent(gdImagePtr im) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageGetTransparent is a macro which returns the wolffd@0: current transparent color index in the image. wolffd@0: If there is no transparent color, gdImageGetTransparent wolffd@0: returns -1. Use this macro to obtain this information; do not wolffd@0: access the structure directly. wolffd@0:
wolffd@0: void gdImageColorDeallocate(gdImagePtr im, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorDeallocate marks the specified color as being wolffd@0: available for reuse. It does not attempt to determine whether wolffd@0: the color index is still in use in the image. After a call wolffd@0: to this function, the next call to wolffd@0: gdImageColorAllocate wolffd@0: for the same image will set new RGB values for that wolffd@0: color index, changing the color of any pixels which wolffd@0: have that index as a result. If multiple calls to wolffd@0: gdImageColorDeallocate are made consecutively, the lowest-numbered wolffd@0: index among them will be reused by the next wolffd@0: gdImageColorAllocate call. wolffd@0:
wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int red, blue;
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Look for red in the color table. */
wolffd@0: red = gdImageColorExact(im, 255, 0, 0);
wolffd@0: /* If red is present... */
wolffd@0: if (red != (-1)) {
wolffd@0:   /* Deallocate it. */
wolffd@0:   gdImageColorDeallocate(im, red);
wolffd@0:   /* Allocate blue, reusing slot in table.
wolffd@0:     Existing red pixels will change color. */
wolffd@0:   blue = gdImageColorAllocate(im, 0, 0, 255);
wolffd@0: }
wolffd@0: /* ... Do something with the image, such as 
wolffd@0:   saving it to a file... */
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageColorTransparent(gdImagePtr im, int color) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageColorTransparent sets the transparent color index wolffd@0: for the specified image to the specified index. To indicate wolffd@0: that there should be no transparent color, invoke wolffd@0: gdImageColorTransparent with a color index of -1. Note that wolffd@0: JPEG images do not support transparency, so this setting has no effect wolffd@0: when writing JPEG images. wolffd@0:

wolffd@0: The color index used should be an index wolffd@0: allocated by gdImageColorAllocate, wolffd@0: whether explicitly invoked by your code or implicitly wolffd@0: invoked by loading an image. wolffd@0: In order to ensure that your image has a reasonable appearance wolffd@0: when viewed by users who do not have transparent background wolffd@0: capabilities (or when you are writing a JPEG-format file, which does wolffd@0: not support transparency), be sure to give reasonable RGB values to the wolffd@0: color you allocate for use as a transparent color, wolffd@0: even though it will be transparent on systems wolffd@0: that support PNG transparency. wolffd@0:

wolffd@0: ... inside a function ...
wolffd@0: gdImagePtr im;
wolffd@0: int black;
wolffd@0: FILE *in, *out;
wolffd@0: in = fopen("photo.png", "rb");
wolffd@0: im = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Look for black in the color table and make it transparent. */
wolffd@0: black = gdImageColorExact(im, 0, 0, 0);
wolffd@0: /* If black is present... */
wolffd@0: if (black != (-1)) {
wolffd@0:   /* Make it transparent */
wolffd@0:   gdImageColorTransparent(im, black);
wolffd@0: }
wolffd@0: /* Save the newly-transparent image back to the file */
wolffd@0: out = fopen("photo.png", "wb");
wolffd@0: gdImagePng(im, out);
wolffd@0: fclose(out);
wolffd@0: /* Destroy it */
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
wolffd@0: void gdImageTrueColor(int red, int green, int blue) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdImageTrueColor returns an RGBA color value for use when wolffd@0: drawing on a truecolor image. Red, green, and blue are all wolffd@0: in the range between 0 (off) and 255 (maximum). This macro should wolffd@0: not be used with palette-based images. If you need to write wolffd@0: code which is compatible with both palette-based and wolffd@0: truecolor images, use gdImageColorResolve. wolffd@0:
wolffd@0: void gdTrueColorAlpha(int red, int green, int blue, int alpha) wolffd@0: (MACRO) wolffd@0:
wolffd@0: gdTrueColorAlpha returns an RGBA color value for use when wolffd@0: drawing on a truecolor image with alpha channel transparency. Red, wolffd@0: green, and blue are all wolffd@0: in the range between 0 (off) and 255 (maximum). Alpha is in the wolffd@0: range between 0 (opaque) and 127 (fully transparent). This macro should wolffd@0: not be used with palette-based images. If you need to write wolffd@0: code which is compatible with both palette-based and wolffd@0: truecolor images, use gdImageColorResolveAlpha.
wolffd@0:

Copying and resizing functions

wolffd@0:
wolffd@0: wolffd@0:
void gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCopy is used to copy a rectangular portion of one image to wolffd@0: another image. (For a way of stretching or shrinking the image wolffd@0: in the process, see wolffd@0: gdImageCopyResized.) wolffd@0:

wolffd@0: The dst argument is the destination image to which the wolffd@0: region will be copied. The src argument is the source wolffd@0: image from which the region is copied. The dstX wolffd@0: and dstY arguments specify the point in the destination wolffd@0: image to which the region will be copied. The srcX wolffd@0: and srcY arguments specify the upper left corner wolffd@0: of the region in the source image. The w wolffd@0: and h arguments specify the width and height wolffd@0: of the region. wolffd@0:

wolffd@0: When you copy a region from one location in an image to another wolffd@0: location in the same image, gdImageCopy will perform as expected wolffd@0: unless the regions overlap, in which case the result is wolffd@0: unpredictable. wolffd@0:

wolffd@0: Important note on copying between images: since wolffd@0: different images do wolffd@0: not necessarily have the same color tables, pixels are not simply set to the wolffd@0: same color index values to copy them. gdImageCopy will attempt wolffd@0: to find an identical RGB value in the destination image for wolffd@0: each pixel in the copied portion of the source image by wolffd@0: invoking gdImageColorExact. If wolffd@0: such a value is not found, gdImageCopy will attempt to wolffd@0: allocate colors as needed using wolffd@0: gdImageColorAllocate. If both of these methods fail, wolffd@0: gdImageCopy will invoke wolffd@0: gdImageColorClosest to find the color in the destination wolffd@0: image which most closely approximates the color of the wolffd@0: pixel being copied. wolffd@0:

wolffd@0: ... Inside a function ...
wolffd@0: gdImagePtr im_in;
wolffd@0: gdImagePtr im_out;
wolffd@0: int x, y;
wolffd@0: FILE *in;
wolffd@0: FILE *out;
wolffd@0: /* Load a small png to tile the larger one with */
wolffd@0: in = fopen("small.png", "rb");
wolffd@0: im_in = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Make the output image four times as large on both axes */
wolffd@0: im_out = gdImageCreate(im_in->sx * 4, im_in->sy * 4);
wolffd@0: /* Now tile the larger image using the smaller one */
wolffd@0: for (y = 0; (y < 4); y++) {
wolffd@0:   for (x = 0; (x < 4); x++) {
wolffd@0:     gdImageCopy(im_out, im_in,
wolffd@0:       x * im_in->sx, y * im_in->sy,
wolffd@0:       0, 0,
wolffd@0:       im_in->sx, im_in->sy);
wolffd@0:   }
wolffd@0: }
wolffd@0: out = fopen("tiled.png", "wb");
wolffd@0: gdImagePng(im_out, out);
wolffd@0: fclose(out);
wolffd@0: gdImageDestroy(im_in);
wolffd@0: gdImageDestroy(im_out);
wolffd@0: 
wolffd@0:
void gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int destW, int destH, int srcW, int srcH) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCopyResized is used to copy a rectangular portion of one image to wolffd@0: another image. The X and Y dimensions of the original region and the wolffd@0: destination region can vary, resulting in stretching or shrinking of wolffd@0: the region as appropriate. (For a simpler version of this function wolffd@0: which does not deal with resizing, see wolffd@0: gdImageCopy.) wolffd@0:

wolffd@0: The dst argument is the destination image to which the wolffd@0: region will be copied. The src argument is the source wolffd@0: image from which the region is copied. The dstX wolffd@0: and dstY arguments specify the point in the destination wolffd@0: image to which the region will be copied. The srcX wolffd@0: and srcY arguments specify the upper left corner wolffd@0: of the region in the source image. The dstW wolffd@0: and dstH arguments specify the width and height wolffd@0: of the destination region. The srcW wolffd@0: and srcH arguments specify the width and height wolffd@0: of the source region and can differ from the destination size, wolffd@0: allowing a region to be scaled during the copying process. wolffd@0:

wolffd@0: When you copy a region from one location in an image to another wolffd@0: location in the same image, gdImageCopy will perform as expected wolffd@0: unless the regions overlap, in which case the result is wolffd@0: unpredictable. If this presents a problem, create a scratch image wolffd@0: in which to keep intermediate results. wolffd@0:

wolffd@0: Important note on copying between images: since images wolffd@0: do not necessarily have the same color tables, pixels are not simply set wolffd@0: to the same color index values to copy them. gdImageCopy will attempt wolffd@0: to find an identical RGB value in the destination image for wolffd@0: each pixel in the copied portion of the source image by wolffd@0: invoking gdImageColorExact. If wolffd@0: such a value is not found, gdImageCopy will attempt to wolffd@0: allocate colors as needed using wolffd@0: gdImageColorAllocate. If both of these methods fail, wolffd@0: gdImageCopy will invoke wolffd@0: gdImageColorClosest to find the color in the destination wolffd@0: image which most closely approximates the color of the wolffd@0: pixel being copied. wolffd@0:

wolffd@0: ... Inside a function ...
wolffd@0: gdImagePtr im_in;
wolffd@0: gdImagePtr im_out;
wolffd@0: int x, y;
wolffd@0: FILE *in;
wolffd@0: FILE *out;
wolffd@0: /* Load a small png to expand in the larger one */
wolffd@0: in = fopen("small.png", "rb");
wolffd@0: im_in = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Make the output image four times as large on both axes */
wolffd@0: im_out = gdImageCreate(im_in->sx * 4, im_in->sy * 4);
wolffd@0: /* Now copy the smaller image, but four times larger */
wolffd@0: gdImageCopyResized(im_out, im_in, 0, 0, 0, 0,
wolffd@0:   im_out->sx, im_out->sy,
wolffd@0:   im_in->sx, im_in->sy);  
wolffd@0: out = fopen("large.png", "wb");
wolffd@0: gdImagePng(im_out, out);
wolffd@0: fclose(out);
wolffd@0: gdImageDestroy(im_in);
wolffd@0: gdImageDestroy(im_out);
wolffd@0: 
wolffd@0:
void gdImageCopyResampled(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int destW, int destH, int srcW, int srcH) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCopyResampled is used to copy a rectangular portion of one image to wolffd@0: another image, smoothly interpolating pixel values so that, in particular, wolffd@0: reducing the size of an image still retains a great deal of clarity. The wolffd@0: X and Y dimensions of the original region and the wolffd@0: destination region can vary, resulting in stretching or shrinking of wolffd@0: the region as appropriate. (For a simpler version of this function wolffd@0: which does not deal with resizing, see wolffd@0: gdImageCopy. For a version which does not interpolate pixel values, wolffd@0: see gdImageCopyResized. wolffd@0:

wolffd@0: Pixel values are only interpolated if the destination image is a wolffd@0: truecolor image. Otherwise, wolffd@0: gdImageCopyResized is wolffd@0: automatically invoked. wolffd@0:

wolffd@0: The dst argument is the destination image to which the wolffd@0: region will be copied. The src argument is the source wolffd@0: image from which the region is copied. The dstX wolffd@0: and dstY arguments specify the point in the destination wolffd@0: image to which the region will be copied. The srcX wolffd@0: and srcY arguments specify the upper left corner wolffd@0: of the region in the source image. The dstW wolffd@0: and dstH arguments specify the width and height wolffd@0: of the destination region. The srcW wolffd@0: and srcH arguments specify the width and height wolffd@0: of the source region and can differ from the destination size, wolffd@0: allowing a region to be scaled during the copying process. wolffd@0:

wolffd@0: When you copy a region from one location in an image to another wolffd@0: location in the same image, gdImageCopy will perform as expected wolffd@0: unless the regions overlap, in which case the result is wolffd@0: unpredictable. If this presents a problem, create a scratch image wolffd@0: in which to keep intermediate results. wolffd@0:

wolffd@0: Important note on copying between images: since images wolffd@0: do not necessarily have the same color tables, pixels are not simply set wolffd@0: to the same color index values to copy them. If the destination image wolffd@0: is a palette image, gd will use the wolffd@0: gdImageColorResolve function to wolffd@0: determine the best color available. wolffd@0:

wolffd@0: ... Inside a function ...
wolffd@0: gdImagePtr im_in;
wolffd@0: gdImagePtr im_out;
wolffd@0: int x, y;
wolffd@0: FILE *in;
wolffd@0: FILE *out;
wolffd@0: /* Load a large png to shrink in the smaller one */
wolffd@0: in = fopen("large.png", "rb");
wolffd@0: im_in = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Make the output image four times as small on both axes. Use
wolffd@0:   a true color image so that we can interpolate colors. */
wolffd@0: im_out = gdImageCreateTrueColor(im_in->sx / 4, im_in->sy / 4);
wolffd@0: /* Now copy the large image, but four times smaller */
wolffd@0: gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0,
wolffd@0:   im_out->sx, im_out->sy,
wolffd@0:   im_in->sx, im_in->sy);  
wolffd@0: out = fopen("large.png", "wb");
wolffd@0: gdImagePng(im_out, out);
wolffd@0: fclose(out);
wolffd@0: gdImageDestroy(im_in);
wolffd@0: gdImageDestroy(im_out);
wolffd@0: 
wolffd@0:
void gdImageCopyRotated(gdImagePtr dst, gdImagePtr src, double dstX, double dstY, int srcX, int srcY, int srcW, int srcH, int angle) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCopyRotated is used to copy a rectangular portion of one image to wolffd@0: another image, or to another region of the same image. The srcX and wolffd@0: srcY coordinates specify the upper left corner of the source area; however, wolffd@0: the dstX and dstY coordinates specify the CENTER of the destination area. wolffd@0: This important distinction is made because the rotated rectangle may wolffd@0: may or may not be parallel to the X and Y axes. The destination coordinates wolffd@0: may be floating point, as the center of the desired destination area may lie wolffd@0: at the center of a pixel (0.5 pixels) rather than its upper left corner. wolffd@0: The angle specified is an integer number of degrees, between 0 and 360, wolffd@0: with 0 degrees causing no change, and counterclockwise rotation as wolffd@0: the angle increases. wolffd@0:

wolffd@0: When you copy a region from one location in an image to another wolffd@0: location in the same image, gdImageCopyRotated will perform as expected wolffd@0: unless the regions overlap, in which case the result is wolffd@0: unpredictable. If this presents a problem, create a scratch image wolffd@0: in which to keep intermediate results. wolffd@0:

wolffd@0: Important note on copying between images: since wolffd@0: palette-based images do not necessarily have the same color tables, pixels wolffd@0: are not simply set to the same color index values to copy them. wolffd@0: If the destination image is not a truecolor image, wolffd@0: gdImageColorResolveAlpha is wolffd@0: used to choose the destination pixel. wolffd@0:

wolffd@0: ... Inside a function ...
wolffd@0: gdImagePtr im_in;
wolffd@0: gdImagePtr im_out;
wolffd@0: int x, y;
wolffd@0: int a;
wolffd@0: FILE *in;
wolffd@0: FILE *out;
wolffd@0: /* Load a small png to rotate in the larger one */
wolffd@0: in = fopen("small.png", "rb");
wolffd@0: im_in = gdImageCreateFromPng(in);
wolffd@0: fclose(in);
wolffd@0: /* Make the output image four times as large on both axes */
wolffd@0: im_out = gdImageCreate(im_in->sx * 4, im_in->sy * 4);
wolffd@0: /* Now rotate the smaller image */
wolffd@0: for (a = 0; (a < 360); a += 45) {
wolffd@0:         double x = cos(a * .0174532925) * gdImageSX(im_out) / 2;
wolffd@0:         double y = -sin(a * .0174532925) * gdImageSY(im_out) / 2;
wolffd@0:   gdImageCopyRotated(im_out, im_in, 
wolffd@0:                 gdImageSX(im_out) / 2 + x, 
wolffd@0:                 gdImageSY(im_out) / 2 + y, 
wolffd@0:     0, 0,
wolffd@0:                 gdImageSX(im_in),
wolffd@0:                 gdImageSY(im_in),
wolffd@0:                 a); 
wolffd@0: }
wolffd@0: out = fopen("large.png", "wb");
wolffd@0: gdImagePng(im_out, out);
wolffd@0: fclose(out);
wolffd@0: gdImageDestroy(im_in);
wolffd@0: gdImageDestroy(im_out);
wolffd@0: 
wolffd@0: wolffd@0:
void gdImageCopyMerge(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h, int pct) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCopyMerge is almost identical to gdImageCopy, except that wolffd@0: it 'merges' the two images by an amount specified in the last parameter. If the last wolffd@0: parameter is 100, then it will function identically to gdImageCopy - the source image replaces wolffd@0: the pixels in the destination. wolffd@0:

wolffd@0: If, however, the pct parameter is less than 100, then the two images are merged. wolffd@0: With pct = 0, no action is taken. wolffd@0:

This feature is most useful to 'highlight' sections of an image by merging a solid color with wolffd@0: pct = 50: wolffd@0:

wolffd@0: ... Inside a function ...
wolffd@0: gdImageCopyMerge(im_out, im_in, 100, 200, 0, 0, 30, 50, 50);
wolffd@0: 
wolffd@0: wolffd@0:
void gdImageCopyMergeGray(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int wolffd@0: w, int h, int pct) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCopyMergeGray is almost identical to gdImageCopyMerge, wolffd@0: except that when merging images it preserves the hue of the source by converting the destination wolffd@0: pixels to grey scale before the copy operation. wolffd@0:
wolffd@0: ... Inside a function ...
wolffd@0: gdImageCopyMergeGray(im_out, im_in, 100, 200, 0, 0, 30, 50, 50);
wolffd@0: 
wolffd@0: wolffd@0:
void gdImagePaletteCopy(gdImagePtr dst, gdImagePtr src) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Copies a palette from one image to another, attempting to match the colors in the target image wolffd@0: to the colors wolffd@0: in the source palette. wolffd@0:
void gdImageSquareToCircle(gdImagePtr im, int radius) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: im MUST be square, but can have any size. Returns a new image wolffd@0: of width and height radius * 2, in which the X axis of wolffd@0: the original has been remapped to theta (angle) and the Y axis wolffd@0: of the original has been remapped to rho (distance from center). wolffd@0: This is known as a "polar coordinate transform." wolffd@0: See also gdImageStringFTCircle, which wolffd@0: uses this function internally. wolffd@0:
void gdImageSharpen(gdImagePtr im, int pct) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: Sharpens the specified image. pct is a sharpening percentage, and wolffd@0: can be greater than 100. Silently does nothing to non-truecolor images. wolffd@0: Silently does nothing for pct<0. Transparency/alpha channel are not wolffd@0: altered. wolffd@0:
wolffd@0:

Miscellaneous Functions

wolffd@0:
wolffd@0: wolffd@0:
int gdImageCompare(gdImagePtr im1, gdImagePtr im2) wolffd@0: (FUNCTION) wolffd@0:
wolffd@0: gdImageCompare returns a bitmap indicating if the two images are different. The members of the wolffd@0: bitmap are defined in gd.h, but the most important is GD_CMP_IMAGE, which indicated that the images wolffd@0: will actually appear different when displayed. Other, less important, differences relate to pallette wolffd@0: entries. Any difference in the transparent colour is assumed to make images display differently, wolffd@0: even if the transparent colour is not used. wolffd@0:
wolffd@0: ... Inside a function ...
wolffd@0: cmpMask = gdImageCompare(im1, im2);
wolffd@0: 
wolffd@0: wolffd@0:
gdImageInterlace(gdImagePtr im, int interlace) (FUNCTION) wolffd@0:
wolffd@0: gdImageInterlace is used to determine whether an image should be stored wolffd@0: in a linear fashion, in which lines will appear on the display from wolffd@0: first to last, or in an interlaced fashion, in which the image wolffd@0: will "fade in" over several passes. By default, images are not wolffd@0: interlaced. (When writing JPEG images, interlacing implies generating wolffd@0: progressive JPEG files, which are represented as a series of scans of wolffd@0: increasing quality. Noninterlaced gd images result in regular wolffd@0: [sequential] JPEG data streams.) wolffd@0:

wolffd@0: A nonzero value for the interlace argument turns on interlace; wolffd@0: a zero value turns it off. Note that interlace has no effect wolffd@0: on other functions, and has no meaning unless you save the wolffd@0: image in PNG or JPEG format; the gd and xbm formats do not support wolffd@0: interlace. wolffd@0:

wolffd@0: When a PNG is loaded with wolffd@0: gdImageCreateFromPng or a JPEG is wolffd@0: loaded with wolffd@0: gdImageCreateFromJpeg, interlace wolffd@0: will be set according to the setting in the PNG or JPEG file. wolffd@0:

wolffd@0: Note that many PNG and JPEG viewers and web browsers do not wolffd@0: support interlace or the incremental display of progressive wolffd@0: JPEGs. However, the interlaced PNG or progressive JPEG should still wolffd@0: display; it will simply appear all at once, just as other images do. wolffd@0:

wolffd@0: gdImagePtr im;
wolffd@0: FILE *out;
wolffd@0: /* ... Create or load the image... */
wolffd@0: 
wolffd@0: /* Now turn on interlace */
wolffd@0: gdImageInterlace(im, 1);
wolffd@0: /* And open an output file */
wolffd@0: out = fopen("test.png", "wb");
wolffd@0: /* And save the image  -- could also use gdImageJpeg */
wolffd@0: gdImagePng(im, out);
wolffd@0: fclose(out);
wolffd@0: gdImageDestroy(im);
wolffd@0: 
wolffd@0:
gdFree(void *ptr) (FUNCTION) wolffd@0:
wolffd@0: gdFree provides a reliable way to free memory allocated by functions wolffd@0: such as gdImagePngPtr which return wolffd@0: blocks of memory. Use of this function guarantees that the wolffd@0: version of free() that is ultimately called will wolffd@0: be intended for use with the version of malloc() that wolffd@0: originally allocated the block. wolffd@0:
wolffd@0:

Constants

wolffd@0:
wolffd@0:
gdAntiAliased (CONSTANT) wolffd@0:
wolffd@0: Used in place of a color when invoking a line-drawing wolffd@0: function such as gdImageLine wolffd@0: or gdImageRectangle. wolffd@0: When gdAntiAliased is used as the color, the foreground color wolffd@0: set with gdImageSetAntiAliased wolffd@0: is used, with antialiasing mechanisms to minimize any wolffd@0: "jagged" appearance. wolffd@0: For more information, see wolffd@0: gdImageSetAntiAliased. wolffd@0:
gdBrushed (CONSTANT) wolffd@0:
wolffd@0: Used in place of a color when invoking a line-drawing wolffd@0: function such as gdImageLine wolffd@0: or gdImageRectangle. wolffd@0: When gdBrushed is used as the color, the brush wolffd@0: image set with gdImageSetBrush wolffd@0: is drawn in place of each pixel of the line (the brush is wolffd@0: usually larger than one pixel, creating the effect wolffd@0: of a wide paintbrush). See also wolffd@0: gdStyledBrushed for a way wolffd@0: to draw broken lines with a series of distinct copies of an image. wolffd@0:
gdMaxColors(CONSTANT) wolffd@0:
wolffd@0: The constant 256. This is the maximum number of colors in a palette-based wolffd@0: PNG file according to the PNG standard, and is also the maximum number of wolffd@0: colors in a palette-based gd image. This of course does not apply to wolffd@0: truecolor images. wolffd@0:
gdStyled (CONSTANT) wolffd@0:
wolffd@0: Used in place of a color when invoking a line-drawing wolffd@0: function such as gdImageLine wolffd@0: or gdImageRectangle. wolffd@0: When gdStyled is used as the color, the colors of the pixels are wolffd@0: drawn successively from the style that has been wolffd@0: set with gdImageSetStyle. wolffd@0: If the color of a pixel is equal to wolffd@0: gdTransparent, that pixel wolffd@0: is not altered. (This mechanism is completely unrelated wolffd@0: to the "transparent color" of the image itself; see wolffd@0: gdImageColorTransparent wolffd@0: gdImageColorTransparent for that mechanism.) See also wolffd@0: gdStyledBrushed. wolffd@0:
gdStyledBrushed (CONSTANT) wolffd@0:
wolffd@0: Used in place of a color when invoking a line-drawing wolffd@0: function such as gdImageLine wolffd@0: or gdImageRectangle. wolffd@0: When gdStyledBrushed is used as the color, the brush wolffd@0: image set with gdImageSetBrush wolffd@0: is drawn at each pixel of the line, providing that the wolffd@0: style set with gdImageSetStyle wolffd@0: contains a nonzero value (OR gdTransparent, which wolffd@0: does not equal zero but is supported for consistency) wolffd@0: for the current pixel. (Pixels are drawn successively from the style as the wolffd@0: line is drawn, returning to the beginning when the wolffd@0: available pixels in the style are exhausted.) Note that wolffd@0: this differs from the behavior of gdStyled, wolffd@0: in which the values in the style are used as actual wolffd@0: pixel colors, except for gdTransparent. wolffd@0:
gdDashSize (CONSTANT) wolffd@0:
wolffd@0: The length of a dash in a dashed line. Defined to be 4 for wolffd@0: backwards compatibility with programs that use wolffd@0: gdImageDashedLine. New wolffd@0: programs should use wolffd@0: gdImageSetStyle and call the standard wolffd@0: gdImageLine function wolffd@0: with the special "color" wolffd@0: gdStyled or gdStyledBrushed. wolffd@0:
gdTiled (CONSTANT) wolffd@0:
wolffd@0: Used in place of a normal color in wolffd@0: gdImageFilledRectangle, wolffd@0: gdImageFilledPolygon, wolffd@0: gdImageFill, and wolffd@0: gdImageFillToBorder. gdTiled selects a pixel from the wolffd@0: tile image set with gdImageSetTile wolffd@0: in such a way as to ensure that the filled area will be wolffd@0: tiled with copies of the tile image. See the discussions of wolffd@0: gdImageFill and wolffd@0: gdImageFillToBorder for special wolffd@0: restrictions regarding those functions. wolffd@0:
gdTransparent (CONSTANT) wolffd@0:
wolffd@0: Used in place of a normal color in a style to be set with wolffd@0: gdImageSetStyle. wolffd@0: gdTransparent is not the transparent wolffd@0: color index of the image; for that functionality please wolffd@0: see gdImageColorTransparent. wolffd@0:
wolffd@0: wolffd@0:

About the additional .gd image file format

wolffd@0: In addition to reading and writing the PNG and JPEG formats and reading the wolffd@0: X Bitmap format, gd has the capability to read and write its wolffd@0: own ".gd" format. This format is not intended for wolffd@0: general purpose use and should never be used to distribute wolffd@0: images. It is not a compressed format. Its purpose is solely to wolffd@0: allow very fast loading of images your program needs often in wolffd@0: order to build other images for output. If you are experiencing wolffd@0: performance problems when loading large, fixed PNG images your wolffd@0: program needs to produce its output images, you may wish wolffd@0: to examine the functions wolffd@0: gdImageCreateFromGd and gdImageGd, wolffd@0: which read and write .gd format images. wolffd@0: wolffd@0:

wolffd@0: The program "pngtogd.c" is provided as a simple way of converting wolffd@0: .png files to .gd format. I emphasize again that you will not wolffd@0: need to use this format unless you have a need for high-speed loading wolffd@0: of a few frequently-used images in your program. wolffd@0: wolffd@0:

About the .gd2 image file format

wolffd@0: In addition to reading and writing the PNG format and reading the wolffd@0: X Bitmap format, gd has the capability to read and write its wolffd@0: own ".gd2" format. This format is not intended for wolffd@0: general purpose use and should never be used to distribute wolffd@0: images. It is a compressed format allowing pseudo-random access wolffd@0: to large image files. Its purpose is solely to wolffd@0: allow very fast loading of parts of images wolffd@0: If you are experiencing wolffd@0: performance problems when loading large, fixed PNG or JPEG images your wolffd@0: program needs to produce its output images, you may wish wolffd@0: to examine the functions wolffd@0: gdImageCreateFromGd2, wolffd@0: gdImageCreateFromGd2Part and gdImageGd2, wolffd@0: which read and write .gd2 format images. wolffd@0: wolffd@0:

wolffd@0: The program "pngtogd2.c" is provided as a simple way of converting wolffd@0: .png files to .gd2 format. wolffd@0: wolffd@0:

About the gdIOCtx structure

wolffd@0: Version 1.5 of GD added a new style of I/O based on an IOCtx wolffd@0: structure (the most up-to-date version can be found in gd_io.h): wolffd@0:
wolffd@0: typedef struct gdIOCtx {
wolffd@0:         int     (*getC)(struct gdIOCtx*);
wolffd@0:         int     (*getBuf)(struct gdIOCtx*, void*, int);
wolffd@0: 
wolffd@0:         void     (*putC)(struct gdIOCtx*, int);
wolffd@0:         int     (*putBuf)(struct gdIOCtx*, const void*, int);
wolffd@0: 
wolffd@0:         int     (*seek)(struct gdIOCtx*, const int); /* Returns 1 on SUCCESS */
wolffd@0:         long    (*tell)(struct gdIOCtx*); 
wolffd@0: 
wolffd@0:         void    (*free)(struct gdIOCtx*);
wolffd@0: 
wolffd@0: } gdIOCtx;
wolffd@0: 
wolffd@0: wolffd@0: Most functions that accepted files in previous versions now also have a wolffd@0: counterpart that accepts an I/O context. These functions have a 'Ctx' wolffd@0: suffix. wolffd@0:

wolffd@0: The Ctx routines use the function pointers in the I/O context pointed to wolffd@0: by gdIOCtx to perform all I/O. Examples of how to implement an I/O context wolffd@0: can be found in io_file.c (which provides a wrapper for file routines), and wolffd@0: io_dp.c (which implements in-memory storage). wolffd@0:

wolffd@0: It is not necessary to implement all functions in an I/O context if you know wolffd@0: that it will only be used in limited cirsumstances. At the time of writing wolffd@0: (Version 1.6.1, July 1999), the known requirements are: wolffd@0:

wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0:
All Must have 'free',
Anything that reads from the contextMust have 'getC' and 'getBuf',
Anything that writes to the contextMust have 'putC' and 'putBuf'.
If gdCreateFromGd2Part is calledMust also have 'seek' and 'tell'. Note: seek must return 1 on SUCCESS and 0 on FAILURE.
If gdImageGd2 is calledMust also have 'seek' and 'tell'.
wolffd@0: wolffd@0: wolffd@0: wolffd@0:

Please tell us you're using gd!

wolffd@0: When you contact us and let us know you are using gd, wolffd@0: you help us justify the time spent in maintaining and improving wolffd@0: it. So please let us know. If the results are publicly wolffd@0: visible on the web, a URL is a wonderful thing to receive, but wolffd@0: if it's not a publicly visible project, a simple note is just wolffd@0: as welcome. wolffd@0: wolffd@0:

How do I get support?

wolffd@0:

Free Support

wolffd@0: wolffd@0: Anyone can mail questions about the gd library using the wolffd@0: LibGD support. However, wolffd@0: we receive a very large volume of email on many subjects, and while we do wolffd@0: our best to respond to all queries this can take some time. Sometimes wolffd@0: the response must take the form of an eventual new release or wolffd@0: an addition to a FAQ or other document, as opposed to an detailed individual response. wolffd@0: wolffd@0:

Hourly Support

wolffd@0: Those requiring support in detail may arrange for direct support wolffd@0: from the maintaines, at the rate of $50/hr, billed wolffd@0: directly by credit card. Purchase orders are also accepted from wolffd@0: Fortune 500 corporations and institutions in good standing. wolffd@0: To make arrangements, contact Pierre-A. Joye. To avoid delay wolffd@0: and/or confusion, be sure to specifically mention that you wish to purchase gd support at the wolffd@0: hourly rate above. wolffd@0: wolffd@0:

How do I report issues, bugs or features request?

wolffd@0: Bugs, feature requests or other issues can be reported using the wolffd@0: libGD.org issues tracker. as well as using wolffd@0: one of our support channels: LibGD support wolffd@0: wolffd@0:

Alphabetical quick index

wolffd@0: gdAntiAliased | wolffd@0: gdBrushed | wolffd@0: gdDashSize | wolffd@0: gdFont | wolffd@0: gdFontGetHuge | wolffd@0: gdFontGetLarge | wolffd@0: gdFontGetMediumBold | wolffd@0: gdFontGetSmall | wolffd@0: gdFontGetTiny | wolffd@0: gdFontCacheSetup | wolffd@0: gdFontCacheShutdown | wolffd@0: gdFontPtr | wolffd@0: gdFree | wolffd@0: gdImage | wolffd@0: gdImageAlphaBlending | wolffd@0: gdImageArc | wolffd@0: gdImageBlue | wolffd@0: gdImageBoundsSafe | wolffd@0: gdImageChar | wolffd@0: gdImageCharUp | wolffd@0: gdImageColorAllocate | wolffd@0: gdImageColorAllocateAlpha | wolffd@0: gdImageColorClosest | wolffd@0: gdImageColorClosestAlpha | wolffd@0: gdImageColorClosestHWB | wolffd@0: gdImageColorDeallocate | wolffd@0: gdImageColorExact | wolffd@0: gdImageColorExactAlpha | wolffd@0: gdImageColorResolve | wolffd@0: gdImageColorResolveAlpha | wolffd@0: gdImageColorTransparent | wolffd@0: gdImageCopy | wolffd@0: gdImageCopyMerge | wolffd@0: gdImageMergeGray | wolffd@0: gdImageCopyResized | wolffd@0: gdImageCopyResampled | wolffd@0: gdImageCopyRotated | wolffd@0: gdImageCreate | wolffd@0: gdImageCreatePalette | wolffd@0: gdImageCreateTrueColor | wolffd@0: gdImageCreateFromGd | wolffd@0: gdImageCreateFromGdCtx | wolffd@0: gdImageCreateFromGdPtr | wolffd@0: gdImageCreateFromGd2 | wolffd@0: gdImageCreateFromGd2Ctx | wolffd@0: gdImageCreateFromGd2Ptr | wolffd@0: gdImageCreateFromGd2Part | wolffd@0: gdImageCreateFromGd2PartCtx | wolffd@0: gdImageCreateFromGd2PartPtr | wolffd@0: gdImageCreateFromJpeg | wolffd@0: gdImageCreateFromJpegCtx | wolffd@0: gdImageCreateFromJpegPtr | wolffd@0: gdImageCreateFromPng | wolffd@0: gdImageCreateFromPngCtx | wolffd@0: gdImageCreateFromPngPtr | wolffd@0: gdImageCreateFromPngSource | wolffd@0: gdImageCreateFromWBMP | wolffd@0: gdImageCreateFromWBMPCtx | wolffd@0: gdImageCreateFromWBMPPtr | wolffd@0: gdImageCreateFromXbm | wolffd@0: gdImageCreateFromXpm | wolffd@0: gdImageDashedLine | wolffd@0: gdImageDestroy | wolffd@0: gdImageFill | wolffd@0: gdImageFilledArc | wolffd@0: gdImageFilledEllipse | wolffd@0: gdImageFillToBorder | wolffd@0: gdImageFilledRectangle | wolffd@0: gdImageGd | wolffd@0: gdImageGd2 | wolffd@0: gdImageGetInterlaced | wolffd@0: gdImageGetPixel | wolffd@0: gdImageGetTransparent | wolffd@0: gdImageGifAnimAdd | wolffd@0: gdImageGifAnimAddCtx | wolffd@0: gdImageGifAnimAddPtr | wolffd@0: gdImageGifAnimBegin | wolffd@0: gdImageGifAnimBeginCtx | wolffd@0: gdImageGifAnimBeginPtr | wolffd@0: gdImageGifAnimEnd | wolffd@0: gdImageGifAnimEndCtx | wolffd@0: gdImageGifAnimEndPtr | wolffd@0: gdImageGreen | wolffd@0: gdImageInterlace | wolffd@0: gdImageJpeg | wolffd@0: gdImageJpegCtx | wolffd@0: gdImageLine | wolffd@0: gdImageFilledPolygon | wolffd@0: gdImageOpenPolygon | wolffd@0: gdImagePaletteCopy | wolffd@0: gdImagePng | wolffd@0: gdImagePngEx | wolffd@0: gdImagePngCtx | wolffd@0: gdImagePngCtxEx | wolffd@0: gdImagePngPtr | wolffd@0: gdImagePngPtrEx | wolffd@0: gdImagePngToSink | wolffd@0: gdImagePolygon | wolffd@0: gdImagePtr | wolffd@0: gdImageWBMP | wolffd@0: gdImageWBMPCtx | wolffd@0: gdImageRectangle | wolffd@0: gdImageRed | wolffd@0: gdImageSaveAlpha | wolffd@0: gdImageSetAntiAliased | wolffd@0: gdImageSetAntiAliasedDontBlend | wolffd@0: gdImageSetBrush | wolffd@0: gdImageSetPixel | wolffd@0: gdImageSetStyle | wolffd@0: gdImageSetThickness | wolffd@0: gdImageSetTile | wolffd@0: gdImageSharpen | wolffd@0: gdImageSquareToCircle | wolffd@0: gdImageString | wolffd@0: gdImageString16 | wolffd@0: gdImageStringFT | wolffd@0: gdImageStringFTCircle | wolffd@0: gdImageStringFTEx | wolffd@0: gdImageStringTTF | wolffd@0: gdImageStringUp | wolffd@0: gdImageStringUp16 | wolffd@0: gdImageToPalette | wolffd@0: gdImageWBMP | wolffd@0: gdMaxColors | wolffd@0: gdPoint | wolffd@0: gdStyled | wolffd@0: gdStyledBrushed | wolffd@0: gdTiled | wolffd@0: gdTransparent wolffd@0:

wolffd@0: wolffd@0: www.libgd.org wolffd@0: wolffd@0: