xref: /PHP-5.5/ext/gd/libgd/gddemo.c (revision 7a8cade3)
1 #include <stdio.h>
2 #include "gd.h"
3 #include "gdfontg.h"
4 #include "gdfonts.h"
5 
6 int
main(void)7 main (void)
8 {
9   /* Input and output files */
10   FILE *in;
11   FILE *out;
12 
13   /* Input and output images */
14   gdImagePtr im_in = 0, im_out = 0;
15 
16   /* Brush image */
17   gdImagePtr brush;
18 
19   /* Color indexes */
20   int white;
21   int blue;
22   int red;
23   int green;
24 
25   /* Points for polygon */
26   gdPoint points[3];
27 
28   /* Create output image, 256 by 256 pixels, true color. */
29   im_out = gdImageCreateTrueColor (256, 256);
30   /* First color allocated is background. */
31   white = gdImageColorAllocate (im_out, 255, 255, 255);
32 
33   /* Set transparent color. */
34   gdImageColorTransparent (im_out, white);
35 
36   /* Try to load demoin.png and paste part of it into the
37      output image. */
38   in = fopen ("demoin.png", "rb");
39   if (!in)
40     {
41       fprintf (stderr, "Can't load source image; this demo\n");
42       fprintf (stderr, "is much more impressive if demoin.png\n");
43       fprintf (stderr, "is available.\n");
44       im_in = 0;
45     }
46   else
47     {
48       im_in = gdImageCreateFromPng (in);
49       fclose (in);
50       /* Now copy, and magnify as we do so */
51       gdImageCopyResized (im_out, im_in,
52 			  32, 32, 0, 0, 192, 192, 255, 255);
53     }
54   red = gdImageColorAllocate (im_out, 255, 0, 0);
55   green = gdImageColorAllocate (im_out, 0, 255, 0);
56   blue = gdImageColorAllocate (im_out, 0, 0, 255);
57   /* Rectangle */
58   gdImageLine (im_out, 16, 16, 240, 16, green);
59   gdImageLine (im_out, 240, 16, 240, 240, green);
60   gdImageLine (im_out, 240, 240, 16, 240, green);
61   gdImageLine (im_out, 16, 240, 16, 16, green);
62   /* Circle */
63   gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue);
64   /* Arc */
65   gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue);
66   /* Flood fill: doesn't do much on a continuously
67      variable tone jpeg original. */
68   gdImageFill (im_out, 8, 8, blue);
69   /* Polygon */
70   points[0].x = 64;
71   points[0].y = 0;
72   points[1].x = 0;
73   points[1].y = 128;
74   points[2].x = 128;
75   points[2].y = 128;
76   gdImageFilledPolygon (im_out, points, 3, green);
77   /* Brush. A fairly wild example also involving a line style! */
78   if (im_in)
79     {
80       int style[8];
81       brush = gdImageCreateTrueColor (16, 16);
82       gdImageCopyResized (brush, im_in,
83 			  0, 0, 0, 0,
84 			  gdImageSX (brush), gdImageSY (brush),
85 			  gdImageSX (im_in), gdImageSY (im_in));
86       gdImageSetBrush (im_out, brush);
87       /* With a style, so they won't overprint each other.
88          Normally, they would, yielding a fat-brush effect. */
89       style[0] = 0;
90       style[1] = 0;
91       style[2] = 0;
92       style[3] = 0;
93       style[4] = 0;
94       style[5] = 0;
95       style[6] = 0;
96       style[7] = 1;
97       gdImageSetStyle (im_out, style, 8);
98       /* Draw the styled, brushed line */
99       gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed);
100     }
101   /* Text */
102   gdImageString (im_out, gdFontGiant, 32, 32,
103 		 (unsigned char *) "hi", red);
104   gdImageStringUp (im_out, gdFontSmall, 64, 64,
105 		   (unsigned char *) "hi", red);
106   /* Make output image interlaced (progressive, in the case of JPEG) */
107   gdImageInterlace (im_out, 1);
108   out = fopen ("demoout.png", "wb");
109   /* Write PNG */
110   gdImagePng (im_out, out);
111   fclose (out);
112   gdImageDestroy (im_out);
113   if (im_in)
114     {
115       gdImageDestroy (im_in);
116     }
117   return 0;
118 }
119