php 图片类

需开启gd 

index.php

 1 <?php 
 2 include_once('cutPic.class.php');
 3 $i = new cutPic("1.jpg");
 4 
 5 $i->cutOutsidePic("cut1.jpg",800,600,1);
 6 $i->cutOutsidePic("cut2.jpg",800,600,2);
 7 $i->cutOutsidePic("cut3.jpg",800,600,3);
 8 
 9 $i->cutOutsidePic("cut4.jpg",800,600,4);
10 $i->cutOutsidePic("cut5.jpg",800,600,5);
11 $i->cutOutsidePic("cut6.jpg",800,600,6);
12 
13 $i->cutOutsidePic("cut7.jpg",800,600,7);
14 $i->cutOutsidePic("cut8.jpg",800,600,8);
15 $i->cutOutsidePic("cut9.jpg",800,600,9);
16 
17 //$i->cutInsidePic("cut1.jpg",490,304,1);
18 //$i->cutInsidePic("cut2.jpg",490,304,2);
19 //$i->cutInsidePic("cut3.jpg",490,304,3);
20 //
21 //$i->cutInsidePic("cut4.jpg",490,304,4);
22 //$i->cutInsidePic("cut5.jpg",490,304,5);
23 //$i->cutInsidePic("cut6.jpg",490,304,6);
24 //
25 //$i->cutInsidePic("cut7.jpg",490,304,7);
26 //$i->cutInsidePic("cut8.jpg",490,304,8);
27 //$i->cutInsidePic("cut9.jpg",490,304,9);
28 
29 
30 //$i->pastePic("1.png",1);
31 //$i->pastePic("1.png",2);
32 //$i->pastePic("1.png",3);
33 //
34 //$i->pastePic("1.png",4);
35 //$i->pastePic("1.png",5);
36 //$i->pastePic("1.png",6);
37 //
38 //$i->pastePic("2.png",7);
39 //$i->pastePic("2.png",8);
40 //$i->pastePic("2.png",9);
41 
42 //$i->scalePic("800.jpg", 800, 600);
43 
44 ///////////////////////////////////////
45 //$i->cutInsidePic("1024.jpg",1024,768,5);
46 //$i = new cutPic("1024.jpg");
47 //$i->pastePic("1.png",1);
48 //$i->pastePic("2.png",9);
49 
50 ?>
51 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
52 <html xmlns="http://www.w3.org/1999/xhtml">
53 <head>
54 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55 <title></title>
56 </head>
57 
58 <body>
59 </body>
60 </html>

Pic.class.php

 1 <?php
 2 
 3 /***************************
 4 图片操作类 by:Lucky (utf8)
 5 Pic.class.php
 6 
 7 2011-4-23
 8 ***************************/
 9 
10 class Pic{
11 
12   public $fileName;    //源图片
13   public $source;    //源图片资源
14   public $width;    //源图片——宽度
15   public $height;    //源图片——高度
16   public $type;    //源图片——类型
17 
18   function __construct($fileName){
19     list($this->width, $this->height, $this->type)    = getimagesize($fileName);
20     $this->source    = $this->createSource($fileName, $this->type);
21     $this->fileName     = $fileName;
22   }
23 
24   function imagePic($thumb, $target, $t_type){    //生成图片 资源,目标地址,类型【1,2,3,4】
25     switch($t_type){
26       case 1:
27         imagegif($thumb, $target, 100);
28         break;
29       case 2:
30         imagejpeg($thumb, $target, 100);
31         break;
32       case 3:
33         imagepng($thumb, $target, 100);
34         break;
35       //     case 4:
36         //     imagebmp($thumb, $target, 100);
37         //     break;
38       default:
39       $this->errorfun("生成图片失败");
40     }    
41   }
42 
43   function createSource($fileName, $f_type){    //由图像建立资源 源地址,类型【1,2,3,4】
44     switch($f_type){
45       case 1:
46         $source = imagecreatefromgif($fileName);
47         break;
48       case 2:
49         $source = imagecreatefromjpeg($fileName);
50         break;
51       case 3:
52         $source = imagecreatefrompng($fileName);
53         break;
54       case 4:
55         $source = imagecreatefromwbmp($fileName);
56         break;
57       default:
58       $this->errorfun("建立图片资源失败");
59     }
60     return $source;
61   }
62 
63   function createThumb($width, $height){     //建立空白图像资源 宽,高
64     $thumb=imagecreatetruecolor ($width, $height); //建立图像
65     imagefill($thumb, 0, 0, imagecolorallocate($thumb, 255, 255, 255));    //imagefill填充背景颜色//imagecolorallocate设置背景颜色 默认为黑色 此处设置为白色    
66     return $thumb;
67   }
68 
69   function errorfun($e='未知错误!'){
70     $msg='<script language="javascript" type="text/javascript">';
71     $msg.='alert("'.$e.'");';
72     //     $msg.='history.back();';
73     $msg.='</script>';
74     echo $msg;
75     exit;
76   }
77 
78   function __destructor(){
79     $this->fileName     = '';
80     $this->source     = '';
81     $this->width     = '';
82     $this->height     = '';
83     $this->type     = '';
84   }
85 
86 }
87 ?>

