resize-gd

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 source
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 */
14 #include <stdio.h>
15 #include <stdlib.h> /* for atoi() */
16 #include <string.h>
17 #include <ctype.h>
18 #include "gd.h" /* Bring in the gd library functions */
21 enum {
22 Png,
23 Jpg,
24 };
27 int main(int argc, char* argv[]) {
28 int i;
29 int w, h;
30 int width, height;
31 int x, y;
32 int type;
33 gdImagePtr im_in;
34 gdImagePtr im_out;
35 FILE* in;
36 FILE* out;
37 char* sep;
39 if (argc < 3) {
40 puts("usage: resize-gd <width>x<height> <imagefiles>\n\
41 resize-gd <size> <imagefiles> (keeps aspect ratio)\n");
42 exit(1);
43 }
45 /* parse width and height */
46 sep = argv[1];
47 while (isdigit(*sep)) {
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 }
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 }
70 /* process images */
71 for (i = 2; i < argc; i++) {
72 printf("processing file '%s'\n", argv[i]);
74 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
75 type = Png;
76 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
77 type = Jpg;
78 } else {
79 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
80 continue;
81 }
82 /* load image */
83 in = fopen(argv[i], "rb");
85 if (type == Png) {
86 im_in = gdImageCreateFromPng(in);
87 } else if (type == Jpg) {
88 im_in = gdImageCreateFromJpeg(in);
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 */
101 fclose(in);
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 }
117 /* copy-resize the image */
118 im_out = gdImageCreateTrueColor(w, h);
119 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
121 /* write image */
122 out = fopen(argv[i], "wb");
123 if (type == Png) {
124 gdImagePng(im_out, out);
125 } else if (type == Jpg) {
126 gdImageJpeg(im_out, out, -1);
127 }
128 fclose(out);
130 gdImageDestroy(im_in);
131 gdImageDestroy(im_out);
132 }
134 return 0;
135 }