Mercurial > resize-gd
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:8c94239b3b3f | 1:7a8f72b27dc3 |
---|---|
1 /* compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c */ | 1 /* |
2 * compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c | |
3 * build-depends: libgd2-noxpm-dev | libgd2-dev | |
4 * depends: libgd2-noxpm | libgd2-xpm | |
5 * | |
6 * | |
7 * | |
8 * http://www.libgd.org/ImageCreation | |
9 * http://cpan.uwinnipeg.ca/htdocs/Image-Resize/Image/Resize.pm.html | |
10 * http://netpbm.sourceforge.net/ | |
11 * | |
12 */ | |
2 | 13 |
3 #include <stdio.h> | 14 #include <stdio.h> |
4 #include <stdlib.h> /* for atoi() */ | 15 #include <stdlib.h> /* for atoi() */ |
5 #include <string.h> | 16 #include <string.h> |
17 #include <ctype.h> | |
6 #include "gd.h" /* Bring in the gd library functions */ | 18 #include "gd.h" /* Bring in the gd library functions */ |
7 | 19 |
8 | 20 |
9 enum { | 21 enum { |
10 Png, | 22 Png, |
13 | 25 |
14 | 26 |
15 int main(int argc, char* argv[]) { | 27 int main(int argc, char* argv[]) { |
16 int i; | 28 int i; |
17 int w, h; | 29 int w, h; |
30 int width, height; | |
18 int x, y; | 31 int x, y; |
19 int type; | 32 int type; |
20 gdImagePtr im_in; | 33 gdImagePtr im_in; |
21 gdImagePtr im_out; | 34 gdImagePtr im_out; |
22 FILE* in; | 35 FILE* in; |
23 FILE* out; | 36 FILE* out; |
37 char* sep; | |
24 | 38 |
25 if (argc < 3) { | 39 if (argc < 3) { |
26 puts("usage: resize-gd <width>x<height> <imagefiles>"); | 40 puts("usage: resize-gd <width>x<height> <imagefiles>\n\ |
41 resize-gd <size> <imagefiles> (keeps aspect ratio)\n"); | |
27 exit(1); | 42 exit(1); |
28 } | 43 } |
29 | 44 |
30 /* parse width and height */ | 45 /* parse width and height */ |
31 w = atoi(argv[1]); | 46 sep = argv[1]; |
32 h = atoi(strstr(argv[1], "x") + 1); | 47 while (isdigit(*sep)) { |
33 printf("w: %d h: %d\n", w, h); | 48 sep++; |
49 } | |
50 if (*sep == 'x' && isdigit(*(sep+1))) { | |
51 sep++; | |
52 while (isdigit(*sep)) { | |
53 sep++; | |
54 } | |
55 } | |
56 if (*sep != '\0') { | |
57 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n"); | |
58 exit(3); | |
59 } | |
60 | |
61 width = atoi(argv[1]); | |
62 sep = strstr(argv[1], "x"); | |
63 if (sep && sep[1] != '\0') { | |
64 height = atoi(sep + 1); | |
65 } else { | |
66 height = -1; /* keep aspect ratio */ | |
67 } | |
68 | |
34 | 69 |
35 /* process images */ | 70 /* process images */ |
36 for (i = 2; i < argc; i++) { | 71 for (i = 2; i < argc; i++) { |
37 printf("processing file '%s'\n", argv[i]); | 72 printf("processing file '%s'\n", argv[i]); |
38 | 73 |
39 if (strcmp(&(argv[i][strlen(argv[i])-4]), ".png") == 0) { | 74 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) { |
40 type = Png; | 75 type = Png; |
41 } else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) { | 76 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) { |
42 type = Jpg; | 77 type = Jpg; |
43 } else { | 78 } else { |
44 fprintf(stderr, "unknown filetype\n"); | 79 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); |
45 exit(2); | 80 continue; |
46 } | 81 } |
82 /* load image */ | |
83 in = fopen(argv[i], "rb"); | |
47 | 84 |
48 /* Load a large png to shrink in the smaller one */ | |
49 in = fopen(argv[i], "rb"); | |
50 if (type == Png) { | 85 if (type == Png) { |
51 im_in = gdImageCreateFromPng(in); | 86 im_in = gdImageCreateFromPng(in); |
52 } else if (type == Jpg) { | 87 } else if (type == Jpg) { |
53 im_in = gdImageCreateFromJpeg(in); | 88 im_in = gdImageCreateFromJpeg(in); |
54 } | 89 } |
90 /* | |
91 if ((im_in = gdImageCreateFromPng(in)) != NULL) { | |
92 type = Png; | |
93 } else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) { | |
94 type = Jpg; | |
95 } else { | |
96 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); | |
97 continue; | |
98 } | |
99 */ | |
100 | |
55 fclose(in); | 101 fclose(in); |
56 | 102 |
103 /* for keeping aspect ratio */ | |
104 if (height <= 0) { | |
105 if (im_in->sx > im_in->sy) { | |
106 w = width; | |
107 h = width * (1.0 * im_in->sy / im_in->sx); | |
108 } else { | |
109 h = width; | |
110 w = width * (1.0 * im_in->sx / im_in->sy); | |
111 } | |
112 } else { | |
113 w = width; | |
114 h = height; | |
115 } | |
116 | |
117 /* copy-resize the image */ | |
57 im_out = gdImageCreateTrueColor(w, h); | 118 im_out = gdImageCreateTrueColor(w, h); |
58 | |
59 /* Now copy the large image */ | |
60 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); | 119 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); |
61 | 120 |
62 /* write out */ | 121 /* write image */ |
63 out = fopen(argv[i], "wb"); | 122 out = fopen(argv[i], "wb"); |
64 if (type == Png) { | 123 if (type == Png) { |
65 gdImagePng(im_out, out); | 124 gdImagePng(im_out, out); |
66 } else if (type == Jpg) { | 125 } else if (type == Jpg) { |
67 gdImageJpeg(im_out, out, -1); | 126 gdImageJpeg(im_out, out, -1); |