@@ -226,7 +226,84 @@ static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50
226
226
}
227
227
return false ;
228
228
}
229
+ /**
230
+ +----------------------------------------------------------
231
+ * 生成特定尺寸缩略图 解决原版缩略图不能满足特定尺寸的问题 PS:会裁掉图片不符合缩略图比例的部分
232
+ +----------------------------------------------------------
233
+ * @static
234
+ * @access public
235
+ +----------------------------------------------------------
236
+ * @param string $image 原图
237
+ * @param string $type 图像格式
238
+ * @param string $thumbname 缩略图文件名
239
+ * @param string $maxWidth 宽度
240
+ * @param string $maxHeight 高度
241
+ * @param boolean $interlace 启用隔行扫描
242
+ +----------------------------------------------------------
243
+ * @return void
244
+ +----------------------------------------------------------
245
+ */
246
+ static function thumb2 ($ image , $ thumbname , $ type ='' , $ maxWidth =200 , $ maxHeight =50 , $ interlace =true ) {
247
+ // 获取原图信息
248
+ $ info = Image::getImageInfo ($ image );
249
+ if ($ info !== false ) {
250
+ $ srcWidth = $ info ['width ' ];
251
+ $ srcHeight = $ info ['height ' ];
252
+ $ type = empty ($ type ) ? $ info ['type ' ] : $ type ;
253
+ $ type = strtolower ($ type );
254
+ $ interlace = $ interlace ? 1 : 0 ;
255
+ unset($ info );
256
+ $ scale = max ($ maxWidth / $ srcWidth , $ maxHeight / $ srcHeight ); // 计算缩放比例
257
+ //判断原图和缩略图比例 如原图宽于缩略图则裁掉两边 反之..
258
+ if ($ maxWidth / $ srcWidth > $ maxHeight / $ srcHeight ){
259
+ //高于
260
+ $ srcX = 0 ;
261
+ $ srcY = ($ srcHeight - $ maxHeight / $ scale ) / 2 ;
262
+ $ cutWidth = $ srcWidth ;
263
+ $ cutHeight = $ maxHeight / $ scale ;
264
+ }else {
265
+ //宽于
266
+ $ srcX = ($ srcWidth - $ maxWidth / $ scale ) / 2 ;
267
+ $ srcY = 0 ;
268
+ $ cutWidth = $ maxWidth / $ scale ;
269
+ $ cutHeight = $ srcHeight ;
270
+ }
229
271
272
+ // 载入原图
273
+ $ createFun = 'ImageCreateFrom ' . ($ type == 'jpg ' ? 'jpeg ' : $ type );
274
+ $ srcImg = $ createFun ($ image );
275
+
276
+ //创建缩略图
277
+ if ($ type != 'gif ' && function_exists ('imagecreatetruecolor ' ))
278
+ $ thumbImg = imagecreatetruecolor ($ maxWidth , $ maxHeight );
279
+ else
280
+ $ thumbImg = imagecreate ($ maxWidth , $ maxHeight );
281
+
282
+ // 复制图片
283
+ if (function_exists ("ImageCopyResampled " ))
284
+ imagecopyresampled ($ thumbImg , $ srcImg , 0 , 0 , $ srcX , $ srcY , $ maxWidth , $ maxHeight , $ cutWidth , $ cutHeight );
285
+ else
286
+ imagecopyresized ($ thumbImg , $ srcImg , 0 , 0 , $ srcX , $ srcY , $ maxWidth , $ maxHeight , $ cutWidth , $ cutHeight );
287
+ if ('gif ' == $ type || 'png ' == $ type ) {
288
+ //imagealphablending($thumbImg, false);//取消默认的混色模式
289
+ //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
290
+ $ background_color = imagecolorallocate ($ thumbImg , 0 , 255 , 0 ); // 指派一个绿色
291
+ imagecolortransparent ($ thumbImg , $ background_color ); // 设置为透明色,若注释掉该行则输出绿色的图
292
+ }
293
+
294
+ // 对jpeg图形设置隔行扫描
295
+ if ('jpg ' == $ type || 'jpeg ' == $ type )
296
+ imageinterlace ($ thumbImg , $ interlace );
297
+
298
+ // 生成图片
299
+ $ imageFun = 'image ' . ($ type == 'jpg ' ? 'jpeg ' : $ type );
300
+ $ imageFun ($ thumbImg , $ thumbname );
301
+ imagedestroy ($ thumbImg );
302
+ imagedestroy ($ srcImg );
303
+ return $ thumbname ;
304
+ }
305
+ return false ;
306
+ }
230
307
/**
231
308
* 根据给定的字符串生成图像
232
309
* @static
@@ -483,4 +560,4 @@ static function output($im, $type='png', $filename='') {
483
560
imagedestroy ($ im );
484
561
}
485
562
486
- }
563
+ }
0 commit comments