ImageCreation

From LibGD

Types Table of Contents Drawing Functions

Contents

gdImageCreate(sx, sy) (FUNCTION)

gdImageCreate is called to create palette-based images, with no more than 256 colors. Invoke gdImageCreate with the x and y dimensions of the desired image. gdImageCreate returns a gdImagePtr to the new image, or NULL if unable to allocate the image. The image must eventually be destroyed using gdImageDestroy().


  1. /* ... inside a function ...*/
  2. gdImagePtr im;
  3. im = gdImageCreate(64, 64);
  4. /* ... Use the image ... */
  5. gdImageDestroy(im);

gdImageCreateTrueColor(sx, sy) (FUNCTION)

gdImageCreateTrueColor is called to create truecolor images, with an essentially unlimited number of colors. Invoke gdImageCreateTrueColor with the x and y dimensions of the desired image. gdImageCreateTrueColor returns a gdImagePtr to the new image, or NULL if unable to allocate the image. The image must eventually be destroyed using gdImageDestroy().

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

  1. /* ... inside a function ...*/
  2. gdImagePtr im;
  3. im = gdImageCreateTrueColor(64, 64);
  4. /* ... Use the image ... */
  5. gdImageDestroy(im);

gdImageCreateFromJpeg(FILE *in) (FUNCTION)

gdImageCreateFromJpegPtr(int size, void *data) (FUNCTION)

gdImageCreateFromJpegCtx(gdIOCtx *in) (FUNCTION)

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

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

  1. gdImagePtr im;
  2. /* ... inside a function ... */
  3. FILE *in;
  4. in = fopen("myjpeg.jpg", "rb");
  5. im = gdImageCreateFromJpeg(in);
  6. fclose(in);
  7. /* ... Use the image ... */
  8. gdImageDestroy(im);

gdImageCreateFromPng(FILE *in) (FUNCTION)

gdImageCreateFromPngPtr(int size, void *data) (FUNCTION)

gdImageCreateFromPngCtx(gdIOCtx *in) (FUNCTION)

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

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

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

  1. gdImagePtr im;
  2. /* ... inside a function ... */
  3. FILE *in;
  4. in = fopen("mypng.png", "rb");
  5. im = gdImageCreateFromPng(in);
  6. fclose(in);
  7. /* ... Use the image ... */
  8. gdImageDestroy(im);

gdImageCreateFromPngSource(gdSourcePtr in) (FUNCTION)

Deprecated in favor of gdImageCreateFromPngCtx. Should not be used in new applications.

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

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

The example below implements gdImageCreateFromPng by creating a custom data source and invoking gdImageCreateFromPngSource.


  1. static int freadWrapper(void *context, char *buf, int len);
  2.  
  3. gdImagePtr gdImageCreateFromPng(FILE *in)
  4. {
  5. gdSource s;
  6. s.source = freadWrapper;
  7. s.context = in;
  8. return gdImageCreateFromPngSource(&s);
  9. }
  10.  
  11. static int freadWrapper(void *context, char *buf, int len)
  12. {
  13. int got = fread(buf, 1, len, (FILE *) context);
  14. return got;
  15. }

gdImageCreateFromGif(FILE *in) (FUNCTION)

gdImageCreateFromGifPtr(int size, void *data) (FUNCTION)

gdImageCreateFromGifCtx(gdIOCtx *in) (FUNCTION)

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

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

  1. gdImagePtr im;
  2. /*... inside a function ...*/
  3. FILE *in;
  4. in = fopen("mygif.gif", "rb");
  5. im = gdImageCreateFromGif(in);
  6. fclose(in);
  7. /* ... Use the image ... */
  8. gdImageDestroy(im);

gdImageCreateFromGd(FILE *in) (FUNCTION)

gdImageCreateFromGdPtr(int size, void *data) (FUNCTION)

gdImageCreateFromGdCtx(gdIOCtx *in) (FUNCTION)

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

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

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

  1. /* ... inside a function ... */
  2. gdImagePtr im;
  3. FILE *in;
  4. in = fopen("mygd.gd", "rb");
  5. im = gdImageCreateFromGd(in);
  6. fclose(in);
  7. /* ... Use the image ... */
  8. gdImageDestroy(im);

gdImageCreateFromGd2(FILE *in) (FUNCTION)

gdImageCreateFromGd2Ptr(int size, void *data) (FUNCTION)

gdImageCreateFromGd2Ctx(gdIOCtx *in) (FUNCTION)

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

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

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

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. FILE *in;
  4. in = fopen("mygd.gd2", "rb");
  5. im = gdImageCreateFromGd2(in);
  6. fclose(in);
  7. /* ... Use the image ... */
  8. gdImageDestroy(im);

