1 <?php
2
3 class ImageTool {
4
5 //以宽为标准,如果小于宽,则不剪裁
6 public static function thumb_img_by_width($src_path, $dest_path,$width,$height){
7
8 $info_arr = getimagesize($src_path);
9 $src_width = $info_arr[0];
10 $src_height = $info_arr[1];
11
12 switch( $info_arr['mime'] ) {
13 case 'image/gif':
14 $src_img = imagecreatefromgif( $src_path );
15 break;
16 case 'image/jpeg':
17 $src_img = imagecreatefromjpeg( $src_path );
18 break;
19 case 'image/png':
20 $src_img = imagecreatefrompng( $src_path );
21 break;
22 }
23
24 if($src_width > $width){
25 //裁剪
26 // 改变后的图象的比例
27 $resize_ratio = ($width) / ($height);
28 // 实际图象的比例
29 $ratio = ($src_width) / ($src_height);
30
31 if ($ratio >= $resize_ratio) {
32 // 高度优先
33 //截取图片中间那一段
34 $img_center_x = intval(($src_width-($src_height * $resize_ratio))/2);
35 //如果不需要只截取中间那一段,而是从图片x=0的地方截取,则$img_center_x=0
36 $newimg = @imagecreatetruecolor($width, $height);
37 $color = imagecolorallocate($newimg,255,255,255);
38 imagecolortransparent($newimg,$color);
39 imagefill($newimg,0,0,$color);
40 @imagecopyresampled($newimg, $src_img, 0, 0, $img_center_x, 0, $width, $height, (($src_height) * $resize_ratio), $src_height);
41 // ImageJpeg($newimg);
42 }else{
43 // 宽度优先
44 //截取图片中间那一段
45 $img_center_y = intval(($src_height-($src_width / $resize_ratio))/2);
46 //如果不需要只截取中间那一段,而是从图片y=0的地方截取,则$img_center_y=0
47 $newimg = @imagecreatetruecolor($width, $height);
48 $color = imagecolorallocate($newimg,255,255,255);
49 imagecolortransparent($newimg,$color);
50 imagefill($newimg,0,0,$color);
51 @imagecopyresampled($newimg, $src_img, 0, 0, 0, $img_center_y, $width, $height, $src_width, (($src_width) / $resize_ratio));
52 }
53 }else{
54 //不裁剪
55 $newimg = @imagecreatetruecolor($src_width, $src_height);
56 $color = imagecolorallocate($newimg,255,255,255);
57 imagecolortransparent($newimg,$color);
58 imagefill($newimg,0,0,$color);
59 @imagecopyresampled($newimg, $src_img, 0, 0, 0, 0, $src_width, $src_height, $src_width, $src_height);
60 }
61
62 //统一保存为png格式
63 imagepng($newimg, $dest_path);
64
65 //gc
66 imagedestroy($src_img);
67 imagedestroy($newimg);
68 }
69
70 public static function thumb_img($src_path, $dest_path,$width,$height){
71
72 $info_arr = getimagesize($src_path);
73 $src_width = $info_arr[0];
74 $src_height = $info_arr[1];
75
76 switch( $info_arr['mime'] ) {
77 case 'image/gif':
78 $src_img = imagecreatefromgif( $src_path );
79 break;
80 case 'image/jpeg':
81 $src_img = imagecreatefromjpeg( $src_path );
82 break;
83 case 'image/png':
84 $src_img = imagecreatefrompng( $src_path );
85 break;
86 }
87
88 // 改变后的图象的比例
89 $resize_ratio = ($width) / ($height);
90 // 实际图象的比例
91 $ratio = ($src_width) / ($src_height);
92
93 if ($ratio >= $resize_ratio) {
94 // 高度优先
95 //截取图片中间那一段
96 $img_center_x = intval(($src_width-($src_height * $resize_ratio))/2);
97 //如果不需要只截取中间那一段,而是从图片x=0的地方截取,则$img_center_x=0
98 $newimg = @imagecreatetruecolor($width, $height);
99 $color = imagecolorallocate($newimg,255,255,255);
100 imagecolortransparent($newimg,$color);
101 imagefill($newimg,0,0,$color);
102 @imagecopyresampled($newimg, $src_img, 0, 0, $img_center_x, 0, $width, $height, (($src_height) * $resize_ratio), $src_height);
103 // ImageJpeg($newimg);
104 }else{
105 // 宽度优先
106 //截取图片中间那一段
107 $img_center_y = intval(($src_height-($src_width / $resize_ratio))/2);
108 //如果不需要只截取中间那一段,而是从图片y=0的地方截取,则$img_center_y=0
109 $newimg = @imagecreatetruecolor($width, $height);
110 $color = imagecolorallocate($newimg,255,255,255);
111 imagecolortransparent($newimg,$color);
112 imagefill($newimg,0,0,$color);
113 @imagecopyresampled($newimg, $src_img, 0, 0, 0, $img_center_y, $width, $height, $src_width, (($src_width) / $resize_ratio));
114 }
115
116 //统一保存为png格式
117 imagepng($newimg, $dest_path);
118
119 //gc
120 imagedestroy($src_img);
121 imagedestroy($newimg);
122 }
123
124 /*
125 * 按比例缩放图片,然后居中裁剪
126 * $src_path 源文件路劲
127 * $dest_path 裁剪图片存放位置
128 * $width 裁剪图片宽带
129 * $height 裁剪图片高度
130 */
131 public static function clip_img($src_path, $dest_path, $width, $height) {
132 if( file_exists($src_path) ) {
133 $info_arr = getimagesize($src_path);
134 $src_width = $info_arr[0];
135 $src_height = $info_arr[1];
136 $src_ratio = $src_height / $src_width;
137 $dest_ratio = $height / $width;
138
139 if( $src_ratio < $dest_ratio ) {
140 $ratio_width = $width;
141 $ratio_height = ( $height * $src_height ) / $src_width;
142 }else if( $src_ratio > $dest_ratio ) {
143 $ratio_height = $height;
144 $ratio_width = ( $width * $src_width ) / $src_height;
145 }else {
146 $ratio_height = $height;
147 $ratio_width = $width;
148 }
149
150 switch( $info_arr['mime'] ) {
151 case 'image/gif':
152 $src_img = imagecreatefromgif( $src_path );
153 break;
154 case 'image/jpeg':
155 $src_img = imagecreatefromjpeg( $src_path );
156 break;
157 case 'image/png':
158 $src_img = imagecreatefrompng( $src_path );
159 break;
160 case 'image/bmp':
161 //待处理
162 break;
163 }
164
165 //等比例缩放图片
166 $thumb_img = imagecreatetruecolor( intval($ratio_width), intval($ratio_height) ) ;
167 $color = imagecolorallocate($thumb_img,255,255,255);
168 imagecolortransparent($thumb_img,$color);
169 imagefill($thumb_img,0,0,$color);
170 imagecopyresampled( $thumb_img, $src_img, 0, 0, 0, 0, $ratio_width, $ratio_height, $src_width, $src_height);
171
172 //剪裁
173 $clip_img = imagecreatetruecolor( intval($width), intval($height) );
174 imagecopy( $clip_img, $thumb_img, 0, 0, ( $ratio_width - $width ) / 2, ( $ratio_height - $height ) / 2, $width, $height );
175
176 //统一保存为png格式
177 imagepng($clip_img, $dest_path);
178
179 //gc
180 imagedestroy($src_img);
181 imagedestroy($thumb_img);
182 imagedestroy($clip_img);
183 }
184 }
185
186 /*
187 * 按比例缩放图片
188 * type = 1 按宽为标准缩放 2 按高为标准缩放
189 */
190 public static function scale_img($src_path,$dest_path,$type,$len){
191 if( file_exists($src_path) ){
192 $info_arr = getimagesize($src_path);
193 $src_width = $info_arr[0];
194 $src_height = $info_arr[1];
195 if($type == 1){
196 $des_width = $len;
197 $des_height = ( $src_height * $des_width ) / $src_width;
198 }else{
199 $des_height = $len;
200 $des_width = ( $src_width * $des_height ) / $src_height;
201 }
202
203 switch( $info_arr['mime'] ) {
204 case 'image/gif':
205 $src_img = imagecreatefromgif( $src_path );
206 break;
207 case 'image/jpeg':
208 $src_img = imagecreatefromjpeg( $src_path );
209 break;
210 case 'image/png':
211 $src_img = imagecreatefrompng( $src_path );
212 break;
213 case 'image/bmp':
214 //待处理
215 break;
216 }
217
218 $thumb_img = imagecreatetruecolor( intval($des_width), intval($des_height) );
219 $color = imagecolorallocate($thumb_img,255,255,255);
220 imagecolortransparent($thumb_img,$color);
221 imagefill($thumb_img,0,0,$color);
222 imagecopyresampled( $thumb_img, $src_img, 0, 0, 0, 0, $des_width, $des_height, $src_width, $src_height);
223
224
225 //统一保存为png格式
226 imagepng($thumb_img, $dest_path);
227
228 //gc
229 imagedestroy($src_img);
230 imagedestroy($thumb_img);
231 }
232 }
233
234 }