# HG changeset patch # User meillo@localhost.localdomain # Date 1213359532 -7200 # Node ID 8c94239b3b3fe5906b4b5d1da44b27d65f99125b initial commit (provides basic funcionality) diff -r 000000000000 -r 8c94239b3b3f Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Jun 13 14:18:52 2008 +0200 @@ -0,0 +1,2 @@ +resize-gd: resize-gd.c + gcc -o resize-gd -lgd -lpng -lz -ljpeg -lm resize-gd.c diff -r 000000000000 -r 8c94239b3b3f resize-gd.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resize-gd.c Fri Jun 13 14:18:52 2008 +0200 @@ -0,0 +1,76 @@ +/* compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c */ + +#include +#include /* for atoi() */ +#include +#include "gd.h" /* Bring in the gd library functions */ + + +enum { + Png, + Jpg, +}; + + +int main(int argc, char* argv[]) { + int i; + int w, h; + int x, y; + int type; + gdImagePtr im_in; + gdImagePtr im_out; + FILE* in; + FILE* out; + + if (argc < 3) { + puts("usage: resize-gd x "); + 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); + + /* 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) { + type = Png; + } else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) { + type = Jpg; + } else { + fprintf(stderr, "unknown filetype\n"); + exit(2); + } + + /* 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); + } + fclose(in); + + 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 */ + out = fopen(argv[i], "wb"); + if (type == Png) { + gdImagePng(im_out, out); + } else if (type == Jpg) { + gdImageJpeg(im_out, out, -1); + } + fclose(out); + + gdImageDestroy(im_in); + gdImageDestroy(im_out); + } + + return 0; +}