gdImageCreateFromGd2Part(FILE *in, int srcX, int srcY, int w, int h) (FUNCTION)

gdImageCreateFromGd2PartPtr(int size, void *data, int srcX, int srcY, int w, int h) (FUNCTION)

gdImageCreateFromGd2PartCtx(gdIOCtx *in) (FUNCTION)

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

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

gdImageCreateFromWBMP(FILE *in) (FUNCTION)

gdImageCreateFromWBMPPtr(int size, void *data) (FUNCTION)

gdImageCreateFromWBMPCtx(gdIOCtx *in) (FUNCTION)

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

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

  1. gdImagePtr im;
  2. /*... inside a function ...*/
  3. FILE *in;
  4. in = fopen("mywbmp.wbmp", "rb");
  5. im = gdImageCreateFromWBMP(in);
  6. fclose(in);
  7. /* ... Use the image ... */
  8. gdImageDestroy(im);

gdImageCreateFromXbm(FILE *in) (FUNCTION)

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

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. FILE *in;
  4. in = fopen("myxbm.xbm", "rb");
  5. im = gdImageCreateFromXbm(in);
  6. fclose(in);
  7. /* ... Use the image ... */
  8. gdImageDestroy(im);

gdImageCreateFromXpm(char *filename) (FUNCTION)

gdImageCreateFromXbm is called to load images from XPM X Window System color bitmap format files. This function is available only if HAVE_XPM is selected in the Makefile and the Xpm library is linked with the application. Unlike most gd file functions, the Xpm functions require filenames, not file pointers. gdImageCreateFromXpm returns a gdImagePtr to the new image, or NULL if unable to load the image (most often because the file is corrupt or does not contain an XPM bitmap format image). You can inspect the sx and sy members of the image to determine its size. The image must eventually be destroyed using gdImageDestroy().

  1. /* ... inside a function ... */
  2. gdImagePtr im;
  3.  
  4. im = gdImageCreateFromXpm("myxpm.xpm");
  5.  
  6. /* ... Use the image ... */
  7. gdImageDestroy(im);
  8.  

gdImageDestroy(gdImagePtr im) (FUNCTION)

gdImageDestroy is used to free the memory associated with an image. It is important to invoke gdImageDestroy before exiting your program or assigning a new image to a gdImagePtr variable.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. im = gdImageCreate(10, 10);
  4. /* ... Use the image ... */
  5. /* Now destroy it */
  6. gdImageDestroy(im);
  7.  

void gdImageJpeg(gdImagePtr im, FILE *out, int quality) (FUNCTION)

void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality) (FUNCTION)

gdImageJpeg outputs the specified image to the specified file in JPEG format. The file must be open for writing. Under MSDOS and all versions of Windows, it is important to use "wb" as opposed to simply "w" as the mode when opening the file, and under Unix there is no penalty for doing so. gdImageJpeg does not close the file; your code must do so.

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

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

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black, white;
  4. FILE *out;
  5. /* Create the image */
  6. im = gdImageCreate(100, 100);
  7. /* Allocate background */
  8. white = gdImageColorAllocate(im, 255, 255, 255);
  9. /* Allocate drawing color */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Draw rectangle */
  12. gdImageRectangle(im, 0, 0, 99, 99, black);
  13. /* Open output file in binary mode */
  14. out = fopen("rect.jpg", "wb");
  15. /* Write JPEG using default quality */
  16. gdImageJpeg(im, out, -1);
  17. /* Close file */
  18. fclose(out);
  19. /* Destroy image */
  20. gdImageDestroy(im);

void* gdImageJpegPtr(gdImagePtr im, int *size, int quality) (FUNCTION)

Identical to gdImageJpeg except that it returns a pointer to a memory area with the JPEG data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory. void gdImageGif(gdImagePtr im, FILE *out) void gdImageGifCtx(gdImagePtr im, gdIOCtx *out) (FUNCTION)

gdImageGif outputs the specified image to the specified file in GIF format. The file must be open for writing. Under MSDOS and all versions of Windows, it is important to use "wb" as opposed to simply "w" as the mode when opening the file, and under Unix there is no penalty for doing so. gdImageGif does not close the file; your code must do so.

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

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black, white;
  4. FILE *out;
  5. /* Create the image */
  6. im = gdImageCreate(100, 100);
  7. /* Allocate background */
  8. white = gdImageColorAllocate(im, 255, 255, 255);
  9. /* Allocate drawing color */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Draw rectangle */
  12. gdImageRectangle(im, 0, 0, 99, 99, black);
  13. /* Open output file in binary mode */
  14. out = fopen("rect.gif", "wb");
  15. /* Write GIF */
  16. gdImageGif(im, out);
  17. /* Close file */
  18. fclose(out);
  19. /* Destroy image */
  20. gdImageDestroy(im);

