/* * "hir.c" - Copyright 2008 HdS619 ( hds619@gmail.com ) * * Options for compile: -lgd -lpng -lz -ljpeg -lfreetype -lm * * HdS image Resize is a free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include #include #include #include #include #include #include #include #include #define or || #define and && int valid_format(char *str) { if ( !strstr(str, "jpg") and \ !strstr(str, "jpeg") and \ !strstr(str, "png") \ ) return 0; else return 1; } char* format(char *str) { if ( strstr(str, "jpg") or strstr(str, "jpeg") ) return "Jpeg"; if ( strstr(str, "png") ) return "Png"; fprintf(stderr, "Internal Error.\nExit.\n\n"); exit(1); } char *size(FILE *bin) { char c, *str; double file_size = 0; fseek(bin, 0, SEEK_SET); while(!feof(bin)) { fread(&c, sizeof(char), 1, bin); file_size++; } fseek(bin, 0, SEEK_SET); str = (char *) malloc(32); if ( file_size >= 1024 ) { file_size /= 1024; if ( file_size >= 1024 ) { file_size /= 1024; sprintf(str, "%- .3lf MB.", file_size); } else sprintf(str, "%- .3lf KB.", file_size); } else sprintf(str, "%- .3lf Bytes.", file_size); return str; } int main(int argc, char *argv[]) { gdImagePtr thumb, source; FILE *in, *out; int white; int tx, ty, x, y; char c, *str; if ( argc < 5 ) { fprintf(stderr, "Usage: %s original_image thumb_image x y [ quality ]\n" "Example: %s img.png img.jpg 100 50 80\n" "\n" "Accept format: png, jpg ( or jpeg ).\n\n", argv[0], argv[0]); return 1; } if ( (tx = atoi(argv[3])) < 1 or (ty = atoi(argv[4])) < 1 ) { fprintf(stderr, "Error: thumb image `%s': x(%d) or y(%d) not valid.\n", argv[2], atoi(argv[3]), atoi(argv[4])); return 1; } if( !valid_format(argv[1]) ) { fprintf(stderr, "Error: source image `%s': not valid format.\n", argv[1]); return 1; } if (!valid_format(argv[2]) ) { fprintf(stderr, "Error: thumb image `%s': not valid format.\n", argv[2]); return 1; } if (!(in = fopen(argv[1], "rb"))) { fprintf(stderr, "Error: source image `%s': not exist.\n"); return 1; } if ( strstr(argv[1], "png") ) source = gdImageCreateFromPng(in); else source = gdImageCreateFromJpeg(in); if ( (y = gdImageSY(source)) < 1 or (x = gdImageSX(source)) < 1 ) { fprintf(stderr, "Error: source image `%s': x(%d) or y(%d) not valid.\n", argv[1], gdImageSX(thumb), gdImageSY(thumb)); return 1; } if (fopen(argv[2], "rb")) { while(1) { fprintf(stderr, "Warning: thumb image `%s': exist, overwrite (y/N) ? ", argv[2]); c = getc(stdin); if ( c == 'y' or c == 'Y' ) break; else if ( c == 'n' or c == 'N' or c == '\n' ) return 1; } } if (!(out = fopen(argv[2], "wb"))) { fprintf(stderr, "Error: thumb image `%s': cannot binary write.\n" " ( Permission denied? )\n", argv[2]); return 1; } fprintf(stdout, " _ _ _ ____ \n" " | | | | (_) | _ \\ \n" " | |_| | | | | |_) | \n" " | _ | | | | _ < \n" " |_| |_| |_| |_| \\_\\ \n" "\n" "++++++++++++++++++++++++++++++++++\n" "+ Source Image: `%s'\n" "+ X: %d Y: %d\n" "+ Format: %s\n" "+ Size:%s\n" "++++++++++++++++++++++++++++++++++\n\n", argv[1], x, y, format(argv[1]), size(in)); fprintf(stdout, " Creating Thumb Image...\n"); thumb = gdImageCreateTrueColor(tx, ty + 20 ); white = gdImageColorAllocate(thumb, 255, 255, 255); str = malloc(30); sprintf(str, "%d x %d %s", x, y, size(in)); gdImageString(thumb, gdFontGetLarge(), 3, ty+3, str, white); free(str); gdImageCopyResized(thumb, source, 0, 0, 0, 0, tx, ty, x, y); if ( strstr(argv[2], "png") ) gdImagePng(thumb, out); else { if ( argc == 6 ) gdImageJpeg(thumb, out, atoi(argv[5])); else gdImageJpeg(thumb, out, 100); } fclose(out); fclose(in); gdImageDestroy(thumb); out = fopen(argv[2], "rb"); fprintf(stdout, "\n" "++++++++++++++++++++++++++++++++++\n" "+ Thumb Image: `%s'\n" "+ X: %d Y: %d\n" "+ Format: %s\n" "+ Size:%s\n" "++++++++++++++++++++++++++++++++++\n\n", argv[2], tx, ty+20, format(argv[2]), size(out)); fclose(out); return 0; }