FAQ
From LibGD
Please Read Before Inquiring
Bindings FAQ
Do you like to maintain a language binder FAQ? Please contact us.
What does "gd" stand for?
In gd 1.0, it stood for "gif draw." After the Unisys patent on the LZW compression used in GIF came to light and GIF support was dropped, it did not officially stand for anything, but let's just say "graphics draw" and leave it at that. (GIF support is back, thanks to the expiration of the patent, but gd can draw much more than GIFs.)
Does gd support GIF images?
Yes. Support for GIF was restored in gd 2.0.28 on July 21st, 2004. Support for creating GIF animations is also available. Note that gdlib-config --features can be used to list the image formats supported by gd. Versions of gdlib-config prior to recent updates do not support the --features option, which can be understood to mean that GIF is not available.
How do I install gd on Fedora Linux?
Try:
yum install gd-devel
Note: this might not install the latest, most-cutting edge version of gd, depending on the version of Fedora you are running and how current their gd packages are at the moment.
How do I get gd to work with PHP?
I have troubles on OS X, how can I solve them?
MatÃas Giovannini maintains a very good OSX Howto here or DOC_INSTALL_OSX
Where can I get a compiled DLL of gd for Windows?
NOT INTENDED FOR PHP, SEE THE PHP FAQ.
A compiled DLL of gd with all features enabled is now available from the gd home page. To avoid conflicts the DLL is named bgd.dll. This is of course intended for programmers, but opens up the field to those who found compiling gd from scratch under Windows to be very challenging. bgd.dll includes png, jpeg, and freetype support as "standard equipment." A similar precompiled library for Linux is planned for the immediate future.
Where can I find an example of PHP programming with gd?
Why do my JPEG thumbnails have missing colors with gd 2.0.x?
When creating truecolor images, you need to call gdImageCreateTrueColor, not gdImageCreate. This used to "work" in gd 1.x, but only in the sense that gd 1.x had no support for real truecolor at all, so it faked it by dithering your image. gd 2.x supports real truecolor, and also has a better way of reducing to palette color when you really want to do that; see the gdImageTrueColorToPalette function -- but you don't need it for this job. PHP and Perl programmers: the function names for you are similar, just leave off the gd prefix. How come MRTG (or product XYZ) doesn't work right? I wrote gd; I can help you with that. I can't help you with someone else's product, even if it happens to use gd. First install gd according to the documentation, then read the documentation for the product that requires it, and contact its authors if need be.
Where do I get gd?
The latest version is available here.
I have added a cool feature. How do I submit the code for inclusion in gd?
I'd be very interested to see your patch, provided that you are willing to allow it to be released under the existing license terms; see the manual for more information. Please submit a feature request in our issues tracker.
Are you available to customize gd on a paid consulting basis?
Certainly. Please contact us for more information.
gd keeps saying it can't find png or jpeg support. I did install libpng and libjpeg. What am I missing?
If you use binary packages, you may install the "-devel" packages. For example, Ubuntu or Debian, it is called libgd2-dev. Refer to the documentaion of your distribution for further informations.
If you installed the png or jpeg library using the sources, be sure to do for libpng:
make install-headers
or for libjpeg:
make install-libin addition to
make install.
You may also need to use a command line like this one when configuring gd:
./configure '--with-jpeg=/usr/local' '--with-png=/usr/local' '--with-zlib-dir=/usr/local'
Often, users have installed these things in /usr/local/include and /usr/local/lib, but do not actually have those directories in their default include and library paths when configuring gd. Thanks to Santanu Misra and Alastair Battrick.
Why does bgd.dll crash with my C program?
You are trying to use gd's stdio library functions with a different C runtime library. Read on for notes on individual compilers.
Microsoft Visual C++: If you want to use gdImageCreateFromPng, gdImagePng, and other functions that take a FILE *, you MUST use Visual C++ 6.0 or earlier and you MUST link with the "multithreaded DLL" runtime library option. If you wish to use the .NET compiler, read the "Borland C++" section for an alternative method.
If you do not take the time to understand this, your program simply will not work! Note: the "multithreaded debug DLL" option is NOT the same thing and will NOT work. If you don't want to use the "multithreaded DLL" runtime library option (see "code generation" under "settings"), then read the "Borland C++" section for an alternative method.
GNU mingw32: you should have no problems. The mingw32 compiler is designed to link your application with msvcrt.dll, the same C runtime library that bgd.dll was linked with. (We build it with mingw32, as it happens.)
GNU cygwin: if you are compiling with -mno-cygwin, see the GNU mingw32 entry, above. If you are linking with the full cygwin runtime library, you might be better off installing gd from source. "configure" and "make install" should work for you as they do on full Unix systems. If you have solved the problem of using a win32 DLL from cygwin, however, you can certainly use bgd.dll using the same techniques described below for Borland C++.
Borland C++: this product apparently provides its own C runtime library, incompatible with Microsoft's msvcrt.dll. You cannot use the FILE * functions in gd.h. However, there is an alternative. The gdImagePngPtr and gdImageCreateFromPngPtr functions provide a way to write image file data to a memory buffer, or load an image from a buffer of image file data. See the next two questions for code samples. How do I load an image file from a buffer in memory? The following C code shows how to load an entire image file into a buffer in memory, then ask gd to read the image from that buffer. Please note that since you are responsible for allocating the buffer, You are also responsible for freeing the buffer with your normal memory management functions.
Note to Borland C++ programmers: this code will allow you to load an image using the bgd.dll library, because no FILE * objects are passed across the DLL boundary.
Of course, there is a gdImageCreateFromJpegPtr function available as well. This particular example loads a PNG image.
#include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> gdImagePtr myLoadPng(char *filename) { FILE *in; struct stat stat_buf; gdImagePtr im; in = fopen("myimage.png", "rb"); if (!in) { /* Error */ } if (fstat(fileno(in), &stat_buf) != 0) { /* Error */ } /* Read the entire thing into a buffer that we allocate */ char *buffer = malloc(stat_buf.st_size); if (!buffer) { /* Error */ } if (fread(buffer, 1, stat_buf.st_size, in) != stat_buf.st_size) { /* Error */ } im = gdImageCreateFromPngPtr( stat_buf.st_size, buffer); /* WE allocated the memory, WE free it with our normal free function */ free(buffer); fclose(in); return im; }
Why don't I have lib.exe?
The makemsvcimport.bat batch file will fail to produce bgd.dll if you are using the Microsoft Visual C++ Free Toolkit. Fortunately, a solution to this problem is available; an acceptable substitute can be created as a simple batch file that uses tools that are included in that package. See this highly useful page of the MaxDB wiki. How do I save an image to a buffer in memory? The following C code shows how to save a gd image to a memory buffer, and then write that buffer to a file on disk. You could also write it directly to stdout, preceded by a Content-type: image/png header and two CR/LF sequences, to directly return an image from a CGI program.
Note to Borland C++ programmers: this code will allow you to save an image using the bgd.dll library, because no FILE * objects are passed across the DLL boundary.
For your convenience, gd allocates the buffer for you; when you are done with it, you must call gdFree to release it.
Of course, there is a gdImageJpegPtr function available as well. This particular example saves a PNG image.
#include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> void mySavePng(char *filename, gdImagePtr im) { FILE *out; int size; char *data; out = fopen("filename, "wb"); if (!out) { /* Error */ } data = (char *) gdImagePngPtr(im, &size); if (!data) { /* Error */ } if (fwrite(data, 1, size, out) != size) { /* Error */ } if (fclose(out) != 0) { /* Error */ } gdFree(data); }
Why doesn't my gd 1.x program work well with gd 2.x?
There are two common reasons:
- You were using an ancient version of gd 1.x with GIF support, and now you are using a not-quite-new-enough version of gd 2.x without GIF support. Solution: get the latest gd 2.x which again supports GIF.
- You are trying to make thumbnails, or otherwise copy photographic-quality images like JPEG files. But you are creating the new image with gdImageCreate, which makes a palette-color image not suitable for photographs. Solution: use gdImageCreateTrueColor (new in gd 2.x), which creates a true-color image. This sort of "worked" in gd 1.x because gd 1.x did a quick and nasty job of converting JPEGs to palette color when reading them. But you will get much better results doing it the right way in gd 2.x. If you really want to, you can reduce to palette color after the copy using gdImageTrueColorToPalette.
