chengkun
2025-04-18 1bb985f32f2efe0f9dd69f3cf29a1c809b1cf96d
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<?php
/*
* $Author :PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2021 宿迁鑫潮信息技术有限公司,并保留所有权利。
*
* 软件声明:未经授权前提下,不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class Upload {
    var $imgname='';
    var $previewname='';                 // 添加这两个变量用于记录,图片全名,以及缩略图全名
    var $upfiledir='/upload';
    var $maxsize='10240';                 //图片最大小KB
    var $addpreview=true;                 //是否生成缩略图
    var $addwatermark=false;             //$addwatermark=1加水印,其它不加水印
    var $watertype='img';                 //水印类型(txt为文字,img为图片)
    var $waterimg='/images/logo.png';    //水印图片
    var $waterstring='www.phpyun.com';     //水印字符
    var $ttf='';                         //默认字体
    var $alpha='50';                     //透明度
    var $position=1;                     //水印位置 1左上,2:上中3:右上4:中左5居中6中右7左下8底中9底右
    var $destination_folder;             //缩略图保存路径,带日期文件夹
    var $errorType; 
    var $pic_type;                       //图片类型
    function Upload($paras=array()){
        if(count($paras)==false){$paras=array('maxsize'=>'10kb','addwatermark'=>true,'watertype'=>'txt/img','waterimg'=>'images/logo.png','waterstring'=>'','ttf'=>'','position'=>'1/2/3/4');}
        foreach($paras as $key => $value){
            $key=strtolower($key);
            $this->$key=$value;
        }
        $this->upfiledir=rtrim($this->upfiledir,'/');
        $this->maxsize=intval($this->maxsize);
 
        //错误类型:无错误为0
        $this->errorType=0;
    }
    /*
        上传图片
        $file
    */
    function picture($file,$pictype=''){
        
        $fileName=$file["name"];
        $filetmpname=$file["tmp_name"];
        
        $uptypes=$this->pic_type;
        $nameArr=@explode(".",$fileName);
        $num=count($nameArr)-1;
        $this->destination_folder=$this->upfiledir.'/'.date('Ymd').'/'; //上传文件路径
        if(!file_exists($this->upfiledir)){
            @mkdir($this->upfiledir,0777,true);
        }
        if(!file_exists($this->destination_folder)){
            @mkdir($this->destination_folder,0777,true);
        }
        $imgpreviewsize=1/2;    //缩略图比例
        if(@filesize($filetmpname) > ($this->maxsize*1024)){//检查文件大小
            return $this->errorType=1;//文件太大
            }
        //in_array($file["type"], $uptypes
        if(!in_array(strtolower($nameArr[$num]),$uptypes)){//检查文件类型
            return $this->errorType=2;//文件类型不符
        }
        if(stripos($nameArr['0'],"<?php")!==false){//检查文件名称是否含有非法字符
            return $this->errorType=6;//非法字符
        }
        
        //$image_size = getimagesize($filetmpname);
        list($width,$height,$ptype,$attr) =getimagesize($filetmpname);
        switch ($ptype)
        {
            case 1:    //gif
                $img = imagecreatefromgif($filetmpname);
                break;
            case 2:    //jpg
                $img = imagecreatefromjpeg($filetmpname);
                break;
            case 3:    //png
                $img = imagecreatefrompng($filetmpname);
                break;
            case 6:    //bmp
                $img = imagecreatefrombmp($filetmpname);
                break;
            default:
                return 0;
                break;
        }
        if (!$img){
            
            return $this->errorType=6;//非法字符
        }
    
        $pinfo  =  pathinfo($fileName);
        $ftype  =  $pinfo['extension'];
        $fname  =  $this->generateImgName($ftype);
        $destination  =  $this->destination_folder.$fname;
        if (file_exists($destination)){
            return $this->errorType=3;//同名文件已经存在了
        }
        // 分辨率限制条件
        $fbl  =  1920;
        if ($pictype == 'logo' && ($height > 300 || $width > 300)){
            // 用户头像,图片太大,需要进行压缩
            $this->imgname = $this->makeThumb($file["tmp_name"], 300, 300, '', false, $fname);
            
            return $this->imgname;
        }elseif ($height > $fbl || $width > $fbl){
            // 图片尺寸过大,直接缩略,创建图片
            $this->imgname = $this->makeThumb($file["tmp_name"], $fbl, $fbl, '', false, $fname);
            
            return $this->imgname;
            
        }elseif(!move_uploaded_file($filetmpname,$destination)){
            
            return $this->errorType=4;//移动文件出错
        }
        $this->imgname=$destination;
        //if($this->addpreview){
        if($ptype != 1 || $this->addpreview){
            if($this->config['is_picthumb']==1){
                $this->imgname = $this->makeThumb($this->imgname,$width,$height,'',true);
            }
        }
        //}
        if($this->addwatermark){
 
            $this->makewatermark($this->imgname);
        }
        return ($this->imgname);
    }
    /**
     * base64图片上传
     */
    function imageBase($data){
 
        preg_match('/^(data:\s*image\/(\w+);base64,)/', $data, $result);
        
        $uimage=str_replace($result[1], '', str_replace('#','+',$data));
        
        if(in_array(strtolower($result[2]),array('jpg','png','gif','jpeg'))){
            $new_file = time().rand(1000,9999).".".$result[2];
        }else{
             $new_file = time().rand(1000,9999).".jpg";
        }
        
        $im = imagecreatefromstring(base64_decode($uimage));
        
        if ($im !== false) {
            
            $this->destination_folder=$this->upfiledir.'/'.date('Ymd').'/'; //上传文件路径
            if(!file_exists($this->upfiledir)){
                @mkdir($this->upfiledir,0777,true);
            }
            if(!file_exists($this->destination_folder)){
                @mkdir($this->destination_folder,0777,true);
            }
 
            $destination =$this->destination_folder.$new_file;
 
            $re=file_put_contents($destination, base64_decode($uimage));
 
            chmod($destination,0777);
 
            $this->imgname=$destination;
 
            if($re){
 
                if(!$this->addpreview && $this->is_picself=='1'){
                    //读取图片信息 判断非法字符
                    $image_data = fread(fopen($this->imgname, 'r'), filesize($this->imgname));
 
                    if(strpos($image_data,'<?php')!==false){
 
                        $this->addpreview = true;
                    }
                }
                if($this->addpreview){
                    list($width,$height,$type,$attr) = getimagesize($this->imgname);
                    $this->imgname = $this->makeThumb($this->imgname,$width,$height,'',$this->addpreview);
                }
            }else{
                return $this->errorType=4;//写入文件出错
            }
        }else{
            return $this->errorType=6;//不合法的图片内容
        }
        if($this->addwatermark){
 
            $this->makewatermark($this->imgname);
        }
        
        return $this->imgname;
         
    }
    //选择生成水印类型(文字或者图片)
    function makewatermark($destination){
        switch($this->watertype){
            case 'txt':   //加水印字符串
           // imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);// 画出水平的字符串
             return $this->waterMarktxt($destination);
            break;
            case 'img':   //加水印图片
            return $this->waterMarkimg($destination);
        break;
        }
    }
    //==========================================
    // 函数: waterMarkimg($destination,$image_size,$destination_folder)
    // 功能: 给图片加水印
    // 参数: $destination 图片文件名
    // 参数: $image_size 大小数组(包含二个字符串)
    //destination_folder文件存放路径
    // 返回: 1 成功 成功时返回生成的图片路径
    //==========================================
    function waterMarkimg($destination)
    {    
        $image_size = getimagesize($destination);
        $iinfo=getimagesize($destination,$iinfo);//取得GIF、JPEG、PNG或SWF图形的大小
        $iinfo2=getimagesize($this->waterimg,$iinfo2);//取得GIF、JPEG、PNG或SWF水印图形的大小
        $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);//建立一个新的图形
        $white=imagecolorallocate($nimage,255,255,255);//分配图形的颜色
        $black=imagecolorallocate($nimage,0,0,0);//分配图形的颜色
        $red=imagecolorallocate($nimage,255,0,0);//分配图形的颜色
        imagefill($nimage,0,0,$white);//将图形着色
        
        switch ($iinfo[2])
        {
            case 1:
            $simage =imagecreatefromgif($destination);//从文件或URL建立一个新的图形
            break;
            case 2:
            $simage =imagecreatefromjpeg($destination);
            break;
            case 3:
            $simage =imagecreatefrompng($destination);
            break;
            case 6:
            $simage =imagecreatefromwbmp($destination);
            break;
            default:$this->errorType=5;//不支持的文件类型
            return;
        }
        imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
       // imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);//建立一个矩形并且填满颜色
        
           switch ($iinfo2[2]){
            case 1:
            $simage1 =imagecreatefromgif($this->waterimg);
            break;
            case 2:
            $simage1 =imagecreatefromjpeg($this->waterimg);
            break;
            case 3:
            $simage1 =imagecreatefrompng($this->waterimg);
            break;
            case 6:
            $simage1 =imagecreatefromwbmp($this->waterimg);
            break;
            default:
 
            $this->errorType=6;//不支持的水印文件类型
            return;
        }
        
        $gifsize=getimagesize($this->waterimg);//取得GIF、JPEG、PNG或SWF图形的大小
       switch($this->position){//水印位置
          case 1:// 上左
          imagecopy($nimage,$simage1,0,0,0,0,$gifsize[0],$gifsize[1]); // 左下
          break;
          case 2:// 上中
          imagecopy($nimage,$simage1,($image_size[0]-$gifsize[0])/2,0,0,0,$gifsize[0],$gifsize[1]); // 左下
          break;
          case 3:// 右上
          imagecopy($nimage,$simage1,$image_size[0]-$gifsize[0],0,0,0,$gifsize[0],$gifsize[1]);
          break;
          case 4:// 中左
          imagecopy($nimage,$simage1,0,($image_size[1]-$gifsize[1])/2,0,0,$gifsize[0],$gifsize[1]); // 左下
          break;
          case 5:// 居中
          imagecopy($nimage,$simage1,($image_size[0]-$gifsize[0])/2, ($image_size[1]-$gifsize[1])/2,0,0,$gifsize[0],$gifsize[1]);
          break;
          case 6:// 中右
          imagecopy($nimage,$simage1,$image_size[0]-$gifsize[0],($image_size[1]-$gifsize[1])/2,0,0,$gifsize[0],$gifsize[1]); // 左下
          break;
          case 9:// 右下
          imagecopy($nimage,$simage1,$image_size[0]-$gifsize[0], $image_size[1]-$gifsize[1],0,0,$gifsize[0],$gifsize[1]);
          break;
          case 7:// 左下
          imagecopy($nimage,$simage1,0,$image_size[1]-$gifsize[1],0,0,$gifsize[0],$gifsize[1]); // 左下
          break;
          case 8:// 底中
          imagecopy($nimage,$simage1,($image_size[0]-$gifsize[0])/2,$image_size[1]-$gifsize[1],0,0,$gifsize[0],$gifsize[1]); // 左下
          break;
        }
       imagedestroy($simage1);// 结束图形
       switch ($iinfo[2]){
            case 1:
            imagejpeg($nimage, $destination);
            break;
            case 2:
            imagejpeg($nimage, $destination);
            break;
            case 3:
            imagepng($nimage, $destination);
            break;
            case 6:
            imagewbmp($nimage, $destination);
            break;
        }
        //覆盖原上传文件
        imagedestroy($nimage);// 结束图形
        imagedestroy($simage);// 结束图形
        return $destination;
    }
    //==========================================
    // 函数: addwatermark($sourFile, $text)
    // 功能: 给图片加水印
    // 参数: $sourFile 图片文件名
    // 参数: $text 文本数组(包含二个字符串)
    //displayPath文件存放路径
    // 返回: 1 成功 成功时返回生成的图片路径
    //==========================================
    function waterMarktxt($sourFile){
         $maxWidth  = 300;            //图片最大宽度
         $maxHeight = 300;            //图片最大高度
         $toFile    = true;            //是否生成文件
        $imageInfo    = $this->getInfo($sourFile);
        switch ($imageInfo["type"])
        {
            case 1:    //gif
            $newName    = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".gif";
                break;
            case 2:    //jpg
            $newName    = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".jpg";
                break;
            case 3:    //png
            $newName    = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".png";
                break;
            default:
                return 0;
                break;
        }
        switch ($imageInfo["type"])
        {
            case 1:    //gif
                $img = imagecreatefromgif($sourFile);
                break;
            case 2:    //jpg
                $img = imagecreatefromjpeg($sourFile);
                break;
            case 3:    //png
                $img = imagecreatefrompng($sourFile);
                break;
            default:
                return 0;
                break;
        }
        if (!$img) {
            return 0;
        }
        $width  = ($maxWidth > $imageInfo["width"]) ? $imageInfo["width"] : $maxWidth;
        $height = ($maxHeight > $imageInfo["height"]) ? $imageInfo["height"] : $maxHeight;
        $srcW    = $imageInfo["width"];
        $srcH    = $imageInfo["height"];
        if ($srcW * $width > $srcH * $height)
            $height = round($srcH * $width / $srcW);
        else
            $width = round($srcW * $height / $srcH);
        //*
        if (function_exists("imagecreatetruecolor")) //GD2.0.1
        {
            $new = imagecreatetruecolor($width, $height);
            if($imageInfo["type"]==1 || $imageInfo["type"]==3){
 
                $color = imagecolorallocate($new,255,255,255);
                imagecolortransparent($new,$color);
                imagefill($new,0,0,$color);
            }
            imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }
        else
        {
            $new = imagecreate($width, $height);
            if($imageInfo["type"]==1 || $imageInfo["type"]==3){
 
                $color = imagecolorallocate($new,255,255,255);
                imagecolortransparent($new,$color);
                imagefill($new,0,0,$color);
            }
            imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }
        $black = imagecolorallocate($new, 0, 0, 0);
        //$alpha = imagecolorallocatealpha($new, 230, 230, 230, 40);
        $alpha = imagecolorallocatealpha($new, 230, 230, 230,$this->alpha);
        //$rectW = max(strlen($text[0]),strlen($text[1]))*7;
        imagefilledrectangle($new, 0, $height-26, $width, $height, $alpha);
        $white = imagecolorallocate ($new, 0, 0, 0);
          imagettftext ($new, 10, 0, 20, $height-8,$white, $this->ttf, $this->waterstring);
        //*/
        if ($toFile)
        {
            if (file_exists($this->destination_folder.$newName))
             @unlink($this->destination_folder.$newName);
            imagejpeg($new, $this->destination_folder.$newName);
            imagedestroy($new);
            imagedestroy($img);
            return $this->destination_folder.$newName;
        }
        else
        {
            imagejpeg($new);
            imagedestroy($new);
            imagedestroy($img);
        }
    }
    //==========================================
    // 函数: getInfo($file)
    // 功能: 返回图像信息
    // 参数: $file 文件名称
    // 返回: 图片信息数组
    //==========================================
    function getInfo($file)
    {
        //$file=$file;
        $data=getimagesize($file);
        $imageInfo["width"]    = $data[0];
        $imageInfo["height"]= $data[1];
        $imageInfo["type"]    = $data[2];
        $imageInfo["name"]    = basename($file);
        $imageInfo["size"]  = filesize($file);
        return $imageInfo;
    }
    //==========================================
    // 函数: makeThumb($sourFile,$width=80,$height=60)
    // 功能: 生成缩略图(输出到浏览器)
    // 参数: $sourFile 图片源文件
    // 参数: $width 生成缩略图的宽度
    // 参数: $height 生成缩略图的高度
    // 返回: 0 失败 成功时返回生成的图片路径
    //==========================================
    function makeThumb($sourFile,$width=100,$height=100,$newNamePre='',$addpreview=false, $zsNewName = '')
    {
        
        $imageInfo    = $this->getInfo($sourFile);
        
        //原图片尺寸
        $srcW    =  $imageInfo["width"];
        $srcH    =  $imageInfo["height"];
        $isBig  =  false;
        //等比缩放
        if(floor($srcW/$srcH) >= 1){
            $width  = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width;
            $height=round($srcH*$width/$srcW);
            //根据比例,加上宽度限制
            if ($height>1080){
                $height = 1080;
                $width=round($srcW*$height/$srcH);
                $isBig  =  true;
            }
        }else{
            $height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height;
            if ($height>1080){
                $height = 1080;
                $isBig  =  true;
            }
            $width=round($srcW*$height/$srcH);
        }
        if ($isBig){
            // 图片尺寸比较大的需要动态设置内存大小
            ini_set("memory_limit", "512M");
        }
        
        if (!empty($zsNewName)){
            // 图片尺寸过大,直接缩略,创建图片所用文件名,与上传时一致,不需要弄新的文件名
            $newName  =  $zsNewName;
            
        }else{
            
            switch ($imageInfo["type"])
            {
                case 1:    //gif
                    $newName  =  'make'.$newNamePre.substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".gif";
                    break;
                case 2:    //jpg
                    $newName  =  'make'.$newNamePre.substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".jpg";
                    break;
                case 3:    //png
                    $newName  =  'make'.$newNamePre.substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".png";
                    break;
                default:
                    return 0;
                    break;
            }
        }
        
        switch ($imageInfo["type"])
        {
            case 1:    //gif
                $img = imagecreatefromgif($sourFile);
                imagesavealpha($img,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
                break;
            case 2:    //jpg
                $img = imagecreatefromjpeg($sourFile);
                break;
            case 3:    //png
                $img = imagecreatefrompng($sourFile);
                imagesavealpha($img,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
                break;
            default:
                return 0;
                break;
        }
        if (!$img){
 
            return 0;
        }
        
        //生成图片
        if (function_exists("imagecreatetruecolor")) //GD2.0.1
        {
            $new = imagecreatetruecolor($width, $height);
            if($imageInfo["type"]==1 || $imageInfo["type"]==3){
                /*
                $color = imagecolorallocate($new,255,255,255);
                imagecolortransparent($new,$color);
                imagefill($new,0,0,$color);
                */
                imagealphablending($new,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
                imagesavealpha($new,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
            }
            ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }else{
            $new = imagecreate($width, $height);
            if($imageInfo["type"]==1 || $imageInfo["type"]==3){
 
                $color = imagecolorallocate($new,255,255,255);
                imagecolortransparent($new,$color);
                imagefill($new,0,0,$color);
            }
            ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }
        //*/
        if($addpreview){
            $this->previewname=$sourFile;
        }else{
            $this->previewname=$this->destination_folder . $newName;
        }
        
        if (file_exists($this->previewname)){
            @unlink($this->previewname);
        }
        if($imageInfo["type"]==3){
            imagepng($new, $this->previewname);
        }elseif($imageInfo["type"]==1){
            imagepng($new, $this->previewname);
        }else{
            ImageJPEG($new, $this->previewname,100);
        }
 
        ImageDestroy($new);
        ImageDestroy($img);
 
        return ($this->previewname);
    }
    function news_makeThumb($sourFile,$width=100,$height=100,$newNamePre=''){
        $imageInfo    = $this->getInfo($sourFile);
        $this->destination_folder=str_replace($imageInfo["name"],"",$sourFile);
        switch ($imageInfo["type"]){
            case 1:    //gif
            $newName    ='make'.$newNamePre.substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".gif";
                break;
            case 2:    //jpg
            $newName    ='make'.$newNamePre.substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".jpg";
                break;
            case 3:    //png
            $newName    ='make'.$newNamePre.substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".png";
                break;
            default:
                return 0;
                break;
        }
        switch ($imageInfo["type"])
        {
            case 1:    //gif
                $img = imagecreatefromgif($sourFile);
                break;
            case 2:    //jpg
                $img = imagecreatefromjpeg($sourFile);
                break;
            case 3:    //png
                $img = imagecreatefrompng($sourFile);
                break;
            default:
                return 0;
                break;
        }
        if (!$img){
            return 0;
        }
        //原图片尺寸
        $srcW    = $imageInfo["width"];
        $srcH    = $imageInfo["height"];
        //等比缩放
        if(floor($srcW/$srcH) >= 1){
            $width  = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width;
            $height=round($srcH*$width/$srcW);
        }else{
            $height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height;
            $width=round($srcW*$height/$srcH);
        }
        //生成图片
        if (function_exists("imagecreatetruecolor")) //GD2.0.1
        {
            //$new = imagecreatetruecolor($width, $height);
            $new = imagecreatetruecolor($width, $height);
            if($imageInfo["type"]==1 || $imageInfo["type"]==3){
 
                $color = imagecolorallocate($new,255,255,255);
                imagecolortransparent($new,$color);
                imagefill($new,0,0,$color);
            }
            ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }else{
            $new = imagecreate($width, $height);
            if($imageInfo["type"]==1 || $imageInfo["type"]==3){
 
                $color = imagecolorallocate($new,255,255,255);
                imagecolortransparent($new,$color);
                imagefill($new,0,0,$color);
            }
            ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }
        //*/
        if (file_exists($this->destination_folder . $newName)){
            unlink($this->destination_folder . $newName);
        }
        
        if($imageInfo["type"]==3){
            imagepng($new, $this->destination_folder . $newName);
        }elseif($imageInfo["type"]==1){
            imagegif($new, $this->destination_folder . $newName);
        }else{
            ImageJPEG($new, $this->destination_folder . $newName,100);
        }
 
        ImageDestroy($new);
        ImageDestroy($img);
        $this->previewname=$this->destination_folder . $newName;
        return ($this->previewname);
    }
    //判断图片是否需要先缩,后上传.
    function getimage($name,$maxwidth=800,$maxheight=600){
        list($width,$height,$type,$attr) =getimagesize($name);
        $imgname=$name;
        if($width>$maxwidth){
           $imgname=$this->makeThumb($name,$maxwidth,$maxheight);//生成缩略图
           unlink($name);//删除原图
        }
        return $imgname;
    }
    function generateImgName($ftype){
        $imgname='';
        $microtime=@explode(" ",microtime());
        $imgname=ceil($microtime[0]*10000000)+$microtime[1];
        $imgname.=rand(1,9);
        $imgname.='.'.$ftype;
        return ($imgname);
    }
}
?>