首页 > 后端开发 > php教程 > php上传图片并压缩的方法

php上传图片并压缩的方法

墨辰丷
发布: 2023-03-29 20:52:02
原创
8450 人浏览过

本篇文章主要介绍php上传图片并压缩的方法,感兴趣的朋友参考下,希望对大家有所帮助。

实现代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

<?php

function _UPLOADPIC($upfile, $maxsize, $updir, $newname = &#39;date&#39;) {

   

 if ($newname == &#39;date&#39;)

 $newname = date ( "Ymdhis" ); //使用日期做文件名

 $name = $upfile ["name"];

 $type = $upfile ["type"];

 $size = $upfile ["size"];

 $tmp_name = $upfile ["tmp_name"];

   

 switch ($type) {

 case &#39;image/pjpeg&#39; :

 case &#39;image/jpeg&#39; :

  $extend = ".jpg";

  break;

 case &#39;image/gif&#39; :

  $extend = ".gif";

  break;

 case &#39;image/png&#39; :

  $extend = ".png";

  break;

 }

 if (emptyempty ( $extend )) {

 echo ( "警告!只能上传图片类型:GIF JPG PNG" );

 exit ();

 }

 if ($size > $maxsize) {

 $maxpr = $maxsize / 1000;

 echo ( "警告!上传图片大小不能超过" . $maxpr . "K!" );

 exit ();

 }

 if (move_uploaded_file ( $tmp_name, $updir . $newname . $extend )) {

 return $updir . $newname . $extend;

 }

}

  

function show_pic_scal($width, $height, $picpath) {

 $imginfo = GetImageSize ( $picpath );

 $imgw = $imginfo [0];

 $imgh = $imginfo [1];

   

 $ra = number_format ( ($imgw / $imgh), 1 ); //宽高比

 $ra2 = number_format ( ($imgh / $imgw), 1 ); //高宽比

   

  

 if ($imgw > $width or $imgh > $height) {

 if ($imgw > $imgh) {

  $newWidth = $width;

  $newHeight = round ( $newWidth / $ra );

   

 } elseif ($imgw < $imgh) {

  $newHeight = $height;

  $newWidth = round ( $newHeight / $ra2 );

 } else {

  $newWidth = $width;

  $newHeight = round ( $newWidth / $ra );

 }

 } else {

 $newHeight = $imgh;

 $newWidth = $imgw;

 }

 $newsize [0] = $newWidth;

 $newsize [1] = $newHeight;

   

 return $newsize;

}

  

  

  

function getImageInfo($src)

{

 return getimagesize($src);

}

/**

* 创建图片,返回资源类型

* @param string $src 图片路径

* @return resource $im 返回资源类型

* **/

function create($src)

{

 $info=getImageInfo($src);

 switch ($info[2])

 {

 case 1:

  $im=imagecreatefromgif($src);

  break;

 case 2:

  $im=imagecreatefromjpeg($src);

  break;

 case 3:

  $im=imagecreatefrompng($src);

  break;

 }

 return $im;

}

/**

* 缩略图主函数

* @param string $src 图片路径

* @param int $w 缩略图宽度

* @param int $h 缩略图高度

* @return mixed 返回缩略图路径

* **/

  

function resize($src,$w,$h)

{

 $temp=pathinfo($src);

 $name=$temp["basename"];//文件名

 $dir=$temp["dirname"];//文件所在的文件夹

 $extension=$temp["extension"];//文件扩展名

 $savepath="{$dir}/{$name}";//缩略图保存路径,新的文件名为*.thumb.jpg

  

 //获取图片的基本信息

 $info=getImageInfo($src);

 $width=$info[0];//获取图片宽度

 $height=$info[1];//获取图片高度

 $per1=round($width/$height,2);//计算原图长宽比

 $per2=round($w/$h,2);//计算缩略图长宽比

  

 //计算缩放比例

 if($per1>$per2||$per1==$per2)

 {

 //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先

 $per=$w/$width;

 }

 if($per1<$per2)

 {

 //原图长宽比小于缩略图长宽比,则按照高度优先

 $per=$h/$height;

 }

 $temp_w=intval($width*$per);//计算原图缩放后的宽度

 $temp_h=intval($height*$per);//计算原图缩放后的高度

 $temp_img=imagecreatetruecolor($temp_w,$temp_h);//创建画布

 $im=create($src);

 imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);

 if($per1>$per2)

 {

 imagejpeg($temp_img,$savepath, 100);

 imagedestroy($im);

 return addBg($savepath,$w,$h,"w");

 //宽度优先,在缩放之后高度不足的情况下补上背景

 }

 if($per1==$per2)

 {

 imagejpeg($temp_img,$savepath, 100);

 imagedestroy($im);

 return $savepath;

 //等比缩放

 }

 if($per1<$per2)

 {

 imagejpeg($temp_img,$savepath, 100);

 imagedestroy($im);

 return addBg($savepath,$w,$h,"h");

 //高度优先,在缩放之后宽度不足的情况下补上背景

 }

}

/**

* 添加背景

* @param string $src 图片路径

* @param int $w 背景图像宽度

* @param int $h 背景图像高度

* @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比

* @return 返回加上背景的图片

* **/

function addBg($src,$w,$h,$fisrt="w")

{

 $bg=imagecreatetruecolor($w,$h);

 $white = imagecolorallocate($bg,255,255,255);

 imagefill($bg,0,0,$white);//填充背景

  

 //获取目标图片信息

 $info=getImageInfo($src);

 $width=$info[0];//目标图片宽度

 $height=$info[1];//目标图片高度

 $img=create($src);

 if($fisrt=="wh")

 {

 //等比缩放

 return $src;

 }

 else

 {

 if($fisrt=="w")

 {

  $x=0;

  $y=($h-$height)/2;//垂直居中

 }

 if($fisrt=="h")

 {

  $x=($w-$width)/2;//水平居中

  $y=0;

 }

 imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);

 imagejpeg($bg,$src,100);

 imagedestroy($bg);

 imagedestroy($img);

 return $src;

 }

  

}

  

  

?>

登录后复制

使用方法:

1

2

3

$filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname=&#39;date&#39;));

 $show_pic_scal=show_pic_scal(230, 230, $filename);

 resize($filename,$show_pic_scal[0],$show_pic_scal[1]);

登录后复制

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

基于PHP实现短信验证码接口的方法

PHP实现页面静态化的简单方法分享

PHP中的数据传输CURL实例分析

以上是php上传图片并压缩的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板