cutPic.class.php

  1 <?php
  2 
  3 /***************************
  4 图片操作类 by:Lucky (utf8)
  5 pastePic.class.php
  6 
  7 2011-4-23
  8 ***************************/
  9 
 10 include_once('Pic.class.php');
 11 
 12 class cutPic extends Pic{
 13 
 14   public $thumb;     //临时资源
 15 
 16   function cutInsidePic($target, $t_width, $t_height, $place='1'){    //截取图片内部 新图片资源
 17     // 1 2 3
 18     // 4 5 6
 19     // 7 8 9 
 20     $this->thumb    = $this->createThumb($t_width, $t_height);
 21     switch($place){
 22       case '1':    //上左
 23         imagecopy($this->thumb,$this->source,0,0,0,0,$this->width,$this->height);
 24         break;
 25       case '2':    //上中
 26         imagecopy($this->thumb,$this->source,0,0,($this->width-$t_width)/2,0,$this->width,$this->height);
 27         break;
 28       case '3':    //上右
 29         imagecopy($this->thumb,$this->source,0,0,($this->width-$t_width),0,$this->width,$this->height);
 30         break;
 31       case '4':    //中左
 32         imagecopy($this->thumb,$this->source,0,($t_height-$this->height)/2,0,0,$this->width,$this->height);
 33         break;
 34       case '5':    //中中
 35         imagecopy($this->thumb,$this->source,0,($t_height-$this->height)/2,($this->width-$t_width)/2,0,$this->width,$this->height);
 36         break;
 37       case '6':    //中右
 38         imagecopy($this->thumb,$this->source,0,($t_height-$this->height)/2,($this->width-$t_width),0,$this->width,$this->height);
 39         break;
 40       case '7':    //下左
 41         imagecopy($this->thumb,$this->source,0,($t_height-$this->height),0,0,$this->width,$this->height);
 42         break;
 43       case '8':    //下中
 44         imagecopy($this->thumb,$this->source,0,($t_height-$this->height),($this->width-$t_width)/2,0,$this->width,$this->height);
 45         break;
 46       case '9':    //下右
 47         imagecopy($this->thumb,$this->source,0,($t_height-$this->height),($this->width-$t_width),0,$this->width,$this->height);
 48         break;
 49       default:
 50       $this->errorfun("截取图片内部失败");
 51     }
 52     $this->imagePic($this->thumb, $target, $this->type);
 53   }
 54 
 55   function cutOutsidePic($target, $t_width, $t_height, $place='1'){    //把图片放置新空白图片 目标图, 目标图的宽, 目标图的高
 56     // 1 2 3
 57     // 4 5 6
 58     // 7 8 9 
 59     $this->thumb    = $this->createThumb($t_width, $t_height);
 60     switch($place){
 61       case '1':    //上左
 62         imagecopy($this->thumb,$this->source,0,0,0,0,$this->width,$this->height);
 63         break;
 64       case '2':    //上中
 65         imagecopy($this->thumb,$this->source,($t_width-$this->width)/2,0,0,0,$this->width,$this->height);
 66         break;
 67       case '3':    //上右
 68         imagecopy($this->thumb,$this->source,($t_width-$this->width),0,0,0,$this->width,$this->height);
 69         break;
 70       case '4':    //中左
 71         imagecopy($this->thumb,$this->source,0,($t_height-$this->height)/2,0,0,$this->width,$this->height);
 72         break;
 73       case '5':    //中中
 74         imagecopy($this->thumb,$this->source,($t_width-$this->width)/2,($t_height-$this->height)/2,0,0,$this->width,$this->height);
 75         break;
 76       case '6':    //中右
 77         imagecopy($this->thumb,$this->source,($t_width-$this->width),($t_height-$this->height)/2,0,0,$this->width,$this->height);
 78         break;
 79       case '7':    //下左
 80         imagecopy($this->thumb,$this->source,0,($t_height-$this->height),0,0,$this->width,$this->height);
 81         break;
 82       case '8':    //下中
 83         imagecopy($this->thumb,$this->source,($t_width-$this->width)/2,($t_height-$this->height),0,0,$this->width,$this->height);
 84         break;
 85       case '9':    //下右
 86         imagecopy($this->thumb,$this->source,($t_width-$this->width),($t_height-$this->height),0,0,$this->width,$this->height);
 87         break;
 88       default:
 89       $this->errorfun("把图片放置新空白图片失败");
 90     }
 91     $this->imagePic($this->thumb, $target, $this->type);
 92   }
 93 
 94   function pastePic($water, $place='1'){    //把图片放置新图上
 95     // 1 2 3
 96     // 4 5 6
 97     // 7 8 9 
 98     list($t_width, $t_height, $t_type)    = getimagesize($water);
 99     $this->thumb    = $this->createSource($water, $t_type);
100     switch($place){
101       case '1':    //上左
102         imagecopy($this->source,$this->thumb,0,0,0,0,$t_width,$t_height);
103         break;
104       case '2':    //上中
105         imagecopy($this->source,$this->thumb,($this->width-$t_width)/2,0,0,0,$t_width,$t_height);
106         break;
107       case '3':    //上右
108         imagecopy($this->source,$this->thumb,($this->width-$t_width),0,0,0,$t_width,$t_height);
109         break;
110       case '4':    //中左
111         imagecopy($this->source,$this->thumb,0,($this->height-$t_height)/2,0,0,$t_width,$t_height);
112         break;
113       case '5':    //中中
114         imagecopy($this->source,$this->thumb,($this->width-$t_width)/2,($this->height-$t_height)/2,0,0,$t_width,$t_height);
115         break;
116       case '6':    //中右
117         imagecopy($this->source,$this->thumb,($this->width-$t_width),($this->height-$t_height)/2,0,0,$t_width,$t_height);
118         break;
119       case '7':    //下左
120         imagecopy($this->source,$this->thumb,0,($this->height-$t_height),0,0,$t_width,$t_height);
121         break;
122       case '8':    //下中
123         imagecopy($this->source,$this->thumb,($this->width-$t_width)/2,($this->height-$t_height),0,0,$t_width,$t_height);
124         break;
125       case '9':    //下右
126         imagecopy($this->source,$this->thumb,($this->width-$t_width),($this->height-$t_height),0,0,$t_width,$t_height);
127         break;
128       default:
129       $this->errorfun("合并图片失败");
130     }
131     $this->imagePic($this->source, $this->fileName, $this->type);
132   }
133 
134   function scalePic($target, $t_width, $t_height){
135 
136     $bili = $this->width/$this->height;
137     $t_bili = $t_width/$t_height;
138     $bgcolor = imagecolorallocate($this->thumb, 255, 255, 255);
139     imagefill($this->thumb,0,0,$bgcolor);
140 
141     if($bili > $t_bili){
142       $this->thumb=imagecreatetruecolor ($t_width,$t_width/$bili);
143       imagecopyresampled($this->thumb,$this->source,0,0,0,0,$t_width,$t_width/$bili,$this->width,$this->height);
144     }elseif($bili < $t_bili){
145       $this->thumb=imagecreatetruecolor ($t_height*$bili,$t_height);
146       imagecopyresampled($this->thumb,$this->source,0,0,0,0,$t_height*$bili,$t_height,$this->width,$this->height);
147     }else{
148       $this->thumb=imagecreatetruecolor ($t_width,$t_height);
149       imagecopyresampled($this->thumb,$this->source,0,0,0,0,$t_width,$t_height,$this->width,$this->height);
150     }
151     $this->imagePic($this->thumb, $target, $this->type);
152 
153   }
154 
155   function __destructor(){
156     $this->thumb     = '';
157   }
158 
159 }
160 ?>

 

posted @ 2013-08-28 16:09  竹三戒  阅读(351)  评论(0编辑  收藏  举报