赵祖辉 图片上传类;支持水印-日期文件夹-生成缩略图 ,支持多文件上传,

  1 <?php
2 /*
3 图片上传类; joffe :mailto:gclinux@163.com 欢迎改进
4 版本:2011-12-26 beta;
5 本类支持多文件上传,图片生成略缩图,加水印,按日期生成目录的基本功能
6 调用例子:
7 控制器代码:
8 <?php
9 if ($_GET['action'] == 'save') {
10 $up = new upload(); //创建
11 $up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}'); //保存路径,支持{y}{m}{d}这几个选项
12 $up->set_thumb(100,80); //缩略图大小设置.单位为像素
13 $up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90); //水印设置
14 $fs = $up->execute(); //开始执行
15
16 var_dump($fs); //测试用 查看类的情况
17 }
18 ?>
19 /////视图表单---------
20 <html>
21 <head><title>test</title></head>
22 <body style="margin:0;padding:0″>
23 <form name="upload" method="post" action="?action=save" enctype="multipart/form-data" style="margin:0″>
24 <input type="file" name="attach[]" />
25 <input type="file" name="attach[]" /> //支持多张图片上传
26 <input type="submit" name="submit" value="上 传" />
27 </form>
28 </body>
29 </html>
30
31 */
32 class upload {
33
34 var $dir; //附件存放物理目录
35 var $time; //自定义文件上传时间
36 var $allow_types; //允许上传附件类型
37 var $field; //上传控件名称
38 var $maxsize; //最大允许文件大小,单位为KB
39
40 var $thumb_width; //缩略图宽度
41 var $thumb_height; //缩略图高度
42
43 var $watermark_file; //水印图片地址
44 var $watermark_pos; //水印位置
45 var $watermark_trans;//水印透明度
46
47
48 //构造函数
49 //$types : 允许上传的文件类型 , $maxsize : 允许大小 , $field : 上传控件名称 , $time : 自定义上传时间
50 function upload($types = 'jpg|png', $maxsize = 1024, $field = 'attach', $time = '') {
51 $this->allow_types = explode('|',$types);
52 $this->maxsize = $maxsize * 1024;
53 $this->field = $field;
54 $this->time = $time ? $time : time();
55 }
56
57 //设置并创建文件具体存放的目录
58 //$basedir : 基目录,必须为物理路径
59 //$filedir : 自定义子目录,可用参数{y}、{m}、{d}
60 function set_dir($basedir,$filedir = '') {
61 $dir = $basedir;
62 !is_dir($dir) && @mkdir($dir,0777);
63 if (!empty($filedir)) {
64 $filedir = str_replace(array('{y}','{m}','{d}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));//用string_replace把{y} {m} {d}几个标签进行替换
65 $dirs = explode('/',$filedir);
66 foreach ($dirs as $d) {
67 !empty($d) && $dir .= $d.'/';
68 !is_dir($dir) && @mkdir($dir,0777);
69 }
70 }
71 $this->dir = $dir;
72 }
73
74 //图片缩略图设置,如果不生成缩略图则不用设置
75 //$width : 缩略图宽度 , $height : 缩略图高度
76 function set_thumb ($width = 0, $height = 0) {
77 $this->thumb_width = $width;
78 $this->thumb_height = $height;
79 }
80
81 //图片水印设置,如果不生成添加水印则不用设置
82 //$file : 水印图片 , $pos : 水印位置 , $trans : 水印透明度
83 function set_watermark ($file, $pos = 6, $trans = 80) {
84 $this->watermark_file = $file;
85 $this->watermark_pos = $pos;
86 $this->watermark_trans = $trans;
87 }
88
89 /*—————————————————————-
90 执行文件上传,处理完返回一个包含上传成功或失败的文件信息数组,
91 其中:name 为文件名,上传成功时是上传到服务器上的文件名,上传失败则是本地的文件名
92 dir 为服务器上存放该附件的物理路径,上传失败不存在该值
93 size 为附件大小,上传失败不存在该值
94 flag 为状态标识,1表示成功,-1表示文件类型不允许,-2表示文件大小超出
95 —————————————————————–*/
96 function execute() {
97 $files = array(); //成功上传的文件信息
98 $field = $this->field;
99 $keys = array_keys($_FILES[$field]['name']);
100 foreach ($keys as $key) {
101 if (!$_FILES[$field]['name'][$key]) continue;
102
103 $fileext = $this->fileext($_FILES[$field]['name'][$key]); //获取文件扩展名
104 $filename = date('Ymdhis',$this->time).mt_rand(10,99).'.'.$fileext; //生成文件名
105 $filedir = $this->dir; //附件实际存放目录
106 $filesize = $_FILES[$field]['size'][$key]; //文件大小
107
108 //文件类型不允许
109 if (!in_array($fileext,$this->allow_types)) {
110 $files[$key]['name'] = $_FILES[$field]['name'][$key];
111 $files[$key]['flag'] = -1;
112 continue;
113 }
114
115 //文件大小超出
116 if ($filesize > $this->maxsize) {
117 $files[$key]['name'] = $_FILES[$field]['name'][$key];
118 $files[$key]['name'] = $filesize;
119 $files[$key]['flag'] = -2;
120 continue;
121 }
122
123 $files[$key]['name'] = $filename;
124 $files[$key]['dir'] = $filedir;
125 $files[$key]['size'] = $filesize;
126
127 //保存上传文件并删除临时文件
128 if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) {
129 move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);
130 @unlink($_FILES[$field]['tmp_name'][$key]);
131 $files[$key]['flag'] = 1;
132
133 //对图片进行加水印和生成缩略图,这里演示只支持jpg和png(gif生成的话会没了帧的)
134 if (in_array($fileext,array('jpg','png'))) {
135 if ($this->thumb_width) {
136 if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) {
137 $files[$key]['thumb'] = 'thumb_'.$filename; //缩略图文件名
138 }
139 }
140 $this->create_watermark($filedir.$filename);
141 }
142 }
143 }
144
145 return $files;
146 }
147
148 //创建缩略图,以相同的扩展名生成缩略图
149 //$src_file : 来源图像路径 , $thumb_file : 缩略图路径
150 function create_thumb ($src_file,$thumb_file) {
151 $t_width = $this->thumb_width;
152 $t_height = $this->thumb_height;
153
154 if (!file_exists($src_file)) return false;
155
156 $src_info = getImageSize($src_file);
157
158 //如果来源图像小于或等于缩略图则拷贝源图像作为缩略图,免去操作
159 if ($src_info[0] <= $t_width && $src_info[1] <= $t_height) {
160 if (!copy($src_file,$thumb_file)) {
161 return false;
162 }
163 return true;
164 }
165
166 //按比例计算缩略图大小
167 if (($src_info[0]-$t_width) > ($src_info[1]-$t_height)) {
168 $t_height = ($t_width / $src_info[0]) * $src_info[1];
169 } else {
170 $t_width = ($t_height / $src_info[1]) * $src_info[0];
171 }
172
173 //取得文件扩展名
174 $fileext = $this->fileext($src_file);
175
176 switch ($fileext) {
177 case 'jpg' :
178 $src_img = ImageCreateFromJPEG($src_file); break;
179 case 'png' :
180 $src_img = ImageCreateFromPNG($src_file); break;
181 case 'gif' :
182 $src_img = ImageCreateFromGIF($src_file); break;
183 }
184
185 //创建一个真彩色的缩略图像
186 $thumb_img = @ImageCreateTrueColor($t_width,$t_height);
187
188 //ImageCopyResampled函数拷贝的图像平滑度较好,优先考虑
189 if (function_exists('imagecopyresampled')) {
190 @ImageCopyResampled($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
191 } else {
192 @ImageCopyResized($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
193 }
194
195 //生成缩略图
196 switch ($fileext) {
197 case 'jpg' :
198 ImageJPEG($thumb_img,$thumb_file); break;
199 case 'gif' :
200 ImageGIF($thumb_img,$thumb_file); break;
201 case 'png' :
202 ImagePNG($thumb_img,$thumb_file); break;
203 }
204
205 //销毁临时图像
206 @ImageDestroy($src_img);
207 @ImageDestroy($thumb_img);
208
209 return true;
210
211 }
212
213 //为图片添加水印
214 //$file : 要添加水印的文件
215 function create_watermark ($file) {
216
217 //文件不存在则返回
218 if (!file_exists($this->watermark_file) || !file_exists($file)) return;
219 if (!function_exists('getImageSize')) return;
220
221 //检查GD支持的文件类型
222 $gd_allow_types = array();
223 if (function_exists('ImageCreateFromGIF')) $gd_allow_types['image/gif'] = 'ImageCreateFromGIF';
224 if (function_exists('ImageCreateFromPNG')) $gd_allow_types['image/png'] = 'ImageCreateFromPNG';
225 if (function_exists('ImageCreateFromJPEG')) $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG';
226
227 //获取文件信息
228 $fileinfo = getImageSize($file);
229 $wminfo = getImageSize($this->watermark_file);
230
231 if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) return;
232
233 if (array_key_exists($fileinfo['mime'],$gd_allow_types)) {
234 if (array_key_exists($wminfo['mime'],$gd_allow_types)) {
235
236 //从文件创建图像
237 $temp = $gd_allow_types[$fileinfo['mime']]($file);
238 $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);
239
240 //水印位置
241 switch ($this->watermark_pos) {
242 case 1 : //顶部居左
243 $dst_x = 0; $dst_y = 0; break;
244 case 2 : //顶部居中
245 $dst_x = ($fileinfo[0] - $wminfo[0])/2; $dst_y = 0; break;
246 case 3 : //顶部居右
247 $dst_x = $fileinfo[0]; $dst_y = 0; break;
248 case 4 : //底部居左
249 $dst_x = 0; $dst_y = $fileinfo[1]; break;
250 case 5 : //底部居中
251 $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break;
252 case 6 : //底部居右
253 $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break;
254 default : //随机
255 $dst_x = mt_rand(0,$fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]);
256 }
257
258 if (function_exists('ImageAlphaBlending')) ImageAlphaBlending($temp_wm,True); //设定图像的混色模式
259 if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //保存完整的 alpha 通道信息
260
261 //为图像添加水印
262 if (function_exists('imageCopyMerge')) {
263 ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);
264 } else {
265 ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);
266 }
267
268 //保存图片
269 switch ($fileinfo['mime']) {
270 case 'image/jpeg' :
271 @imageJPEG($temp,$file);
272 break;
273 case 'image/png' :
274 @imagePNG($temp,$file);
275 break;
276 case 'image/gif' :
277 @imageGIF($temp,$file);
278 break;
279 }
280 //销毁零时图像
281 @imageDestroy($temp);
282 @imageDestroy($temp_wm);
283 }
284 }
285 }
286
287 //获取文件扩展名
288 function fileext($filename) {
289 return strtolower(substr(strrchr($filename,'.'),1,10));
290 }
291 }
292 ?>
posted @ 2012-03-16 13:57  mr.coke  阅读(624)  评论(0编辑  收藏  举报