用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > php教程

php文件上传类可生成缩略图代码

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