view resize-gd.c @ 1:7a8f72b27dc3

added keep aspect ratio; validated size
author meillo@localhost.localdomain
date Sat, 14 Jun 2008 09:08:05 +0200
parents 8c94239b3b3f
children 8e71b54b6e1e
line wrap: on
line source

/*
 * 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 <stdio.h>
#include <stdlib.h>  /* for atoi() */
#include <string.h>
#include <ctype.h>
#include "gd.h" /* Bring in the gd library functions */


enum {
	Png,
	Jpg,
};


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 <width>x<height> <imagefiles>\n\
       resize-gd <size> <imagefiles> (keeps aspect ratio)\n");
		exit(1);
	}

	/* parse width and height */
	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 <size> or <width>x<height>.\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) {
			type = Png;
		} else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
			type = Jpg;
		} else {
			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");

		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);
		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);

		/* write image */
		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;
}