PHP生成缩略图

1 /**

2 * 生成缩略图

3 * @param string 源图绝对完整地址{带文件名及后缀名}

4 * @param string 目标图绝对完整地址{带文件名及后缀名}

5 * @param int 缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}

6 * @param int 缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}

7 * @param int 是否裁切{宽,高必须非0}

8 * @param int/float 缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}

9 * @return boolean

10 */

11 public static function img2Thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)

12 {

13 if(!is_file($src_img))

14 {

15 return false;

16 }

17 //echo 'cccccc'."\n";

18 $ot = CommonMethod::fileext($dst_img);

19 $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);

20 $srcinfo = getimagesize($src_img);

21 $src_w = $srcinfo[0];

22 $src_h = $srcinfo[1];

23 $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));

24 $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);

25

26 $dst_h = $height;

27 $dst_w = $width;

28 $x = $y = 0;

29

30 /**

31 * 缩略图不超过源图尺寸(前提是宽或高只有一个)

32 */

33 if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))

34 {

35 $proportion = 1;

36 }

37 if($width> $src_w)

38 {

39 $dst_w = $width = $src_w;

40 }

41 if($height> $src_h)

42 {

43 $dst_h = $height = $src_h;

44 }

45 if(!$width && !$height && !$proportion)

46 {

47 return false;

48 }

49 if(!$proportion)

50 {

51 if($cut == 0)

52 {

53 if($dst_w && $dst_h)

54 {

55 if($dst_w/$src_w> $dst_h/$src_h)

56 {

57 $dst_w = $src_w * ($dst_h / $src_h);

58 $x = 0 - ($dst_w - $width) / 2;

59 }

60 else

61 {

62 $dst_h = $src_h * ($dst_w / $src_w);

63 $y = 0 - ($dst_h - $height) / 2;

64 }

65 }

66 else if($dst_w xor $dst_h)

67 {

68 if($dst_w && !$dst_h) //有宽无高

69 {

70 $propor = $dst_w / $src_w;

71 $height = $dst_h = $src_h * $propor;

72 }

73 else if(!$dst_w && $dst_h) //有高无宽

74 {

75 $propor = $dst_h / $src_h;

76 $width = $dst_w = $src_w * $propor;

77 }

78 }

79 }

80 else

81 {

82 if(!$dst_h) //裁剪时无高

83 {

84 $height = $dst_h = $dst_w;

85 }

86 if(!$dst_w) //裁剪时无宽

87 {

88 $width = $dst_w = $dst_h;

89 }

90 $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);

91 $dst_w = (int)round($src_w * $propor);

92 $dst_h = (int)round($src_h * $propor);

93 $x = ($width - $dst_w) / 2;

94 $y = ($height - $dst_h) / 2;

95 }

96 }

97 else

98 {

99 $proportion = min($proportion, 1);

100 $height = $dst_h = $src_h * $proportion;

101 $width = $dst_w = $src_w * $proportion;

102 }

103

104 $src = $createfun($src_img);

105 $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);

106 $white = imagecolorallocate($dst, 255, 255, 255);

107 imagefill($dst, 0, 0, $white);

108 //echo '11111'."\n";

109 if(function_exists('imagecopyresampled'))

110 {

111 imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);

112 }

113 else

114 {

115 imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);

116 }

117 //echo '222222'."\n";

118 $otfunc($dst, $dst_img);

119 imagedestroy($dst);

120 imagedestroy($src);

121 return true;

122 }