# HG changeset patch # User meillo@localhost.localdomain # Date 1213427285 -7200 # Node ID 7a8f72b27dc34ab707291b2e9d1b5ce939f46f60 # Parent 8c94239b3b3fe5906b4b5d1da44b27d65f99125b added keep aspect ratio; validated size diff -r 8c94239b3b3f -r 7a8f72b27dc3 resize-gd.c --- a/resize-gd.c Fri Jun 13 14:18:52 2008 +0200 +++ b/resize-gd.c Sat Jun 14 09:08:05 2008 +0200 @@ -1,8 +1,20 @@ -/* compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c */ +/* + * compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c + * build-depends: libgd2-noxpm-dev | libgd2-dev + * depends: libgd2-noxpm | libgd2-xpm + * + * + * + * http://www.libgd.org/ImageCreation + * http://cpan.uwinnipeg.ca/htdocs/Image-Resize/Image/Resize.pm.html + * http://netpbm.sourceforge.net/ + * + */ #include #include /* for atoi() */ #include +#include #include "gd.h" /* Bring in the gd library functions */ @@ -15,51 +27,98 @@ int main(int argc, char* argv[]) { int i; int w, h; + int width, height; int x, y; int type; gdImagePtr im_in; gdImagePtr im_out; FILE* in; FILE* out; + char* sep; if (argc < 3) { - puts("usage: resize-gd x "); + puts("usage: resize-gd x \n\ + resize-gd (keeps aspect ratio)\n"); exit(1); } /* parse width and height */ - w = atoi(argv[1]); - h = atoi(strstr(argv[1], "x") + 1); - printf("w: %d h: %d\n", w, h); + sep = argv[1]; + while (isdigit(*sep)) { + sep++; + } + if (*sep == 'x' && isdigit(*(sep+1))) { + sep++; + while (isdigit(*sep)) { + sep++; + } + } + if (*sep != '\0') { + fprintf(stderr, "Invalid form of size. Has to be or x.\n"); + exit(3); + } + + width = atoi(argv[1]); + sep = strstr(argv[1], "x"); + if (sep && sep[1] != '\0') { + height = atoi(sep + 1); + } else { + height = -1; /* keep aspect ratio */ + } + /* process images */ for (i = 2; i < argc; i++) { printf("processing file '%s'\n", argv[i]); - if (strcmp(&(argv[i][strlen(argv[i])-4]), ".png") == 0) { + if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) { type = Png; - } else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) { + } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) { type = Jpg; } else { - fprintf(stderr, "unknown filetype\n"); - exit(2); + fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); + continue; } + /* load image */ + in = fopen(argv[i], "rb"); - /* Load a large png to shrink in the smaller one */ - in = fopen(argv[i], "rb"); if (type == Png) { im_in = gdImageCreateFromPng(in); } else if (type == Jpg) { im_in = gdImageCreateFromJpeg(in); } + /* + if ((im_in = gdImageCreateFromPng(in)) != NULL) { + type = Png; + } else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) { + type = Jpg; + } else { + fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); + continue; + } + */ + fclose(in); + /* for keeping aspect ratio */ + if (height <= 0) { + if (im_in->sx > im_in->sy) { + w = width; + h = width * (1.0 * im_in->sy / im_in->sx); + } else { + h = width; + w = width * (1.0 * im_in->sx / im_in->sy); + } + } else { + w = width; + h = height; + } + + /* copy-resize the image */ im_out = gdImageCreateTrueColor(w, h); - - /* Now copy the large image */ gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); - /* write out */ + /* write image */ out = fopen(argv[i], "wb"); if (type == Png) { gdImagePng(im_out, out);