📜 ⬆️ ⬇️

We draw animated gifs with libgd.

The point of this article is to give an idea of ​​using the GD library in conjunction with C.

Download libgd or read more about its use here: http://www.libgd.org/ (eng.)

After installing GD, the list of functions and the types of received / returned data can be viewed in the /usr/local/include/gd.h file (in my case, otherwise sudo updatedb && locate gd.h ) to understand the purpose of functions is quite simple by their name.
')
The code of a small program with my description, which draws this:

Image and video hosting by TinyPic



If anything is unclear, I’ll provide a more detailed explanation below.

So, let's begin:

//
#include <gd.h>
#include <stdio.h>

int main( int argc, char **argv) {

gdImagePtr im,im_clear; //
int black, white, trans; //
FILE *out1; //

im = gdImageCreate(100, 100); // 100100
im_clear = gdImageCreate(100, 100);

white = gdImageColorAllocate(im, 255, 255, 255); //
black = gdImageColorAllocate(im, 0, 0, 0);
trans = gdImageColorAllocate(im, 1, 1, 1);

gdImageCopy (im_clear, im, 0,0,0,0, 100, 100); // im im_clear
if (argc>1) out1 = fopen(argv[1], "wb" ); else out1 = fopen( "anim.gif" , "wb" ); //
gdImageGifAnimBegin(im, out1, 1, 0); // gif-

for ( int i=0;i<25;i++)
{
gdImageCopy (im, im_clear, 0,0,0,0, 100, 100); // im
gdImageRectangle(im, i*2, i*2, 100-i*2-1, 100-i*2-1, black); //
gdImageGifAnimAdd(im, out1, 0, 0, 0, 10, 1, NULL); //
};

putc ( ';' , out1); //
fclose(out1); //

gdImageDestroy(im); //
gdImageDestroy(im_clear);

}

* This source code was highlighted with Source Code Highlighter .


Save the file
compile it, in my case like this: gcc-lgd filename_o-o imname executable_file
and run: ./ executable_file_name [where_save_picture]

upd: Ubuntu 8.10 OS was used in my case, in your case there may be minor differences in the commands.

Source: https://habr.com/ru/post/52775/


All Articles