void* gdImageGifPtr(gdImagePtr im, int *size) (FUNCTION)

Identical to gdImageGif except that it returns a pointer to a memory area with the GIF data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory.

void gdImageGifAnimBegin(gdImagePtr im, FILE *out, int GlobalCM, int Loops)

void gdImageGifAnimBeginCtx(gdImagePtr im, gdIOCtx *out, int GlobalCM, int Loops) (FUNCTION)

This function must be called as the first function when creating a GIF animation. It writes the correct GIF file headers to selected file output, and prepares for frames to be added for the animation. The image argument is not used to produce an image frame to the file, it is only used to establish the GIF animation frame size, interlacing options and the color palette. gdImageGifAnimAdd is used to add the first and subsequent frames to the animation, and the animation must be terminated by writing a semicolon character (;) to it or by using gdImageGifAnimEnd to do that.

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

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

void* gdImageGifAnimBeginPtr(gdImagePtr im, int *size, int GlobalCM, int Loops) (FUNCTION)

Identical to gdImageGifAnimBegin except that it returns a pointer to a memory area with the GIF data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory.

void gdImageGifAnimAdd(gdImagePtr im, FILE *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) (FUNCTION)

void gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtx *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) (FUNCTION)

This function writes GIF animation frames to GIF animation, which was initialized with gdImageGifAnimBegin. With LeftOfs and TopOfs you can place this frame in different offset than (0,0) inside the image screen as defined in gdImageGifAnimBegin. Delay between the previous frame and this frame is in 1/100s units. Disposal is usually gdDisposalNone, meaning that the pixels changed by this frame should remain on the display when the next frame begins to render, but can also be gdDisposalUnknown (not recommended), gdDisposalRestoreBackground (restores the first allocated color of the global palette), or gdDisposalRestorePrevious (restores the appearance of the affected area before the frame was rendered). Only gdDisposalNone is a sensible choice for the first frame. If previm is passed, the built-in GIF optimizer will always use gdDisposalNone regardless of the Disposal parameter.

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

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

  1. /*... inside a function ...*/
  2. gdImagePtr im, im2, im3;
  3. int black, white, trans;
  4. FILE *out;
  5. /* Create the image */
  6. im = gdImageCreate(100, 100);
  7. /* Allocate background */
  8. white = gdImageColorAllocate(im, 255, 255, 255);
  9. /* Allocate drawing color */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Allocate transparent color for animation compression */
  12. trans = gdImageColorAllocate(im, 1, 1, 1);
  13. /* Draw rectangle */
  14. gdImageRectangle(im, 0, 0, 10, 10, black);
  15. /* Open output file in binary mode */
  16. out = fopen("anim.gif", "wb");
  17. /* Write GIF header. Use global color map. Loop a few times */
  18. gdImageGifAnimBegin(im, out, 1, 3);
  19. /* Write the first frame. No local color map. Delay = 1s */
  20. gdImageGifAnimAdd(im, out, 0, 0, 0, 100, 1, NULL);
  21. /* construct the second frame */
  22. im2 = gdImageCreate(100, 100);
  23. /* Allocate background to make it white */
  24. (void)gdImageColorAllocate(im2, 255, 255, 255);
  25. /* Make sure the palette is identical */
  26. gdImagePaletteCopy (im2, im);
  27. /* Draw something */
  28. gdImageRectangle(im2, 0, 0, 15, 15, black);
  29. /* Allow animation compression with transparent pixels */
  30. gdImageColorTransparent (im2, trans);
  31. /* Add the second frame */
  32. gdImageGifAnimAdd(im2, out, 0, 0, 0, 100, 1, im);
  33. /* construct the second frame */
  34. im3 = gdImageCreate(100, 100);
  35. /* Allocate background to make it white */
  36. (void)gdImageColorAllocate(im3, 255, 255, 255);
  37. /* Make sure the palette is identical */
  38. gdImagePaletteCopy (im3, im);
  39. /* Draw something */
  40. gdImageRectangle(im3, 0, 0, 15, 20, black);
  41. /* Allow animation compression with transparent pixels */
  42. gdImageColorTransparent (im3, trans);
  43. /* Add the third frame, compressing against the second one */
  44. gdImageGifAnimAdd(im3, out, 0, 0, 0, 100, 1, im2);
  45. /* Write the end marker */
  46. /* gdImageGifAnimEnd(out); is the same as the following: */
  47. putc (';', out);
  48. /* Close file */
  49. fclose(out);
  50. /* Destroy images */
  51. gdImageDestroy(im);
  52. gdImageDestroy(im2);
  53. gdImageDestroy(im3);

