|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
/* * "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 <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <gd.h> #include <gdfontt.h> #include <gdfonts.h> #include <gdfontmb.h> #include <gdfontl.h> #include <gdfontg.h> #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; } |