/* * Copyright (c) 2001 George C A Reid * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include #include #include #include #include #include #include #include #include #define CENTREY(a,b) ((((b)->sy)-((a)->sy))/2) #ifdef CGI char x2c(char *); void unescape_url(char *); char ** getcgivars(void); #endif int merge_images(FILE *, FILE *, unsigned char *s); int main(int argc, char *argv[]) { #ifdef CGI char **cgivars = getcgivars(); int i; #endif char *sptr = NULL; FILE *a, *b; #ifdef CGI for (i=0; cgivars[i]; i+=2) { if (!strncmp(cgivars[i], "a", 1)) a = fopen(cgivars[i+1], "r"); if (!strncmp(cgivars[i], "b", 1)) b = fopen(cgivars[i+1], "r"); if (!strncmp(cgivars[i], "s", 1)) sptr = cgivars[i+1]; } printf("Content-type: image/png\n\n"); #else fprintf(stderr, "mergeimg: Copyright (C) 2001 George C A Reid \n"); if (argc < 4) { fprintf(stderr, "\nusage: %s file1.png file2.png text\n", argv[0]); exit(1); } a = fopen(argv[1], "r"); b = fopen(argv[2], "r"); sptr = argv[3]; #endif merge_images(a, b, sptr); return 0; } int merge_images(FILE *q, FILE *p, unsigned char *s) { int black; gdImagePtr input, output; input = gdImageCreateFromPng(q); output = gdImageCreateFromPng(p); gdImageCopyMerge(output, input, 325, CENTREY(input, output), 0, 0, input->sx, input->sy, 20); black = gdImageColorClosest(output, 0, 0, 0); gdImageString(output, gdFontLarge, 325+(input->sx/2) - (strlen(s) * gdFontLarge->w / 2), 0, s, black); gdImagePng(output, stdout); gdImageDestroy(output); gdImageDestroy(input); return 0; } #ifdef CGI char x2c(char *what) { char digit; digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0')); digit *= 16; digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0')); return digit; } void unescape_url(char *url) { register int i, j; for (i = 0, j = 0; url[j]; ++i, ++j) { if ((url[i] = url[j]) == '%') { url[i] = x2c(&url[j + 1]); j += 2; } } url[i] = '\0'; } char **getcgivars() { register int i; char *request_method; int content_length; char *cgiinput; char **cgivars; char **pairlist; int paircount; char *nvpair; char *eqpos; request_method = getenv("REQUEST_METHOD"); if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD")) { cgiinput = strdup(getenv("QUERY_STRING")); } else if (!strcmp(request_method, "POST")) { if (strcasecmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) { printf("getcgivars(): Unsupported Content-Type.\n"); exit(1); } if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) { printf ("getcgivars(): No Content-Length was sent with the POST request.\n"); exit(1); } if (!(cgiinput = (char *) malloc(content_length + 1))) { printf ("getcgivars(): Could not malloc for cgiinput.\n"); exit(1); } if (!fread(cgiinput, content_length, 1, stdin)) { printf("Couldn't read CGI input from STDIN.\n"); exit(1); } cgiinput[content_length] = '\0'; } else { printf("getcgivars(): unsupported REQUEST_METHOD\n"); exit(1); } for (i = 0; cgiinput[i]; i++) if (cgiinput[i] == '+') cgiinput[i] = ' '; pairlist = (char **) malloc(256 * sizeof(char **)); paircount = 0; nvpair = strtok(cgiinput, "&"); while (nvpair) { pairlist[paircount++] = strdup(nvpair); if (!(paircount % 256)) pairlist = (char **) realloc(pairlist, (paircount + 256) * sizeof(char **)); nvpair = strtok(NULL, "&"); } pairlist[paircount] = 0; /* terminate the list with NULL */ cgivars = (char **) malloc((paircount * 2 + 1) * sizeof(char **)); for (i = 0; i < paircount; i++) { if ((eqpos = strchr(pairlist[i], '='))) { *eqpos = '\0'; unescape_url(cgivars[i * 2 + 1] = strdup(eqpos + 1)); } else { unescape_url(cgivars[i * 2 + 1] = strdup("")); } unescape_url(cgivars[i * 2] = strdup(pairlist[i])); } cgivars[paircount * 2] = 0; /* terminate the list with NULL */ free(cgiinput); for (i = 0; pairlist[i]; i++) free(pairlist[i]); free(pairlist); return cgivars; } #endif /* CGI */