Applications/Libgd
From HPC
Contents
Application Details
- Description: GD is an open source code library for the dynamic creation of images by programmers. GD is written in C, and "wrappers" are available for Perl, PHP and other languages. GD creates PNG, JPEG, GIF, WebP, XPM, BMP images, among other formats. GD is commonly used to generate charts, graphics, thumbnails, and most anything else, on the fly. While not restricted to use on the web, the most common applications of GD involve website development.
- Version: 2.2.3 (compiled with gcc)
- Module: gcc/2.2.3
- Licence: GNU
Usage Examples
Module
[username@login01 ~]$ module add libgd/gcc/2.2.3
Compilation
'Libgd is a library for use either with a compiled binary that requires it, or it could be required at compilation time also.
A typical code snippet requiring is shown below:
#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); }
Compilation is carried out below, in this example the gcc compiler is used. However, the applies similarly to the Intel compiler too.
gcc -o savePNG savePNG.c -lm -lgd
Limitations
- Very large images can cause gd to run out of memory. And sometimes the image file itself isn't terribly large— consider a JPEG of a completely blank field, 8,000 pixels on a side: the file compresses well but representing it in memory as a bitmap is impractical. If you are coding in PHP, you can check for this situation with the getimagesize function, which determines the image dimensions without using gd. This is possible because the popular image formats all store the image dimensions near the beginning of the file where they are easily accessible. Perl programmers can use the similar Image::Size CPAN module. If you are not using PHP or Perl and your language of choice does not offer a similar feature, you can implement the technique yourself. See the GIF specification, the JPEG specification, and the PNG specification.