Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89764
  • 博文数量: 11
  • 博客积分: 248
  • 博客等级: 二等列兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-10 13:34
文章分类

全部博文(11)

文章存档

2015年(1)

2013年(1)

2012年(9)

我的朋友

分类: 系统运维

2012-03-11 09:37:36

包括批量上传文件,给图片添加水印,生成图像缩略图等功能,附件目录可使用日期参数自定义格式。 
  1. <?php
  2. //-------------------------------------
  3. // 文件说明:文件上传处理类
  4. // 文件作者:Jesse Lee
  5. // 作者主页:
  6. // 最后更新:2008-5-14
  7. //-------------------------------------
  8. class upload {
  9.     var $dir; //附件存放物理目录
  10.     var $time; //自定义文件上传时间
  11.     var $allow_types; //允许上传附件类型
  12.     var $field; //上传控件名称
  13.     var $maxsize; //最大允许文件大小,单位为KB
  14.     var $thumb_width; //缩略图宽度
  15.     var $thumb_height; //缩略图高度
  16.     var $watermark_file; //水印图片地址
  17.     var $watermark_pos; //水印位置
  18.     var $watermark_trans;//水印透明度
  19.     //构造函数
  20.     //$types : 允许上传的文件类型 , $maxsize : 允许大小 , $field : 上传控件名称 , $time : 自定义上传时间
  21.     function upload($types = 'jpg|png', $maxsize = 1024, $field = 'attach', $time = '') {
  22.         $this->allow_types = explode('|',$types);
  23.         $this->maxsize = $maxsize * 1024;
  24.         $this->field = $field;
  25.         $this->time = $time ? $time : time();
  26.     }
  27.     //设置并创建文件具体存放的目录
  28.     //$basedir : 基目录,必须为物理路径
  29.     //$filedir : 自定义子目录,可用参数{y}、{m}、{d}
  30.     function set_dir($basedir,$filedir = '') {
  31.         $dir = $basedir;
  32.         !is_dir($dir) && @mkdir($dir,0777);
  33.         if (!empty($filedir)) {
  34.             $filedir = str_replace(array('{y}','{m}','{y}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));
  35.             $dirs = explode('/',$filedir);
  36.             foreach ($dirs as $d) {
  37.                 !empty($d) && $dir .= $d.'/';
  38.                 !is_dir($dir) && @mkdir($dir,0777);
  39.             }
  40.         }
  41.         $this->dir = $dir;
  42.     }
  43.     //图片缩略图设置,如果不生成缩略图则不用设置
  44.     //$width : 缩略图宽度 , $height : 缩略图高度
  45.     function set_thumb ($width = 0, $height = 0) {
  46.         $this->thumb_width = $width;
  47.         $this->thumb_height = $height;
  48.     }
  49.     //图片水印设置,如果不生成添加水印则不用设置
  50.     //$file : 水印图片 , $pos : 水印位置 , $trans : 水印透明度
  51.     function set_watermark ($file, $pos = 6, $trans = 80) {
  52.         $this->watermark_file = $file;
  53.         $this->watermark_pos = $pos;
  54.         $this->watermark_trans = $trans;
  55.     }
  56.     /*----------------------------------------------------------------
  57.     执行文件上传,处理完返回一个包含上传成功或失败的文件信息数组,
  58.     其中:name 为文件名,上传成功时是上传到服务器上的文件名,上传失败则是本地的文件名
  59.           dir 为服务器上存放该附件的物理路径,上传失败不存在该值
  60.           size 为附件大小,上传失败不存在该值
  61.           flag 为状态标识,1表示成功,-1表示文件类型不允许,-2表示文件大小超出
  62.     ----------------------------------------------------------------- */
  63.     function execute() {
  64.         $files = array(); //成功上传的文件信息
  65.         $field = $this->field;
  66.         $keys = array_keys($_FILES[$field]['name']);
  67.         foreach ($keys as $key) {
  68.             if (!$_FILES[$field]['name'][$key]) continue;
  69.             
  70.             $fileext = $this->fileext($_FILES[$field]['name'][$key]); //获取文件扩展名
  71.             $filename = $this->time.mt_rand(100,999).'.'.$fileext; //生成文件名
  72.             $filedir = $this->dir; //附件实际存放目录
  73.             $filesize = $_FILES[$field]['size'][$key]; //文件大小
  74.             
  75.             //文件类型不允许
  76.             if (!in_array($fileext,$this->allow_types)) {
  77.                 $files[$key]['name'] = $_FILES[$field]['name'][$key];
  78.                 $files[$key]['flag'] = -1;
  79.                 continue;
  80.             }
  81.             //文件大小超出
  82.             if ($filesize > $this->maxsize) {
  83.                 $files[$key]['name'] = $_FILES[$field]['name'][$key];
  84.                 $files[$key]['flag'] = -2;
  85.                 continue;
  86.             }
  87.             $files[$key]['name'] = $filename;
  88.             $files[$key]['dir'] = $filedir;
  89.             $files[$key]['size'] = $filesize;
  90.             //保存上传文件并删除临时文件
  91.             if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) {
  92.                 move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);
  93.                 @unlink($_FILES[$field]['tmp_name'][$key]);
  94.                 $files[$key]['flag'] = 1;
  95.                 //对图片进行加水印和生成缩略图
  96.                 if (in_array($fileext,array('jpg','png','gif'))) {
  97.                     if ($this->thumb_width) {
  98.                         if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) {
  99.                             $files[$key]['thumb'] = 'thumb_'.$filename; //缩略图文件名
  100.                         }
  101.                     }
  102.                     $this->create_watermark($filedir.$filename);
  103.                 }
  104.             }
  105.         }
  106.         return $files;
  107.     }
  108.     //创建缩略图,以相同的扩展名生成缩略图
  109.     //Php.aspx_file : 来源图像路径 , $thumb_file : 缩略图路径
  110.     function create_thumb (Php.aspx_file,$thumb_file) {
  111.         $t_width = $this->thumb_width;
  112.         $t_height = $this->thumb_height;
  113.         if (!file_exists(Php.aspx_file)) return false;
  114.         Php.aspx_info = getImageSize(Php.aspx_file);
  115.         //如果来源图像小于或等于缩略图则拷贝源图像作为缩略图
  116.         if (Php.aspx_info[0] <= $t_width && Php.aspx_info[1] <= $t_height) {
  117.             if (!copy(Php.aspx_file,$thumb_file)) {
  118.                 return false;
  119.             }
  120.             return true;
  121.         }
  122.         //按比例计算缩略图大小
  123.         if (Php.aspx_info[0] - $t_width > Php.aspx_info[1] - $t_height) {
  124.             $t_height = ($t_width / Php.aspx_info[0]) * Php.aspx_info[1];
  125.         } else {
  126.             $t_width = ($t_height / Php.aspx_info[1]) * Php.aspx_info[0];
  127.         }
  128.         //取得文件扩展名
  129.         $fileext = $this->fileext(Php.aspx_file);
  130.         switch ($fileext) {
  131.             case 'jpg' :
  132.                 Php.aspx_img = ImageCreateFromJPEG(Php.aspx_file); break;
  133.             case 'png' :
  134.                 Php.aspx_img = ImageCreateFromPNG(Php.aspx_file); break;
  135.             case 'gif' :
  136.                 Php.aspx_img = ImageCreateFromGIF(Php.aspx_file); break;
  137.         }
  138.         //创建一个真彩色的缩略图像
  139.         $thumb_img = @ImageCreateTrueColor($t_width,$t_height);
  140.         //ImageCopyResampled函数拷贝的图像平滑度较好,优先考虑
  141.         if (function_exists('imagecopyresampled')) {
  142.             @ImageCopyResampled($thumb_img,Php.aspx_img,0,0,0,0,$t_width,$t_height,Php.aspx_info[0],Php.aspx_info[1]);
  143.         } else {
  144.             @ImageCopyResized($thumb_img,Php.aspx_img,0,0,0,0,$t_width,$t_height,Php.aspx_info[0],Php.aspx_info[1]);
  145.         }
  146.         //生成缩略图
  147.         switch ($fileext) {
  148.             case 'jpg' :
  149.                 ImageJPEG($thumb_img,$thumb_file); break;
  150.             case 'gif' :
  151.                 ImageGIF($thumb_img,$thumb_file); break;
  152.             case 'png' :
  153.                 ImagePNG($thumb_img,$thumb_file); break;
  154.         }
  155.         //销毁临时图像
  156.         @ImageDestroy(Php.aspx_img);
  157.         @ImageDestroy($thumb_img);
  158.         return true;
  159.     }
  160.     //为图片添加水印
  161.     //$file : 要添加水印的文件
  162.     function create_watermark ($file) {
  163.         //文件不存在则返回
  164.         if (!file_exists($this->watermark_file) || !file_exists($file)) return;
  165.         if (!function_exists('getImageSize')) return;
  166.         
  167.         //检查GD支持的文件类型
  168.         $gd_allow_types = array();
  169.         if (function_exists('ImageCreateFromGIF')) $gd_allow_types['image/gif'] = 'ImageCreateFromGIF';
  170.         if (function_exists('ImageCreateFromPNG')) $gd_allow_types['image/png'] = 'ImageCreateFromPNG';
  171.         if (function_exists('ImageCreateFromJPEG')) $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG';
  172.         //获取文件信息
  173.         $fileinfo = getImageSize($file);
  174.         $wminfo = getImageSize($this->watermark_file);
  175.         if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) return;
  176.         if (array_key_exists($fileinfo['mime'],$gd_allow_types)) {
  177.             if (array_key_exists($wminfo['mime'],$gd_allow_types)) {
  178.                 
  179.                 //从文件创建图像
  180.                 $temp = $gd_allow_types[$fileinfo['mime']]($file);
  181.                 $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);
  182.                 //水印位置
  183.                 switch ($this->watermark_pos) {
  184.                     case 1 : //顶部居左
  185.                         $dst_x = 0; $dst_y = 0; break;
  186.                     case 2 : //顶部居中
  187.                         $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break;
  188.                     case 3 : //顶部居右
  189.                         $dst_x = $fileinfo[0]; $dst_y = 0; break;
  190.                     case 4 : //底部居左
  191.                         $dst_x = 0; $dst_y = $fileinfo[1]; break;
  192.                     case 5 : //底部居中
  193.                         $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break;
  194.                     case 6 : //底部居右
  195.                         $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break;
  196.                     default : //随机
  197.                         $dst_x = mt_rand(0,$fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]);
  198.                 }
  199.                 if (function_exists('ImageAlphaBlending')) ImageAlphaBlending($temp_wm,True); //设定图像的混色模式
  200.                 if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //保存完整的 alpha 通道信息
  201.                 //为图像添加水印
  202.                 if (function_exists('imageCopyMerge')) {
  203.                     ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);
  204.                 } else {
  205.                     ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);
  206.                 }
  207.                 //保存图片
  208.                 switch ($fileinfo['mime']) {
  209.                     case 'image/jpeg' :
  210.                         @imageJPEG($temp,$file);
  211.                         break;
  212.                     case 'image/png' :
  213.                         @imagePNG($temp,$file);
  214.                         break;
  215.                     case 'image/gif' :
  216.                         @imageGIF($temp,$file);
  217.                         break;
  218.                 }
  219.                 //销毁零时图像
  220.                 @imageDestroy($temp);
  221.                 @imageDestroy($temp_wm);
  222.             }
  223.         }
  224.     }
  225.     //获取文件扩展名
  226.     function fileext($filename) {
  227.         return strtolower(substr(strrchr($filename,'.'),1,10));
  228.     }
  229. }
  230. ?>

  231. 使用示例:

  232. 代码:

  233. <?php
  234. if ($_GET['action'] == 'save') {
  235.    
  236.     $up = new upload();
  237.     $up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}');
  238.     $up->set_thumb(100,80);
  239.     $up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90);
  240.     $fs = $up->execute();
  241.    
  242.     var_dump($fs);
  243. }
  244. ?>
  245. <html>
  246.     <head><title>test</title></head>
  247.     <body style="margin:0;padding:0">
  248.     <form name="upload" method="post" action="?action=save" enctype="multipart/form-data" style="margin:0">
  249.         <input type="file" name="attach[]" />
  250.         <input type="file" name="attach[]" />
  251.         <input type="submit" name="submit" value="上 传" />
  252.     </form>
  253.     </body>
  254. </html>

阅读(2260) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~