Back to index

php5  5.3.10
gddemo.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "gd.h"
00003 #include "gdfontg.h"
00004 #include "gdfonts.h"
00005 
00006 int
00007 main (void)
00008 {
00009   /* Input and output files */
00010   FILE *in;
00011   FILE *out;
00012 
00013   /* Input and output images */
00014   gdImagePtr im_in = 0, im_out = 0;
00015 
00016   /* Brush image */
00017   gdImagePtr brush;
00018 
00019   /* Color indexes */
00020   int white;
00021   int blue;
00022   int red;
00023   int green;
00024 
00025   /* Points for polygon */
00026   gdPoint points[3];
00027 
00028   /* Create output image, 256 by 256 pixels, true color. */
00029   im_out = gdImageCreateTrueColor (256, 256);
00030   /* First color allocated is background. */
00031   white = gdImageColorAllocate (im_out, 255, 255, 255);
00032 
00033   /* Set transparent color. */
00034   gdImageColorTransparent (im_out, white);
00035 
00036   /* Try to load demoin.png and paste part of it into the
00037      output image. */
00038   in = fopen ("demoin.png", "rb");
00039   if (!in)
00040     {
00041       fprintf (stderr, "Can't load source image; this demo\n");
00042       fprintf (stderr, "is much more impressive if demoin.png\n");
00043       fprintf (stderr, "is available.\n");
00044       im_in = 0;
00045     }
00046   else
00047     {
00048       im_in = gdImageCreateFromPng (in);
00049       fclose (in);
00050       /* Now copy, and magnify as we do so */
00051       gdImageCopyResized (im_out, im_in,
00052                        32, 32, 0, 0, 192, 192, 255, 255);
00053     }
00054   red = gdImageColorAllocate (im_out, 255, 0, 0);
00055   green = gdImageColorAllocate (im_out, 0, 255, 0);
00056   blue = gdImageColorAllocate (im_out, 0, 0, 255);
00057   /* Rectangle */
00058   gdImageLine (im_out, 16, 16, 240, 16, green);
00059   gdImageLine (im_out, 240, 16, 240, 240, green);
00060   gdImageLine (im_out, 240, 240, 16, 240, green);
00061   gdImageLine (im_out, 16, 240, 16, 16, green);
00062   /* Circle */
00063   gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue);
00064   /* Arc */
00065   gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue);
00066   /* Flood fill: doesn't do much on a continuously
00067      variable tone jpeg original. */
00068   gdImageFill (im_out, 8, 8, blue);
00069   /* Polygon */
00070   points[0].x = 64;
00071   points[0].y = 0;
00072   points[1].x = 0;
00073   points[1].y = 128;
00074   points[2].x = 128;
00075   points[2].y = 128;
00076   gdImageFilledPolygon (im_out, points, 3, green);
00077   /* Brush. A fairly wild example also involving a line style! */
00078   if (im_in)
00079     {
00080       int style[8];
00081       brush = gdImageCreateTrueColor (16, 16);
00082       gdImageCopyResized (brush, im_in,
00083                        0, 0, 0, 0,
00084                        gdImageSX (brush), gdImageSY (brush),
00085                        gdImageSX (im_in), gdImageSY (im_in));
00086       gdImageSetBrush (im_out, brush);
00087       /* With a style, so they won't overprint each other.
00088          Normally, they would, yielding a fat-brush effect. */
00089       style[0] = 0;
00090       style[1] = 0;
00091       style[2] = 0;
00092       style[3] = 0;
00093       style[4] = 0;
00094       style[5] = 0;
00095       style[6] = 0;
00096       style[7] = 1;
00097       gdImageSetStyle (im_out, style, 8);
00098       /* Draw the styled, brushed line */
00099       gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed);
00100     }
00101   /* Text */
00102   gdImageString (im_out, gdFontGiant, 32, 32,
00103                (unsigned char *) "hi", red);
00104   gdImageStringUp (im_out, gdFontSmall, 64, 64,
00105                  (unsigned char *) "hi", red);
00106   /* Make output image interlaced (progressive, in the case of JPEG) */
00107   gdImageInterlace (im_out, 1);
00108   out = fopen ("demoout.png", "wb");
00109   /* Write PNG */
00110   gdImagePng (im_out, out);
00111   fclose (out);
00112   gdImageDestroy (im_out);
00113   if (im_in)
00114     {
00115       gdImageDestroy (im_in);
00116     }
00117   return 0;
00118 }