php 图片处理类(简单易用)

php中文网
发布: 2016-07-25 09:00:20
原创
1279人浏览过
一个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

204

205

206

207

208

209

210

211

212

213

214

215

216

<?php 

 class Image { 

     private $path

     //构造方法用来对图片所在位置进行初使化 

     function __construct($path="./"){ 

     $this->path=rtrim($path, "/")."/"

     

     /* 对图片进行缩放

      *

      * 参数$name: 是需要处理的图片名称

      * 参数$width:是缩放后的宽度

      * 参数$height:是缩放后的高度

      * 参数$qz: 是新图片的名称前缀

      * 返回值:就是缩放后的图片名称,失败则返回false

      *

      */ 

  function thumb($name, $width, $height, $qz="th_"){ 

  //获取图片信息 

  $imgInfo=$this->getInfo($name); //图片的宽度,高度,类型 

  //获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png 

  $srcImg=$this->getImg($name, $imgInfo); 

  //获取计算图片等比例之后的大小, $size["width"], $size["height"] 

  $size=$this->getNewSize($name, $width, $height, $imgInfo); 

  //获取新的图片资源, 处理一下gif透明背景 

  $newImg=$this->kidOfImage($srcImg, $size, $imgInfo); 

  //另存为一个新的图片,返回新的缩放后的图片名称     

  return $this->createNewImage($newImg, $qz.$name, $imgInfo);   

     

       

     private function createNewImage($newImg, $newName, $imgInfo){ 

  switch($imgInfo["type"]){ 

      case 1://gif 

   $result=imageGif($newImg, $this->path.$newName); 

   break

      case 2://jpg 

   $result=imageJPEG($newImg, $this->path.$newName); 

   break

      case 3://png 

   $return=imagepng($newImg, $this->path.$newName); 

   break

  

  imagedestroy($newImg); 

  return $newName

     

       

     private function kidOfImage($srcImg, $size, $imgInfo){ 

  $newImg=imagecreatetruecolor($size["width"], $size["height"]); 

     

  $otsc=imagecolortransparent($srcImg); 

       

  if($otsc >=0 && $otsc <= imagecolorstotal($srcImg)){ 

      $tran=imagecolorsforindex($srcImg, $otsc); 

       

      $newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]); 

       

      imagefill($newImg, 0, 0, $newt); 

       

      imagecolortransparent($newImg, $newt); 

  

       

  imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]); 

       

  imagedestroy($srcImg); 

       

  return $newImg

     

       

     private function getNewSize($name, $width, $height, $imgInfo){ 

  $size["width"]=$imgInfo["width"]; 

  $size["height"]=$imgInfo["height"]; 

       

  //缩放的宽度如果比原图小才重新设置宽度 

  if($width < $imgInfo["width"]){ 

      $size["width"]=$width

  

  //缩放的高度如果比原图小才重新设置高度 

  if($height < $imgInfo["height"]){ 

      $size["height"]=$height

  

       

  //图片等比例缩放的算法 

  if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){ 

      $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]); 

  }else

      $size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]); 

  }    

  return $size;

     

       

     private function getInfo($name){ 

  $data=getImageSize($this->path.$name); 

       

  $imageInfo["width"]=$data[0]; 

  $imageInfo["height"]=$data[1]; 

  $imageInfo["type"]=$data[2]; 

       

  return $imageInfo

     

       

     private function getImg($name, $imgInfo){ 

  $srcPic=$this->path.$name

       

  switch($imgInfo["type"]){ 

      case 1: //gif 

   $img=imagecreatefromgif($srcPic); 

   break

      case 2: //jpg 

   $img=imageCreatefromjpeg($srcPic); 

   break

      case 3: //png 

   $img=imageCreatefrompng($srcPic); 

   break

      default

   return false; 

         

  

       

  return $img

     

     /* 功能:为图片加水印图片

      * 参数$groundName: 背景图片,即需要加水印的图片

      * 参数$waterName: 水钱图片

      * 参数#aterPost:水印位置, 10种状态, 

      *  0为随机位置

      *

      *  1. 为顶端居左  2. 为顶端居中  3 为顶端居右

      *  4  为中部居左  5. 为中部居中  6 为中部居右

      *  7 . 为底端居左 8. 为底端居中, 9. 为底端居右

      *

      * 参数$qz : 是加水印后的图片名称前缀

      * 返回值:就是处理后图片的名称

      *

      */ 

     function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){ 

        

  if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){ 

      $groundInfo=$this->getInfo($groundName); 

      $waterInfo=$this->getInfo($waterName); 

      //水印的位置 

      if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){ 

   echo "水印不应该比背景图片小!"

   return

      

       

      $groundImg=$this->getImg($groundName, $groundInfo); 

      $waterImg=$this->getImg($waterName, $waterInfo); 

       

      $groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo); 

       

      return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo); 

  }else

      echo "图片或水印图片不存在"

      return false; 

  

     

       

     private function copyImage($groundImg, $waterImg, $pos, $waterInfo){ 

  imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]); 

  imagedestroy($waterImg); 

       

  return $groundImg

     

        

     private function position($groundInfo, $waterInfo, $waterPos){ 

  //需要背景比水印图片大 

  if(($groundInfo["width"]< $waterInfo["width"]) ||($groundInfo["height"] < $waterInfo["height"])){ 

      return false; 

  

       

  switch($waterPos){ 

      case 1: 

   $posX=0; 

   $posY=0; 

   break

      case 2: 

   $posX=($groundInfo["width"]-$waterInfo["width"])/2; 

   $posY=0; 

   break

      case 3: 

   $posX=$groundInfo["width"]-$waterInfo["width"]; 

   $posY=0; 

   break

      case 4: 

   $posX=0; 

   $posY=($groundInfo["height"]-$waterInfo["height"]) /2; 

   break

      case 5: 

   $posX=($groundInfo["width"]-$waterInfo["width"])/2; 

   $posY=($groundInfo["height"]-$waterInfo["height"]) /2; 

   break

      case 6: 

   $posX=$groundInfo["width"]-$waterInfo["width"]; 

   $posY=($groundInfo["height"]-$waterInfo["height"]) /2; 

   break

      case 7: 

   $posX=0; 

   $posY=$groundInfo["height"]-$waterInfo["height"]; 

   break

      case 8: 

   $posX=($groundInfo["width"]-$waterInfo["width"])/2; 

   $posY=$groundInfo["height"]-$waterInfo["height"]; 

   break

      case 9: 

   $posX=$groundInfo["width"]-$waterInfo["width"]; 

   $posY=$groundInfo["height"]-$waterInfo["height"]; 

   break

      case 0: 

      default

   $posX=rand(0, ($groundInfo["width"]-$waterInfo["width"])); 

   $posY=rand(0, ($groundInfo["height"]-$waterInfo["height"])); 

   break

  

       

  return array("posX"=>$posX, "posY"=>$posY); 

     }      

 }

登录后复制


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号