LibGD – Dynamic Image Creation Library

libGD - Graphics Library

LibGD is an open source Graphics library for the creation of images dynamically by programmers. GD is written in C, and “wrappers” are available for Perl, PHP and other languages. 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.

It allows to quickly draw images complete with lines, arcs, text, multiple colors, cut and paste from other images, and flood fills, and write out the result as a PNG or JPEG file.

To use gd in your program, include the file gd.h, and link with the gd library and the other required libraries; the syntax for most Unix flavors is:

-lgd -lpng -lz -ljpeg -lfreetype -lm

GD is written in C Programming Language and various binding are available for different languages like C#, clip. D, Javascript, Lua, Pearl, PHP, Pascal, Python and ruby. The library was originally developed by Thomas Boutell and is now maintained by Pierre-A. Joye under the umbrella of PHP.net.

Home Page: Dynamic Creation of Images

Download: LibGD Releases

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

/* Bring in gd library functions */#include "gd.h"

/* Bring in standard I/O so we can output the PNG to a file */#include <stdio.h>

int main() {
  /* Declare the image */  gdImagePtr im;
  /* Declare output files */  FILE *pngout, *jpegout;
  /* Declare color indexes */  int black;
  int white;

  /* Allocate the image: 64 pixels across by 64 pixels tall */  im = gdImageCreate(64, 64);

  /* Allocate the color black (red, green and blue all minimum).
    Since this is the first color in a new image, it will
    be the background color. */  black = gdImageColorAllocate(im, 0, 0, 0);

  /* Allocate the color white (red, green and blue all maximum). */  white = gdImageColorAllocate(im, 255, 255, 255);

  /* Draw a line from the upper left to the lower right,
    using white color index. */  gdImageLine(im, 0, 0, 63, 63, white);

  /* Open a file for writing. "wb" means "write binary", important
    under MSDOS, harmless under Unix. */  pngout = fopen("test.png", "wb");

  /* Do the same for a JPEG-format file. */  jpegout = fopen("test.jpg", "wb");

  /* Output the image to the disk file in PNG format. */  gdImagePng(im, pngout);

  /* Output the same image in JPEG format, using the default
    JPEG quality setting. */  gdImageJpeg(im, jpegout, -1);

  /* Close the files. */  fclose(pngout);
  fclose(jpegout);

  /* Destroy the image in memory. */  gdImageDestroy(im);
}

 

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post