1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Image
21: {
22: 23: 24: 25: 26:
27: private static $quality = 75;
28:
29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43:
44: public static function crop($source, $target, $thumbwidth, $thumbheight, $watermark = '')
45: {
46: $info = getimagesize($source);
47: switch ($info['mime']) {
48: case 'image/gif':
49: $o_im = imageCreateFromGIF($source);
50: break;
51: case 'image/jpg':
52: case 'image/jpeg':
53: case 'image/pjpeg':
54: $o_im = self::orient($source);
55: break;
56: case 'image/png':
57: case 'image/x-png':
58: $o_im = imageCreateFromPNG($source);
59: break;
60: default:
61: return false;
62: }
63: $o_wd = @imagesx($o_im);
64: $o_ht = @imagesy($o_im);
65: $wm = $o_wd / $thumbwidth;
66: $hm = $o_ht / $thumbheight;
67: $h_height = $thumbheight / 2;
68: $w_height = $thumbwidth / 2;
69: $t_im = ImageCreateTrueColor($thumbwidth, $thumbheight);
70: $int_width = 0;
71: $int_height = 0;
72: $adjusted_width = $thumbwidth;
73: $adjusted_height = $thumbheight;
74: if ($o_wd > $o_ht) {
75: $adjusted_width = ceil($o_wd / $hm);
76: $half_width = $adjusted_width / 2;
77: $int_width = $half_width - $w_height;
78: if ($adjusted_width < $thumbwidth) {
79: $adjusted_height = ceil($o_ht / $wm);
80: $half_height = $adjusted_height / 2;
81: $int_height = $half_height - $h_height;
82: $adjusted_width = $thumbwidth;
83: $int_width = 0;
84: }
85: } elseif (($o_wd < $o_ht) || ($o_wd == $o_ht)) {
86: $adjusted_height = ceil($o_ht / $wm);
87: $half_height = $adjusted_height / 2;
88: $int_height = $half_height - $h_height;
89: if ($adjusted_height < $thumbheight) {
90: $adjusted_width = ceil($o_wd / $hm);
91: $half_width = $adjusted_width / 2;
92: $int_width = $half_width - $w_height;
93: $adjusted_height = $thumbheight;
94: $int_height = 0;
95: }
96: }
97: ImageCopyResampled($t_im, $o_im, -$int_width, -$int_height, 0, 0, $adjusted_width, $adjusted_height, $o_wd, $o_ht);
98: if ($watermark != '') {
99: $t_im = self::watermarkText($t_im, $watermark);
100: }
101: $ret = @ImageJPEG($t_im, $target, self::$quality);
102: imageDestroy($o_im);
103: imageDestroy($t_im);
104:
105: return $ret;
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115:
116: public static function flip($imgsrc)
117: {
118: $width = imagesx($imgsrc);
119: $height = imagesy($imgsrc);
120: $src_x = $width - 1;
121: $src_y = 0;
122: $src_width = -$width;
123: $src_height = $height;
124: $imgdest = imagecreatetruecolor($width, $height);
125: if (imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) {
126: return $imgdest;
127: }
128:
129: return $imgsrc;
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139:
140: public static function info($src)
141: {
142:
143: $info = getimagesize($src);
144: if ($info && $info[0] > 0 && $info[1] > 0) {
145: return array(
146: 'width' => $info[0],
147: 'height' => $info[1],
148: 'mime' => $info['mime'],
149: );
150: }
151:
152: return false;
153: }
154:
155: 156: 157: 158: 159: 160: 161: 162:
163: public static function orient($source)
164: {
165: $imgsrc = imageCreateFromJPEG($source);
166: if (function_exists('exif_read_data')) {
167:
168: $exif = @exif_read_data($source);
169: if (!isset($exif['Orientation'])) {
170: return $imgsrc;
171: } elseif ($exif['Orientation'] == 2) {
172:
173: $imgsrc = self::flip($imgsrc);
174: } elseif ($exif['Orientation'] == 3) {
175:
176: $imgsrc = imagerotate($imgsrc, 180, 0);
177: } elseif ($exif['Orientation'] == 4) {
178:
179: $imgsrc = self::flip($imgsrc);
180: } elseif ($exif['Orientation'] == 5) {
181:
182: $imgsrc = imagerotate($imgsrc, 270, 0);
183: $imgsrc = self::flip($imgsrc);
184: } elseif ($exif['Orientation'] == 6) {
185:
186: $imgsrc = imagerotate($imgsrc, 270, 0);
187: } elseif ($exif['Orientation'] == 7) {
188:
189: $imgsrc = imagerotate($imgsrc, 90, 0);
190: $imgsrc = self::flip($imgsrc);
191: } elseif ($exif['Orientation'] == 8) {
192:
193: $imgsrc = imagerotate($imgsrc, 90, 0);
194: }
195: }
196:
197: return $imgsrc;
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215:
216: public static function resize($source, $target, $name, $width, $watermark = '')
217: {
218: $info = @getimagesize($source);
219: if ($info[0] > $width || $info[1] > $width) {
220: switch ($info['mime']) {
221: case 'image/gif':
222: $o_im = imageCreateFromGIF($source);
223: break;
224: case 'image/jpg':
225: case 'image/jpeg':
226: case 'image/pjpeg':
227: $o_im = self::orient($source);
228: break;
229: case 'image/png':
230: case 'image/x-png':
231: $o_im = imageCreateFromPNG($source);
232: break;
233: }
234: $o_wd = @imagesx($o_im);
235: $o_ht = @imagesy($o_im);
236: if ($o_wd <= $o_ht) {
237: $h = $width;
238: $w = round($h * $o_wd / $o_ht);
239: } else {
240: $w = $width;
241: $h = round($w * $o_ht / $o_wd);
242: }
243: $t_im = @ImageCreateTrueColor($w, $h);
244: @ImageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $w + 1, $h + 1, $o_wd, $o_ht);
245: if ($watermark != '') {
246: $t_im = self::watermarkText($t_im, $watermark);
247: }
248: $newname = substr($name, 0, strrpos($name, '.')).'.jpg';
249: if (!@ImageJPEG($t_im, $target.$newname, self::$quality)) {
250: $ret = false;
251: } else {
252: $ret['name'] = $newname;
253: $ret['width'] = $w;
254: $ret['height'] = $h;
255: $ret['mime'] = 'image/jpeg';
256: }
257: @imageDestroy($o_im);
258: @imageDestroy($t_im);
259:
260: return $ret;
261: } elseif (@copy($source, $target.$name)) {
262: $ret['name'] = $name;
263: $ret['width'] = $info[0];
264: $ret['height'] = $info[1];
265: $ret['mime'] = $info['mime'];
266:
267: return $ret;
268: }
269:
270: return false;
271: }
272:
273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285:
286: public static function watermarkText($imgsrc, $text, $pos = '', $color = 'CCCCCC', $font_size = 20, $opacity = 50)
287: {
288: $font = ROOT_PATH.'skin/fonts/leelawad.ttf';
289: $offset = 5;
290: $alpha_color = imagecolorallocatealpha($imgsrc, hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)), 127 * (100 - $opacity) / 100);
291: $box = imagettfbbox($font_size, 0, $font, $text);
292: if (preg_match('/center/i', $pos)) {
293: $y = $box[1] + (imagesy($imgsrc) / 2) - ($box[5] / 2);
294: } elseif (preg_match('/bottom/i', $pos)) {
295: $y = imagesy($imgsrc) - $offset;
296: } else {
297: $y = $box[1] - $box[5] + $offset;
298: }
299: if (preg_match('/center/i', $pos)) {
300: $x = $box[0] + (imagesx($imgsrc) / 2) - ($box[4] / 2);
301: } elseif (preg_match('/right/i', $pos)) {
302: $x = $box[0] - $box[4] + imagesx($imgsrc) - $offset;
303: } else {
304: $x = $offset;
305: }
306: imagettftext($imgsrc, $font_size, 0, $x, $y, $alpha_color, $font, $text);
307:
308: return $imgsrc;
309: }
310: }
311: