gd and animated GIFs

[email protected] ("Bill Frost")
Newsgroups php.gd.devel
Message-ID <001601c86e2e$a5978220$ab00a8c0@BillFrost>
Hi all,

Just playing around with gd and an animated GIF. With the current Windows
build (2.0.34 binaries) and the DLL, I can build and run the example
animation at
http://cvs.php.net/viewvc.cgi/gd/libgd/examples/gif.c?view=markup but I only
see a blank, white image of 100, 100. No frames!

The culprit appears to be this line:

<snip>
gdImageFilledRectangle(im, rand() % 100, rand() % 100, rand() % 100, rand()
% 100, black);
<snip>

If I change it to:

gdImageRectangle(im, rand() % 100, rand() % 100, rand() % 100, rand() % 100,
black);

All runs as it should and a random coloured rectangle with a white interior
is drawn.


For what it's worth, I replaced the program with a quick, tacky one that
slides, expands / contracts or bounces most of the gd objects in a 120, 120
pixel animation. Please check it and possibly replace the demonstration one
with this one if you think it is worth it.

**********************

#include <stdio.h>
#include <stdlib.h>
#include "gd.h"
#include "gdfonts.h"

int main(int argc, char ** argv)
{
   int i = 0;
   FILE * out;
   gdImagePtr img;
   gdImagePtr prev =NULL;
   int white, randColor, blue, cyan, darkgreen, tan, navyblue, powderblue,
red, salmon;

   img = gdImageCreate(120, 120);
   if (!img) {
      fprintf(stderr, "can't create image");
      return 1;
   }

   out = fopen("anim.gif", "wb");
   if (!out) {
      fprintf(stderr, "can't create file %s", "anim.gif");
      return 1;
   }

   white = gdImageColorAllocate(img, 255, 255, 255);
   gdImageGifAnimBegin(img, out, 1, 3);
   char message[6];

   for (i = 0; i < 20; i++) {
      int r, g, b;
      img = gdImageCreate(120, 120);
      gdPoint dataFrame[4];
      dataFrame[0].x = 75+i;
      dataFrame[0].y = 5+i;
      dataFrame[1].x = 95-i;
      dataFrame[1].y = 5+2*i;
      dataFrame[2].x = 95-i;
      dataFrame[2].y = 95+i;
      dataFrame[3].x = 75+i;
      dataFrame[3].y = 99-2*i;
      r = rand() % 255;
      g = rand() % 255;
      b = rand() % 255;

      white = gdImageColorAllocate(img, 255, 255, 255);
      blue = gdImageColorAllocate(img, 0, 0, 255);
      cyan = gdImageColorAllocate(img, 0, 255, 255);
      darkgreen = gdImageColorAllocate(img, 0, 100, 0);
      navyblue = gdImageColorAllocate(img, 0, 0, 128);
      powderblue = gdImageColorAllocate(img, 176, 224, 230);
      red = gdImageColorAllocate(img, 255, 0, 0);
      salmon = gdImageColorAllocate(img, 255, 140, 105);
      tan = gdImageColorAllocate(img, 210, 180, 140);
      randColor = gdImageColorAllocate(img,  r, g, b);

      gdImageFilledPolygon(img, dataFrame, 4, darkgreen);
      gdImageRectangle(img, 5, 5, 3*i, 15+i, blue);
      gdImageFilledRectangle(img, 25, 35+i, 45+3*i, 45+2*i, navyblue);
      gdImageFilledEllipse(img, 75-i, (25+7*i) % 100, 25+i, 22+i,
randColor);
      gdImageString (img, gdFontGetSmall(), 10, 100-i, (unsigned char *)
"Animation", darkgreen);
      itoa(i+1, message, 10);
      gdImageString (img, gdFontGetSmall(), 67, 100-i, (unsigned char *)
message, red);
      gdImageSetThickness(img, i);
      gdImageLine(img, 15+3*i, 15, 105-3*i, 15, tan); // x top axis

      gdImageGifAnimAdd(img, out, 1, 0, 0, 10, 1, prev);

      if(prev) {
         gdImageDestroy(prev);
      }
      prev = img;
   }
   gdImageGifAnimEnd(out);
   fclose(out);
   system("anim.gif");
   
   return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.