PHP 图片上传并生成缩略图类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?PHP
/**
* 上传图片
*/
class imgUpload{
        static protected $a;
        protected $formName;        //表单名称
        protected $directory;        //文件上传至目录
        protected $maxSize;                //最大文件上传大小
        protected $canUpload;        //是否可以上传
        protected $doUpFile;        //上传的文件名
        protected $sm_File;                //缩略图名称
          
        private function __construct($_formName='file', $_directory='./images/uploads/', $_maxSize=1048576){                        //1024*1024=1M
                //初始化参数
                $this->formName = $_formName;
                $this->directory = $_directory;
                $this->maxSize = $_maxSize;
                $this->canUpload = true;
                $this->doUpFile = '';
                $this->sm_File = '';
        }
          
        //判断图片是否属于允许格式内
        static public function Type($_formName='file'){
                $_type = $_FILES[$_formName]['type'];
                switch ($_type){
                        case 'image/gif':
                                if (self::$a==NULL)
                                        self::$a = new imgUpload($_formName);
                                break;
                        case 'image/pjpeg':
                                if (self::$a==NULL)
                                        self::$a = new imgUpload($_formName);
                                break;
                        case 'image/x-png':
                                if (self::$a==NULL)
                                        self::$a = new imgUpload($_formName);
                                break;
                        default:
                                self::$a = false;
                }
                return self::$a;
        }
          
        //获取文件大小
        public function getSize($_format='K'){
                if ($this->canUpload) {
                        if (0 == $_FILES[$this->formName]['size']) {
                                $this->canUpload = false;
                                return $this->canUpload;
                                break;
                        }
                        switch ($_format){
                                case 'B':
                                        return $_FILES[$this->formName]['size'];
                                        break;
                                case 'K':
                                        return round($_FILES[$this->formName]['size'] / 1024);
                                        break;
                                case 'M':
                                        return round($_FILES[$this->formName]['size'] / (1024*1024),2);
                                        break;
                        }
                }      
        }
          
        //获取文件类型
        public function getExt(){
                if ($this->canUpload) {
                        $_name = $_FILES[$this->formName]['name'];
                        $_nameArr = explode('.',$_name);
                        $_count = count($_nameArr)-1;
                }
                return $_nameArr[$_count];
        }
          
        //获取文件名称
        public function getName(){
                if ($this->canUpload) {
                        return $_FILES[$this->formName]['name'];
                }
        }
          
        //新建文件名
        public function newName(){
                return date('YmdHis').rand(0,9);
        }
          
        //上传文件
        public function upload(){
                if ($this->canUpload)
                {
                        $_getSize = $this->getSize('B');
                        if (!$_getSize)
                        {
                                return $_getSize;
                                break;
                        }
                        else
                        {
                                $_newName = $this->newName();
                                $_ext = $this->getExt();
                                $_doUpload = move_uploaded_file($_FILES[$this->formName]['tmp_name'], $this->directory.$_newName.".".$_ext);
                                if ($_doUpload)
                                {
                                        $this->doUpFile = $_newName;
                                }
                                return $_doUpload;
                        }
                }
        }
          
        //创建缩略图
        public function thumb($_dstChar='_m', $_max_len=320){                //$_dstChar:_m , _s
                if ($this->canUpload && $this->doUpFile != "") {
                        $_ext = $this->getExt();
                        $_srcImage = $this->directory.$this->doUpFile.".".$_ext;
                          
                        //得到图片信息数组
                        $_date = getimagesize($_srcImage, &$info);
                          
                        $src_w = $_date[0];        //源图片宽
                        $src_h = $_date[1];        //源图片高
                        $src_max_len = max($src_w, $src_h);                //求得长边
                        $src_min_len = min($src_w, $src_h);                //求得短边
                        $dst_w = '';                //目标图片宽
                        $dst_h = '';                //目标图片高
                          
                        //宽高按比例缩放,最长边不大于$_max_len
                        if ($src_max_len>$_max_len)
                        {
                                $percent = $src_min_len / $src_max_len;
                                if ($src_w == $src_max_len)
                                {
                                        $dst_w = $_max_len;
                                        $dst_h = $percent * $dst_w;
                                }
                                else
                                {
                                        $dst_h = $_max_len;
                                        $dst_w = $percent * $dst_h;
                                }
                        }
                        else
                        {
                                $dst_w = $src_w;
                                $dst_h = $src_h;
                        }
                          
                        //建立缩略图时,源图片的位置
                        $src_x = '';
                        $src_y = '';
                          
                        //判断如果缩略图用于logo,将对其进行裁减
                        if ('s_' == $_dstChar) {
                                $src_x = $src_w * 0.10;
                                $src_y = $src_h * 0.10;
                                $src_w *= 0.8;
                                $src_h *= 0.8;
                        }
                          
                        //判断图片类型并创建对应新图片
                        switch ($_date[2]){
                                case 1:
                                        $src_im = imagecreatefromgif($_srcImage);
                                        break;
                                case 2:
                                        $src_im = imagecreatefromjpeg($_srcImage);
                                        break;
                                case 3:
                                        $src_im = imagecreatefrompng($_srcImage);
                                        break;
                                case 8:
                                        $src_im = imagecreatefromwbmp($_srcImage);
                                        break;
                        }
                          
                        //创建一幅新图像
                        if ($_date[2]==1) {                //gif无法应用imagecreatetruecolor
                                $dst_im = imagecreate($dst_w, $dst_h);
                        }else {
                                $dst_im = imagecreatetruecolor($dst_w, $dst_h);
                        }
          
                        //对这副图像进行缩略图copy
//                        $bg = imagecolorallocate($dst_im,255,255,0);
                        imagecopyresized($dst_im,$src_im, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
                        //对图片进行抗锯齿操作
                        imageantialias($dst_im, true);
                          
                        switch ($_date[2]) {
                                case 1:
                                        $cr = imagegif($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
                                        break;
                                case 2:
                                        $cr = imagejpeg($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
                                        break;
                                case 3://imagepng有问题,所以在这里用imagejpg代替
                                        $cr = imagejpeg($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
                                        break;
                        }
//                        $cr = imagejpeg($dst_im, $this->directory.$_dstChar.$this->doUpFile, 90);
                        if ($cr) {
                                $this->sm_File = $this->directory.$this->doUpFile.$_dstChar.".".$_ext;
  
                                return $this->sm_File;
                        }else {
                                return false;
                        }
                }
                imagedestroy($dst_im);
                imagedestroy($cr);
        }
          
        //得到上传后的文件名
        public function getUpFile(){
                if ($this->doUpFile!='') {
                        $_ext = $this->getExt();
                        return $this->doUpFile.".".$_ext;
                }else {
                        return false;
                }
        }
          
        //得到上传后的文件全路径
        public function getFilePatch(){
                if ($this->doUpFile!='') {
                        $_ext = $this->getExt();
                        return $this->directory.$this->doUpFile.".".$_ext;
                }else {
                        return false;
                }
        }
          
        //得到缩略图文件全路径
        public function getThumb(){
                if ($this->sm_File!='') {
                        return $this->sm_File;
                }else {
                        return false;
                }
        }
        //得到上传文件的路径
        public function getDirectory(){
                if ($this->directory!='') {
                        return $this->directory;
                }else {
                        return false;
                }
        }
}
?>