void* gdImageGifAnimAddPtr(gdImagePtr im, int *size, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) (FUNCTION)

Identical to gdImageGifAnimAdd except that it returns a pointer to a memory area with the GIF data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory.

void gdImageGifAnimEnd(FILE *out)

void gdImageGifAnimEndCtx(gdIOCtx *out) (FUNCTION)

Writes semicolon character (;) to the output file. This terminates the GIF file properly. You can omit the call to gdImageGifAnimEnd and just print out the semicolon.

void* gdImageGifAnimEndPtr(int *size) (FUNCTION)

Returns a one byte string containing the semicolon character (;). Returns a pointer to a memory area with that string. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory. The string ";" can be used in place of this function.

void gdImagePng(gdImagePtr im, FILE *out)

void gdImagePngCtx(gdImagePtr im, gdIOCtx *out) (FUNCTION)

gdImagePng outputs the specified image to the specified file in PNG format. The file must be open for writing. Under MSDOS and all versions of Windows, it is important to use "wb" as opposed to simply "w" as the mode when opening the file, and under Unix there is no penalty for doing so. gdImagePng does not close the file; your code must do so.

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black, white;
  4. FILE *out;
  5. /* Create the image */
  6. im = gdImageCreate(100, 100);
  7. /* Allocate background */
  8. white = gdImageColorAllocate(im, 255, 255, 255);
  9. /* Allocate drawing color */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Draw rectangle */
  12. gdImageRectangle(im, 0, 0, 99, 99, black);
  13. /* Open output file in binary mode */
  14. out = fopen("rect.png", "wb");
  15. /* Write PNG */
  16. gdImagePng(im, out);
  17. /* Close file */
  18. fclose(out);
  19. /* Destroy image */
  20. gdImageDestroy(im);

void gdImagePngEx(gdImagePtr im, FILE *out, int level)

void gdImagePngCtxEx(gdImagePtr im, gdIOCtx *out, int level) (FUNCTION)

Like gdImagePng, gdImagePngEx outputs the specified image to the specified file in PNG format. In addition, gdImagePngEx allows the level of compression to be specified. A compression level of 0 means "no compression." A compression level of 1 means "compressed, but as quickly as possible." A compression level of 9 means "compressed as much as possible to produce the smallest possible file." A compression level of -1 will use the default compression level at the time zlib was compiled on your system.

For more information, see gdImagePng.

void* gdImagePngPtr(gdImagePtr im, int *size) (FUNCTION)

Identical to gdImagePng except that it returns a pointer to a memory area with the PNG data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory.

void* gdImagePngPtrEx(gdImagePtr im, int *size, int level) (FUNCTION)

Like gdImagePngPtr, gdImagePngPtrEx returns a pointer to a PNG image in allocated memory. In addition, gdImagePngPtrEx allows the level of compression to be specified. A compression level of 0 means "no compression." A compression level of 1 means "compressed, but as quickly as possible." A compression level of 9 means "compressed as much as possible to produce the smallest possible file." A compression level of -1 will use the default compression level at the time zlib was compiled on your system.

For more information, see gdImagePngPtr.

gdImagePngToSink(gdImagePtr im, gdSinkPtr out) (FUNCTION)

gdImagePngToSink is called to write a PNG to a data "sink" (destination) other than a file. Usage is very similar to the gdImagePng function, except that the programmer provides a custom data sink.

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

The example below implements gdImagePng by creating a custom data source and invoking gdImagePngFromSink.

  1. static int stdioSink(void *context, char *buffer, int len)
  2. {
  3. return fwrite(buffer, 1, len, (FILE *) context);
  4. }
  5.  
  6. void gdImagePng(gdImagePtr im, FILE *out)
  7. {
  8. gdSink mySink;
  9. mySink.context = (void *) out;
  10. mySink.sink = stdioSink;
  11. gdImagePngToSink(im, &mySink);
  12. }

void gdImageWBMP(gdImagePtr im, int fg, FILE *out)

gdImageWBMPCtx(gdIOCtx *out) (FUNCTION)(FUNCTION)

