图片上传处理类已测试

<?php
class upload{
    
    private $dir;                         //附件存放物理目录
    private $time;                        //自定义文件上传时间
    private $allow_types;                 //允许上传附件类型
    private $field;                       //上传控件名称
    private $maxsize;                     //最大允许文件大小,单位为KB
    private $thumb_width;                 //缩略图宽度
    private $thumb_height;                //缩略图高度
    private $watermark_file;               //水印图片地址
    private $watermark_pos;                //水印位置
    private $watermark_trans;             //水印透明度
    
    /**
     * 构造函数
     * @param $types   上传图片类型
     * @param $maxzize 上传文件大小
     * @param $filed   上传表单名称
     * @param $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();    
    }
    
    /**
     * 设置图片存放路径
     * @param  $basedir 图片保存路径
     * @param  $filedir 图片保存子目录
     */
    function set_dir($basedir,$filedir='')
    {
        $dir = $basedir;
        //获取自定义目录
        $dir = str_replace('\','/',$dir);
        $self_dir =str_replace('\','/',dirname(__FILE__));
        if($dir!=$self_dir && strlen($dir) > strlen($self_dir))
        {
            $mydir =  substr($dir,strrpos($dir,'/',-2));
        }
        !is_dir($dir) && @mkdir($dir,0777);
        if(!empty($filedir))
        {
            $filedir = str_replace(array('{y}','{m}','{d}'),
                      array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),
                      strtolower($filedir)
              );
            $this->mydir = $mydir.$filedir.'/';
            $dirs = explode('/',$filedir);
            foreach ($dirs as $d)
            {
                !empty($d) && $dir .=$d.'/';
                !is_dir($dir) && @mkdir($dir,0777);
            }
        }
        $this->dir = $dir;
    }
    
    /**
     * 设置缩略图大小
     * @param  $width  缩略图宽
     * @param  $height 缩略图高
     */
    
    function set_thumb($width=0,$height=0)
    {
        $this->thumb_width =$width;
        $this->thumb_height =$height;
        
    }
    /**
     * 设置水印属性
     * @param  $file  水印文件名
     * @param  $pos   水印位置
     * @param  $trans 水印透明图
     */
    function set_watermark($file,$pos = 0, $trans =80)
    {
        $this->watermark_file = $file;
        $this->watermark_pos = $pos;
        $this->watermark_trans = $trans;
    }
    /**
     * 执行图片上传水印处理
     * @return Ambigous <multitype:, number, string>
     */
    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]['flags'] = 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;
            $files[$key]['relative_dir'] = $this->mydir;
            //保存上传文件并删除临时文件
            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;
    }
    
    /**
     * 生成缩略图
     * @param  $src_file   源图片
     * @param  $thumb_file 缩略图
     * @return boolean     
     */
    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,90); 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;    
    }
    
    /**
     * 设置水印
     * @param $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]-$fileinfo[1];
                            break;
                    default ://随机
                            $dst_x = mt_rank(0,$fileinfo[0]-$wminfo[0]);
                            $dst_y = mt_rank(0,$fileinfo[1]-$wminfo[1]);
                }
                //设定图像的混色模式
                if(function_exists('ImageAlphaBlending')) ImageAlphaBlending($temp_wm,true);
                //保存完整的 alpha 通道信息
                if(function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,true);
                //为图像添加水印
                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);
                
            }
        }
        
    }
    
    
     
     /**
      * 获取文件扩展名
      * @param unknown_type $filename
      * @return string
      */
    function fileext($filename) {
         return strtolower(substr(strrchr($filename,'.'),1,10));
    }

}

?>


<?php    
if (isset($_GET['action']) == 'save') {    
    $up = new upload();    
    $up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}');    
    $up->set_thumb(100,80);
    //$up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90);    
    $fs = $up->execute();
   //var_dump($fs);    
}    
?>    
<html>    
    <head><title>test</title></head>    
    <body style="margin:0;padding:0">    
    <form name="upload" method="post" action="?action=save" enctype="multipart/form-data" style="margin:0">    
        <input type="file" name="attach[]" />    
        <input type="file" name="attach[]" />    
        <input type="submit" name="submit" value="上 传" />    
    </form>    
    </body>    
</html>
原文地址:https://www.cnblogs.com/hubing/p/3437634.html