resize-gd

changeset 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
files resize-gd.c
diffstat 1 files changed, 73 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/resize-gd.c	Fri Jun 13 14:18:52 2008 +0200
     1.2 +++ b/resize-gd.c	Sat Jun 14 09:08:05 2008 +0200
     1.3 @@ -1,8 +1,20 @@
     1.4 -/* compile with: gcc -lgd -lpng -lz -ljpeg  -lm resize-gd.c  */
     1.5 +/*
     1.6 + * compile with: gcc -lgd -lpng -lz -ljpeg  -lm resize-gd.c
     1.7 + * build-depends: libgd2-noxpm-dev | libgd2-dev
     1.8 + * depends: libgd2-noxpm | libgd2-xpm
     1.9 + *
    1.10 + *
    1.11 + *
    1.12 + * http://www.libgd.org/ImageCreation
    1.13 + * http://cpan.uwinnipeg.ca/htdocs/Image-Resize/Image/Resize.pm.html
    1.14 + * http://netpbm.sourceforge.net/
    1.15 + *
    1.16 + */
    1.17  
    1.18  #include <stdio.h>
    1.19  #include <stdlib.h>  /* for atoi() */
    1.20  #include <string.h>
    1.21 +#include <ctype.h>
    1.22  #include "gd.h" /* Bring in the gd library functions */
    1.23  
    1.24  
    1.25 @@ -15,51 +27,98 @@
    1.26  int main(int argc, char* argv[]) {
    1.27  	int i;
    1.28  	int w, h;
    1.29 +	int width, height;
    1.30  	int x, y;
    1.31  	int type;
    1.32  	gdImagePtr im_in;
    1.33  	gdImagePtr im_out;
    1.34  	FILE* in;
    1.35  	FILE* out;
    1.36 +	char* sep;
    1.37  
    1.38  	if (argc < 3) {
    1.39 -		puts("usage: resize-gd <width>x<height> <imagefiles>");
    1.40 +		puts("usage: resize-gd <width>x<height> <imagefiles>\n\
    1.41 +       resize-gd <size> <imagefiles> (keeps aspect ratio)\n");
    1.42  		exit(1);
    1.43  	}
    1.44  
    1.45  	/* parse width and height */
    1.46 -	w = atoi(argv[1]);
    1.47 -	h = atoi(strstr(argv[1], "x") + 1);
    1.48 -	printf("w: %d  h: %d\n", w, h);
    1.49 +	sep = argv[1];
    1.50 +	while (isdigit(*sep)) {
    1.51 +		sep++;
    1.52 +	}
    1.53 +	if (*sep == 'x' && isdigit(*(sep+1))) {
    1.54 +		sep++;
    1.55 +		while (isdigit(*sep)) {
    1.56 +			sep++;
    1.57 +		}
    1.58 +	}
    1.59 +	if (*sep != '\0') {
    1.60 +		fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
    1.61 +		exit(3);
    1.62 +	}
    1.63 +
    1.64 +	width = atoi(argv[1]);
    1.65 +	sep = strstr(argv[1], "x");
    1.66 +	if (sep && sep[1] != '\0') {
    1.67 +		height = atoi(sep + 1);
    1.68 +	} else {
    1.69 +		height = -1;  /* keep aspect ratio */
    1.70 +	}
    1.71 +
    1.72  
    1.73  	/* process images */
    1.74  	for (i = 2; i < argc; i++) {
    1.75  		printf("processing file '%s'\n", argv[i]);
    1.76  
    1.77 -		if (strcmp(&(argv[i][strlen(argv[i])-4]), ".png") == 0) {
    1.78 +		if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
    1.79  			type = Png;
    1.80 -		} else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) {
    1.81 +		} else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
    1.82  			type = Jpg;
    1.83  		} else {
    1.84 -			fprintf(stderr, "unknown filetype\n");
    1.85 -			exit(2);
    1.86 +			fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
    1.87 +			continue;
    1.88  		}
    1.89 +		/* load image */
    1.90 +		in = fopen(argv[i], "rb");
    1.91  
    1.92 -		/* Load a large png to shrink in the smaller one */
    1.93 -		in = fopen(argv[i], "rb");
    1.94  		if (type == Png) {
    1.95  			im_in = gdImageCreateFromPng(in);
    1.96  		} else if (type == Jpg) {
    1.97  			im_in = gdImageCreateFromJpeg(in);
    1.98  		}
    1.99 +		/*
   1.100 +		if ((im_in = gdImageCreateFromPng(in)) != NULL) {
   1.101 +			type = Png;
   1.102 +		} else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) {
   1.103 +			type = Jpg;
   1.104 +		} else {
   1.105 +			fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
   1.106 +			continue;
   1.107 +		}
   1.108 +		*/
   1.109 +
   1.110  		fclose(in);
   1.111  
   1.112 +		/* for keeping aspect ratio */
   1.113 +		if (height <= 0) {
   1.114 +			if (im_in->sx > im_in->sy) {
   1.115 +				w = width;
   1.116 +				h = width * (1.0 * im_in->sy / im_in->sx);
   1.117 +			} else {
   1.118 +				h = width;
   1.119 +				w = width * (1.0 * im_in->sx / im_in->sy);
   1.120 +			}
   1.121 +		} else {
   1.122 +			w = width;
   1.123 +			h = height;
   1.124 +		}
   1.125 +
   1.126 +		/* copy-resize the image */
   1.127  		im_out = gdImageCreateTrueColor(w, h);
   1.128 -
   1.129 -		/* Now copy the large image */
   1.130  		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
   1.131  
   1.132 -		/* write out */
   1.133 +		/* write image */
   1.134  		out = fopen(argv[i], "wb");
   1.135  		if (type == Png) {
   1.136  			gdImagePng(im_out, out);