1. <?php  
2. /**
3. * 生成缩略图函数(支持图片格式:gif、jpeg、png和bmp)
4. * @author ruxing.li
5. * @param string $src 源图片路径
6. * @param int $width 缩略图宽度(只指定高度时进行等比缩放)
7. * @param int $width 缩略图高度(只指定宽度时进行等比缩放)
8. * @param string $filename 保存路径(不指定时直接输出到浏览器)
9. * @return bool
10. */
11. function mkThumbnail($src, $width = null, $height = null, $filename = null) {
12. if (!isset($width) && !isset($height))
13. return false;
14. if (isset($width) && $width <= 0)
15. return false;
16. if (isset($height) && $height <= 0)
17. return false;
18.
19. $size = getimagesize($src);
20. if (!$size)
21. return false;
22.
23. $src_w, $src_h, $src_type) = $size;
24. $src_mime = $size['mime'];
25. switch($src_type) {
26. case 1 :
27. $img_type = 'gif';
28. break;
29. case 2 :
30. $img_type = 'jpeg';
31. break;
32. case 3 :
33. $img_type = 'png';
34. break;
35. case 15 :
36. $img_type = 'wbmp';
37. break;
38. default :
39. return false;
40. }
41.
42. if (!isset($width))
43. $width = $src_w * ($height / $src_h);
44. if (!isset($height))
45. $height = $src_h * ($width / $src_w);
46.
47. $imagecreatefunc = 'imagecreatefrom' . $img_type;
48. $src_img = $imagecreatefunc($src);
49. $dest_img = imagecreatetruecolor($width, $height);
50. $dest_img, $src_img, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
51.
52. $imagefunc = 'image' . $img_type;
53. if ($filename) {
54. $imagefunc($dest_img, $filename);
55. else {
56. 'Content-Type: ' . $src_mime);
57. $imagefunc($dest_img);
58. }
59. $src_img);
60. $dest_img);
61. return true;
62. }
63.
64. $result = mkThumbnail('./IMG_3324.JPG', 147, 147);