gdImageWBMP outputs the specified image to the specified file in WBMP format. The file must be open for writing. Under MSDOS and all versions of Windows, it is important to use "wb" as opposed to simply "w" as the mode when opening the file, and under Unix there is no penalty for doing so. gdImageWBMP does not close the file; your code must do so.

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

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black, white;
  4. FILE *out;
  5. /* Create the image */
  6. im = gdImageCreate(100, 100);
  7. /* Allocate background */
  8. white = gdImageColorAllocate(im, 255, 255, 255);
  9. /* Allocate drawing color */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Draw rectangle */
  12. gdImageRectangle(im, 0, 0, 99, 99, black);
  13. /* Open output file in binary mode */
  14. out = fopen("rect.wbmp", "wb");
  15. /* Write WBMP, with black as foreground */
  16. gdImageWBMP(im, black, out);
  17. /* Close file */
  18. fclose(out);
  19. /* Destroy image */
  20. gdImageDestroy(im);

void* gdImageWBMPPtr(gdImagePtr im, int *size) (FUNCTION)

Identical to gdImageWBMP except that it returns a pointer to a memory area with the WBMP data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory.

void gdImageGd(gdImagePtr im, FILE *out) (FUNCTION)

gdImageGd outputs the specified image to the specified file in the gd image format. The file must be open for writing. Under MSDOS and all versions of Windows, it is important to use "wb" as opposed to simply "w" as the mode when opening the file, and under Unix there is no penalty for doing so. gdImagePng does not close the file; your code must do so.

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

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black, white;
  4. FILE *out;
  5. /* Create the image */
  6. im = gdImageCreate(100, 100);
  7. /* Allocate background */
  8. white = gdImageColorAllocate(im, 255, 255, 255);
  9. /* Allocate drawing color */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Draw rectangle */
  12. gdImageRectangle(im, 0, 0, 99, 99, black);
  13. /* Open output file in binary mode */
  14. out = fopen("rect.gd", "wb");
  15. /* Write gd format file */
  16. gdImageGd(im, out);
  17. /* Close file */
  18. fclose(out);
  19. /* Destroy image */
  20. gdImageDestroy(im);

void* gdImageGdPtr(gdImagePtr im, int *size) (FUNCTION)

Identical to gdImageGd except that it returns a pointer to a memory area with the GD data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory.

void gdImageGd2(gdImagePtr im, FILE *out, int chunkSize, int fmt)

void gdImageGd2Ctx(gdImagePtr im, gdIOCtx *out, int chunkSize, int fmt) (FUNCTION)

gdImageGd2 outputs the specified image to the specified file in the gd2 image format. The file must be open for writing. Under MSDOS and all versions of Windows, it is important to use "wb" as opposed to simply "w" as the mode when opening the file, and under Unix there is no penalty for doing so. gdImageGd2 does not close the file; your code must do so.

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

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

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

  1. /*... inside a function ...*/
  2. gdImagePtr im;
  3. int black, white;
  4. FILE *out;
  5. /* Create the image */
  6. im = gdImageCreate(100, 100);
  7. /* Allocate background */
  8. white = gdImageColorAllocate(im, 255, 255, 255);
  9. /* Allocate drawing color */
  10. black = gdImageColorAllocate(im, 0, 0, 0);
  11. /* Draw rectangle */
  12. gdImageRectangle(im, 0, 0, 99, 99, black);
  13. /* Open output file in binary mode */
  14. out = fopen("rect.gd", "wb");
  15. /* Write gd2 format file */
  16. gdImageGd2(im, out, 0, GD2_FMT_COMPRESSED);
  17. /* Close file */
  18. fclose(out);
  19. /* Destroy image */
  20. gdImageDestroy(im);

void* gdImageGd2Ptr(gdImagePtr im, int chunkSize, int fmt, int *size) (FUNCTION)

Identical to gdImageGd2 except that it returns a pointer to a memory area with the GD2 data. This memory must be freed by the caller when it is no longer needed. The caller must invoke gdFree(), not free(), unless the caller is absolutely certain that the same implementations of malloc, free, etc. are used both at library build time and at application build time. The 'size' parameter receives the total size of the block of memory.

void gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted)

gdImagePtr gdImageCreatePaletteFromTrueColor(gdImagePtr im, int ditherFlag, int colorsWanted) (FUNCTION)

gdImageCreatePaletteFromTrueColor returns a new image. gdImageTrueColorToPalette permanently converts the existing image. The two functions are otherwise identical.

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

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

Views
Personal tools

This a PHP.net Project

This library was originally developed by Thomas Boutell