📄 正在查看:twcms/kongphp/ext/image.class.php
大小:7,591 字节 · 修改:2014-01-23 01:42:52 · 行数:253
1<?php
2/**
3 * Copyright (C) 2013-2014 www.kongphp.com All rights reserved.
4 * Licensed http://www.gnu.org/licenses/lgpl.html
5 * Author: wuzhaohuan <kongphp@gmail.com>
6 */
7
8class image{
9 /**
10 * 图片裁切
11 * @param string $src_file 原图路径
12 * @param string $dst_file 目标路径 (裁切后,建议后缀为jpg。null时自动生成目标路径)
13 * @param int $dst_w 目标宽度
14 * @param int $dst_h 目标高度
15 * @param int $type 目标类型 1为补白裁剪 2为居中裁剪 3为上左裁剪 4为按等比缩放,最大不会超过 $dst_w $dst_h
16 * @param int $quality 目标质量
17 */
18 public static function thumb($src_file, $dst_file, $dst_w = 120, $dst_h = 120, $type = 1, $quality = 90) {
19 if(!is_file($src_file)) return FALSE;
20
21 is_null($dst_file) && $dst_file = self::thumb_name($src_file);
22
23 $dst_ext = self::ext($dst_file);
24 if(!in_array($dst_ext, array('jpg', 'gif', 'png'))) return FALSE;
25
26 $imgs = getimagesize($src_file);
27 $src_w = $imgs[0];
28 $src_h = $imgs[1];
29 if(empty($src_w) || empty($src_h)) return FALSE;
30
31 // GD库不支持时,使用原图
32 if(!function_exists('imagecreatefromjpeg')) {
33 return copy($src_file, $dst_file);
34 }
35
36 $im_src = self::load_img($src_file, $imgs['mime']);
37
38 switch($type) {
39 case 1: // 补白裁剪
40 $scale = min($dst_w/$src_w, $dst_h/$src_h);
41 $new_w = round($src_w * $scale);
42 $new_h = round($src_h * $scale);
43 $dst_x = $new_w < $dst_w ? ($dst_w - $new_w)/2 : 0;
44 $dst_y = $new_h < $dst_h ? ($dst_h - $new_h)/2 : 0;
45
46 $im_dst = imagecreatetruecolor($dst_w, $dst_h);
47 imagefill($im_dst, 0, 0 ,0xFFFFFF);
48 imagecopyresampled($im_dst, $im_src, $dst_x, $dst_y, 0, 0, $new_w, $new_h, $src_w, $src_h);
49 break;
50 case 2: // 居中裁剪
51 $scale = max($dst_w/$src_w, $dst_h/$src_h);
52 $new_w = round($dst_w/$scale);
53 $new_h = round($dst_h/$scale);
54 $x = ($src_w - $new_w)/2;
55 $y = ($src_h - $new_h)/2;
56
57 $im_dst = imagecreatetruecolor($dst_w, $dst_h);
58 imagecopyresampled($im_dst, $im_src, 0, 0, $x, $y, $dst_w, $dst_h, $new_w, $new_h);
59 break;
60 case 3: // 上左裁剪
61 $scale = max($dst_w/$src_w, $dst_h/$src_h);
62 $new_w = round($dst_w/$scale);
63 $new_h = round($dst_h/$scale);
64
65 $im_dst = imagecreatetruecolor($dst_w, $dst_h);
66 imagecopyresampled($im_dst, $im_src, 0, 0, 0, 0, $dst_w, $dst_h, $new_w, $new_h);
67 break;
68 default: // 按等比缩放,最大不会超过 $dst_w $dst_h
69 $src_scale = $src_w/$src_h;
70 $scale_w = $dst_w/$src_w;
71 $scale_h = $dst_h/$src_h;
72 $scale = min($scale_w, $scale_h);
73 $new_w = round($dst_w/$scale);
74 $new_h = round($dst_h/$scale);
75
76 if($scale_w >= $scale_h) {
77 $new_dst_w = round($dst_h * $src_scale);
78 $new_dst_h = $dst_h;
79 }else{
80 $new_dst_w = $dst_w;
81 $new_dst_h = round($dst_w * $src_scale);
82 }
83
84 $im_dst = imagecreatetruecolor($new_dst_w, $new_dst_h);
85 imagecopyresampled($im_dst, $im_src, 0, 0, 0, 0, $dst_w, $dst_h, $new_w, $new_h);
86 }
87
88 switch($dst_ext) {
89 case 'jpg': imagejpeg($im_dst, $dst_file, $quality); break;
90 case 'gif': imagegif($im_dst, $dst_file); break;
91 case 'png': imagepng($im_dst, $dst_file); break;
92 }
93
94 imagedestroy($im_dst);
95 imagedestroy($im_src);
96 return TRUE;
97 }
98
99 /**
100 * 图片水印
101 * @param string $src_file 原图路径
102 * @param string $wat_file 水印路径 (建议使用png图片)
103 * @param string $dst_file 目标路径 (null为覆盖原图)
104 * @param int $pos 水印位置
105 * @param int $pct 水印透明度
106 */
107 public static function watermark($src_file, $wat_file, $dst_file = null, $pos = 0, $pct = 80){
108 $src_ext = self::ext($src_file);
109 if(!in_array($src_ext, array('jpg', 'jpeg', 'gif', 'png'))) return FALSE;
110
111 is_null($dst_file) && $dst_file = &$src_file;
112
113 // 动画图片不加水印
114 if($src_ext == 'gif' && self::check_animation($src_file)) {
115 copy($src_file, $dst_file);
116 return filesize($dst_file);
117 }
118
119 $wat_ext = self::ext($wat_file);
120 if(!in_array($wat_ext, array('jpg', 'gif', 'png'))) return FALSE;
121
122 if(!function_exists('imagecopy')) return FALSE;
123
124 $srcs = getimagesize($src_file);
125 $wats = getimagesize($wat_file);
126 if(empty($srcs[0]) || empty($wats[0])) return FALSE;
127
128 // 加载原图
129 $im_src = self::load_img($src_file, $srcs['mime']);
130
131 // 加载水印图
132 $im_wat = self::load_img($wat_file, $wats['mime']);
133
134 $src_w = $srcs[0];
135 $src_h = $srcs[1];
136 $wat_w = $wats[0];
137 $wat_h = $wats[1];
138 $wat_w = $wat_w > $src_w ? $src_w : $wat_w;
139 $wat_h = $wat_h > $src_h ? $src_h : $wat_h;
140
141 // 水印位置
142 switch($pos){
143 case 1: //顶端居左
144 $x = 0;
145 $y = 0;
146 break;
147 case 2: //顶端居中
148 $x = ($src_w - $wat_w) / 2;
149 $y = 0;
150 break;
151 case 3: //顶端居右
152 $x = $src_w - $wat_w;
153 $y = 0;
154 break;
155 case 4: //中部居左
156 $x = 0;
157 $y = ($src_h - $wat_h) / 2;
158 break;
159 case 5: //中部居中
160 $x = ($src_w - $wat_w) / 2;
161 $y = ($src_h - $wat_h) / 2;
162 break;
163 case 6: //中部居右
164 $x = $src_w - $wat_w;
165 $y = ($src_h - $wat_h) / 2;
166 break;
167 case 7: //底端居左
168 $x = 0;
169 $y = $src_h - $wat_h;
170 break;
171 case 8: //底端居中
172 $x = ($src_w - $wat_w) / 2;
173 $y = $src_h - $wat_h;
174 break;
175 case 9: //底端居右
176 $x = $src_w - $wat_w;
177 $y = $src_h - $wat_h;
178 break;
179 default: //随机
180 $x = rand(0, ($src_w - $wat_w));
181 $y = rand(0, ($src_h - $wat_h));
182 }
183
184 if($wat_ext == 'png') {
185 imagecopy($im_src, $im_wat, $x, $y, 0, 0, $wat_w, $wat_h);
186 }else{
187 imagealphablending($im_src, true);
188 imagecopymerge($im_src, $im_wat, $x, $y, 0, 0, $wat_w, $wat_h, $pct);
189 }
190
191 switch($srcs['mime']) {
192 case 'image/jpeg': imagejpeg($im_src, $dst_file, 100); break;
193 case 'image/gif': imagegif($im_src, $dst_file); break;
194 case 'image/png': imagepng($im_src, $dst_file); break;
195 }
196
197 imagedestroy($im_src);
198 imagedestroy($im_wat);
199 return filesize($dst_file);
200 }
201
202 // 生成缩略图名字
203 public static function thumb_name($filename) {
204 return substr($filename, 0, strrpos($filename, '.')).'_thumb.jpg';
205 }
206
207 // 获取文件扩展名
208 public static function ext($filename) {
209 return strtolower(substr(strrchr($filename, '.'), 1, 10));
210 }
211
212 // 检查是否是动画图片
213 public static function check_animation($filename) {
214 $fp = fopen($filename, 'rb');
215 $s = fread($fp, filesize($filename));
216 fclose($fp);
217 return strpos($s, 'NETSCAPE2.0') === FALSE ? 0 : 1;
218 }
219
220 // 加载图片资源
221 public static function load_img($src_file, $mime) {
222 switch($mime) {
223 case 'image/jpeg':
224 $im_src = imagecreatefromjpeg($src_file);
225 !$im_src && $im_src = imagecreatefromgif($src_file);
226 break;
227 case 'image/gif':
228 $im_src = imagecreatefromgif($src_file);
229 !$im_src && $im_src = imagecreatefromjpeg($src_file);
230 break;
231 case 'image/png':
232 $im_src = imagecreatefrompng($src_file);
233 break;
234 case 'image/wbmp':
235 $im_src = imagecreatefromwbmp($src_file);
236 break;
237 default:
238 return FALSE;
239 }
240 return $im_src;
241 }
242}
243
244/*
245用法:
246image::thumb('img/1.jpg', 'img/1_1.jpg', 120, 120, 1);
247image::thumb('img/1.jpg', 'img/1_2.jpg', 120, 120, 2);
248image::thumb('img/1.jpg', 'img/1_3.jpg', 120, 120, 3);
249
250image::watermark('img/2.gif', 'img/water.gif', 'img/2_new.gif', 9, 80);
251image::watermark('img/2.gif', 'img/water.gif', null, 9, 80);
252*/
253