电脑安全

您当前的位置:主页 > 电脑安全 >

php文件处理类-网络编程

来源:[db:来源]标题:   水印    编辑:电脑技术全能网 时间:2019-09-19 18:28

原标题:php文件处理类-网络编程
   php文件处置类<?php//-------------------------------------//-------------------------------------class upload { var $dir; //附件寄存物理名目 var $time; //自界说文件上传时光 var $allow_types; //同意上传附件范例 var $field; //上传控件称号 var $maxsize; //最大同意文件巨细,单元为KB var $thumb_width; //缩略图宽度 var $thumb_height; //缩略图高度 var $watermark_file; //水印图片地点 var $watermark_pos; //水印地位 var $watermark_trans;//水印通明度 //结构函数 //$types : 同意上传的文件范例 , $maxsize : 同意巨细 , $field : 上传控件称号 , $time : 自界说上传时光 function upload($types = 'jpg|png', $maxsize = 1024, $field = 'attach', $time = '') { $this->allow_types = explode('|',$types); $this->maxsize = $maxsize * 1024; $this->field = $field; $this->time = $time ? $time : time(); } //设置并创立文件详细寄存的名目 //$basedir : 基名目,必需为物理门路 //$filedir : 自界说子名目,可用参数{y}、{m}、{d} function set_dir($basedir,$filedir = '') { $dir = $basedir; !is_dir($dir) && @mkdir($dir,0777); if (!empty($filedir)) { $filedir = str_replace(array('{y}','{m}','{y}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir)); $dirs = explode('/',$filedir); foreach ($dirs as $d) { !empty($d) && $dir .= $d.'/'; !is_dir($dir) && @mkdir($dir,0777); } } $this->dir = $dir; } //图片缩略图设置,假如不天生缩略图则不必设置 //$width : 缩略图宽度 , $height : 缩略图高度 function set_thumb ($width = 0, $height = 0) { $this->thumb_width = $width; $this->thumb_height = $height; } //图片水印设置,假如不天生增加水印则不必设置 //$file : 水印图片 , $pos : 水印地位 , $trans : 水印通明度 function set_watermark ($file, $pos = 6, $trans = 80) { $this->watermark_file = $file; $this->watermark_pos = $pos; $this->watermark_trans = $trans; } /*---------------------------------------------------------------- 履行文件上传,处置完前往一个包括上传胜利或失利的文件信息数组, 此中:name 为文件名,上传胜利时是上传到效劳器上的文件名,上传失利则是当地的文件名 dir 为效劳器上寄存该附件的物理门路,上传失利不存在该值 size 为附件巨细,上传失利不存在该值 flag 为状况标识,1表现胜利,-1表现文件范例不同意,-2表现文件巨细超越 -----------------------------------------------------------------*/ function execute() { $files = array(); //胜利上传的文件信息 $field = $this->field; $keys = array_keys($_FILES[$field]['name']); foreach ($keys as $key) { if (!$_FILES[$field]['name'][$key]) continue; $fileext = $this->fileext($_FILES[$field]['name'][$key]); //猎取文件扩大名 $filename = $this->time.mt_rand(100,999).'.'.$fileext; //天生文件名 $filedir = $this->dir; //附件现实寄存名目 $filesize = $_FILES[$field]['size'][$key]; //文件巨细 //文件范例不同意 if (!in_array($fileext,$this->allow_types)) { $files[$key]['name'] = $_FILES[$field]['name'][$key]; $files[$key]['flag'] = -1; continue; } //文件巨细超越 if ($filesize > $this->maxsize) { $files[$key]['name'] = $_FILES[$field]['name'][$key]; $files[$key]['flag'] = -2; continue; } $files[$key]['name'] = $filename; $files[$key]['dir'] = $filedir; $files[$key]['size'] = $filesize; //保留上传文件并删除常设文件 if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) { move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename); @unlink($_FILES[$field]['tmp_name'][$key]); $files[$key]['flag'] = 1; //对图片停止加水印和天生缩略图 if (in_array($fileext,array('jpg','png','gif'))) { if ($this->thumb_width) { if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) { $files[$key]['thumb'] = 'thumb_'.$filename; //缩略图文件名 } } $this->create_watermark($filedir.$filename); } } } return $files; } //创立缩略图,以雷同的扩大名天生缩略图 //$src_file : 起源图象门路 , $thumb_file : 缩略图门路 function create_thumb ($src_file,$thumb_file) { $t_width = $this->thumb_width; $t_height = $this->thumb_height; if (!file_exists($src_file)) return false; $src_info = getImageSize($src_file); //假如起源图象小于或即是缩略图则拷贝源图象作为缩略图 if ($src_info[0] <= $t_width && $src_info[1] <= $t_height) { if (!copy($src_file,$thumb_file)) { return false; } return true; } //按比例盘算缩略图巨细 if ($src_info[0] - $t_width > $src_info[1] - $t_height) { $t_height = ($t_width / $src_info[0]) * $src_info[1]; } else { $t_width = ($t_height / $src_info[1]) * $src_info[0]; } //获得文件扩大名 $fileext = $this->fileext($src_file); switch ($fileext) { case 'jpg' : $src_img = ImageCreateFromJPEG($src_file); break; case 'png' : $src_img = ImageCreateFromPNG($src_file); break; case 'gif' : $src_img = ImageCreateFromGIF($src_file); break; } //创立一个真黑色的缩略图象 $thumb_img = @ImageCreateTrueColor($t_width,$t_height); //ImageCopyResampled函数拷贝的图象腻滑度较好,优先斟酌 if (function_exists('imagecopyresampled')) { @ImageCopyResampled($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]); } else { @ImageCopyResized($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]); } //天生缩略图 switch ($fileext) { case 'jpg' : ImageJPEG($thumb_img,$thumb_file); break; case 'gif' : ImageGIF($thumb_img,$thumb_file); break; case 'png' : ImagePNG($thumb_img,$thumb_file); break; } //烧毁常设图象 @ImageDestroy($src_img); @ImageDestroy($thumb_img); return true; } //为图片增加水印 //$file : 要增加水印的文件 function create_watermark ($file) { //文件不存在则前往 if (!file_exists($this->watermark_file) || !file_exists($file)) return; if (!function_exists('getImageSize')) return; //检讨GD支撑的文件范例 $gd_allow_types = array(); if (function_exists('ImageCreateFromGIF')) $gd_allow_types['image/gif'] = 'ImageCreateFromGIF'; if (function_exists('ImageCreateFromPNG')) $gd_allow_types['image/png'] = 'ImageCreateFromPNG'; if (function_exists('ImageCreateFromJPEG')) $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG'; //猎取文件信息 $fileinfo = getImageSize($file); $wminfo = getImageSize($this->watermark_file); if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) return; if (array_key_exists($fileinfo['mime'],$gd_allow_types)) { if (array_key_exists($wminfo['mime'],$gd_allow_types)) { //从文件创立图象 $temp = $gd_allow_types[$fileinfo['mime']]($file); $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file); //水印地位 switch ($this->watermark_pos) { case 1 : //顶部居左 $dst_x = 0; $dst_y = 0; break; case 2 : //顶部居中 $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break; case 3 : //顶部居右 $dst_x = $fileinfo[0]; $dst_y = 0; break; case 4 : //底部居左 $dst_x = 0; $dst_y = $fileinfo[1]; break; case 5 : //底部居中 $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break; case 6 : //底部居右 $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break; default : //随机 $dst_x = mt_rand(0,$fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]); } if (function_exists('ImageAlphaBlending')) ImageAlphaBlending($temp_wm,True); //设定图象的混色形式 if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //保留完全的 alpha 通道信息 //为图象增加水印 if (function_exists('imageCopyMerge')) { ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans); } else { ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]); } //保留图片 switch ($fileinfo['mime']) { case 'image/jpeg' : @imageJPEG($temp,$file); break; case 'image/png' : @imagePNG($temp,$file); break; case 'image/gif' : @imageGIF($temp,$file); break; } //烧毁零时图象 @imageDestroy($temp); @imageDestroy($temp_wm); } } } //猎取文件扩大名 function fileext($filename) { return strtolower(substr(strrchr($filename,'.'),1,10)); }}?>