PHP Image processing class library and demo sharing _php tutorial

Source: Internet
Author: User
Tags imagejpeg md5 encryption

PHP Image processing class library and demo sharing


Simply write a PHP image processing class library, although the function is relatively small, but at present also useless to too advanced, later used to fill it, or the point of the suggestion add what function, or what needs can tell me, I have time to add, if who has extended to this class library, Also trouble to take out everyone share, the code is now can be used on the line, considering the things are not many, what better advice please tell me, thank you

img.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

/**

* Created by Phpstorm.

* User:mction

* DATE:2015/5/14 0014

* time:15:36

* Simple Image class library, all relative paths in this class are based on the Web site root directory, if you need to modify, modify the constant __webroot__ to

* Function: Specify text content create picture (not Chinese), create captcha picture, create thumbnail, other features to be continued

Method

* Style (array $Options) to set the picture style, reset to default style before each setting

* Create_img_png () creates a Png image, the related property is specified by style

* CREATE_IMG_JPEG () creates a Jpeg image, the related property is specified by the style

* Create_verify () Create CAPTCHA image, related properties specified by style

* Get_verify () Gets the value of the captcha created, MD5 encryption

* LOAD_IMG (string $FilePath) load image, create image source for other method call source, FilePath as image relative path

* CREATE_THUMB (String $FileName, String $FilePath) creates a thumbnail of an image loaded by load_img (), FileName is the saved image prefix, FilePath is the relative path of the saved image. File name not included (example:/uploads/images/thumb/)

*/

if (!defined ("__webroot__")) define ("__webroot__", $_server[' Document_root ']);

Class IMG {

protected $_img; Picture Source

protected $_fileimg; The loaded picture source

protected $_fileinfo; An array of information about the loaded picture

protected $_picinfo; An array of wide and high information about the loaded picture

protected $_rand = ' abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890 '; Random factor

protected $_code = "; Verification Code

Public $Width = 300;//picture default width

Public $Height = 80; Image Default Height

Public $BackgroundColor = "000000";

Public $Font = "/PHPS/PUBLIC/FONT/ARIALNB. TTF "; Default font

public $FontSize = 16; Default font Size

Public $FontColor = "FFFFFF"; Default font Color

Public $Content = "Test Word";

Public $Align = "Left";

Public $Codes = 4; Number of verification codes

Public $Line = 6; Number of interfering lines

Public $LOADERR = '; Error message

Public Function __construct () {}

/** Setting picture Properties

* @param array $Options Property

* @return $this return objects

*/

Public Function Style ($Options) {

$this-_re_set ();

foreach ($Options as $K = = $V) {

if (In_array ($K, Array (' Width ', ' Height ', ' backgroundcolor ', ' Font ', ' FontSize ', ' fontcolor ', ' Content ', ' Align ', ' Codes ', ' line ', ' Snow ')) {

if ($K = = "BackgroundColor" | | $K = = "FontColor") {

if (Preg_match ("# ([a-za-z0-9]{6}) #", $V)) $this, $K = $V;

}else{

$this $K = $V;

}

}

}

return $this;

}

/**

* Reset Properties, no external access available

*/

protected function _re_set () {

$this Width = 100;

$this, Height = 30;

$this-backgroundcolor = "000000";

$this, Font = "/PHPS/PUBLIC/FONT/ARIALNB. TTF ";

$this-FontSize = 16;

$this-fontcolor = "FFFFFF";

$this-Align = "Left";

$this-Codes = 4;

$this line = 6;

}

/**

* Create an image source, add a background, create an image

* @param bool $BGC Specify whether to create a background color and a rectangular block

*/

protected function _create_img_gb ($BGC = True) {

$this-_img = Imagecreatetruecolor ($this Width, $this, Height); Create a background source

if ($BGC) {

Preg_match ("# ([a-za-z0-9]{2}] ([a-za-z0-9]{2}) ([a-za-z0-9]{2}) #", $this-BackgroundColor, $COLORARR); Separates color values into three groups of 16-bit binary numbers

$Color = Imagecolorallocate ($this-_img,hexdec ($COLORARR [1]), Hexdec ($COLORARR [2]), Hexdec ($COLORARR [3])); Add a background color to an IMG image source

Imagefilledrectangle ($this, _img,0, $this, $this, width,0, $Color); Create an image

}

}

/**

* Create random verification code

*/

protected function _create_code () {

$Len = strlen ($this-_rand)-1;

for ($i = 0; $i < $this, Codes; $i + +) {

_code $this, $this---_rand[mt_rand (0, $Len)];

}

}

/**

* Write a string to the image, not currently supported in Chinese

*/

protected function _write_text () {

$FontWidth = Imagefontwidth ($this-FontSize); Gets the width of a character of the font size

Preg_match_all ('/(.) /us ', $this, Content, $TEXTARR); Divide the content by the number of statistics in an array

$FontHeight = Imagefontheight ($this-FontSize); Gets the height of the font size

$X = Ceil (($this Width-($FontWidth * COUNT ($TextArr [0]))/2); Set the distance from the left margin of the X wheelbase

$Y = Ceil ($this, Height + $FontHeight)/2); Set the distance of the top margin of the Y wheelbase

Preg_match ("# ([a-za-z0-9]{2}] ([a-za-z0-9]{2}) ([a-za-z0-9]{2}) #", $this-FontColor, $COLORARR);

$Color = Imagecolorallocate ($this-_img,hexdec ($COLORARR [1]), Hexdec ($COLORARR [2]), Hexdec ($COLORARR [3])); Set Text color

Imagettftext ($this-_img, $this-fontsize,0, $X, $Y, $Color, __webroot__. Font, $this-and Content); Write content

}

/**

* Write the verification code to the image

*/

protected function _write_code () {

$_x = Codes, width/$this, $this Setting the aspect ratio

for ($i = 0; $i < $this, Codes; $i + +) {//Loop Codes times, generate one verification code value at a time

$Color = Imagecolorallocate ($this, _img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156)); Randomly generate the color of the CAPTCHA value

Imagettftext ($this, _img, $this, Fontsize,mt_rand ( -30,30), $_x* $i +mt_rand (1,5), $this-height/1.3, $Color , __webroot__. $this, Font, $this-_code[$i]); Generate a verification code value

}

}

/**

* Write interference lines to the image

*/

protected function _write_line () {//Generate interfering lines

For ($i =0, $i < $this line; $i + +) {

$Color = Imagecolorallocate ($this, _img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));

Imageline ($this, _img,mt_rand (0, $this, width), mt_rand (0, $this, Height), Mt_rand (0, $this-width), mt_ Rand (0, $this, Height), $Color);

}

}

/**

* Set Image type to JPEG

*/

protected function _img_jpeg () {

Header (' Content-type:image/jpeg ');

Imagejpeg ($this-_img);

Imagedestroy ($this-_img);

}

/**

* Set Image type to PNG

*/

protected function _img_png () {

Header (' content-type:image/png ');

Imagepng ($this-_img);

Imagedestroy ($this-_img);

}

/**

* Create a JPEG string image

*/

Public Function create_img_jpg () {

$this-_CREATE_IMG_GB (True);

$this-_write_text ();

$this-_img_jpeg ();

}

/**

* Create a PNG string image

*/

Public Function Create_img_png () {

$this-_CREATE_IMG_GB (True);

$this-_write_text ();

$this-_img_png ();

}

/**

* Create a PNG image of the verification code

*/

Public Function create_verify () {

$this-backgroundcolor = ";

for ($I = 0; $I < 3; $I + +) {

$this, BackgroundColor. = Dechex (Mt_rand (20,155));

}

$this-_CREATE_IMG_GB (True);

$this-_create_code ();

$this-_write_line ();

$this-_write_code ();

$this-_img_png ();

}

/**

* External access to MD5 encrypted verification code

* @return String

*/

Public Function get_verify () {

Return MD5 ($this-_code);

}

/**

* Load an image file and get information about the image

* @param string $FilePath image relative path address

* @return $this |bool successfully returned the object, otherwise it will return false

*/

Public Function load_img ($FilePath) {

$FilePath = __webroot__. $FilePath;

if (!is_file ($FilePath)) {

$this-loaderr = "Path error, file does not exist";

Return False;

}

$this-_picinfo = getimagesize ($FilePath);

$this-_fileinfo = PathInfo ($FilePath);

Switch ($this-_picinfo[2]) {

Case 1: $this->_fileimg = Imagecreatefromgif ($FilePath);

Case 2: $this->_fileimg = Imagecreatefromjpeg ($FilePath);

Case 3: $this->_fileimg = Imagecreatefrompng ($FilePath);

Default: $this-loaderr = "type error, unsupported picture type"; Return False;

}

Return True;

}

/**

* Create thumbnail image

* @param string $FileName The picture name prefix saved

* @param string $FilePath to save the relative path of the picture

* @return Mixed returns an array of information about the resulting picture

*/

Public Function Create_thumb ($FileName, $FilePath) {

$SavePath = __webroot__. $FilePath;

if (!file_exists ($SavePath)) {

mkdir ($SavePath, 0777,true);

}

$FileName = $FileName. Date ("Ymdhis"). Rand (100,999). '. '. $this, _fileinfo[' extension ');

$FilePath = $FilePath. $FileName;

$SavePath = $SavePath. $FileName;

$this-_CREATE_IMG_GB (False);

Imagecopyresampled ($this, _img, $this, _fileimg,0,0,0,0, $this, Width, $this, Height, $this, Picinfo[0], $this-_picinfo[1]);

Switch ($this-_picinfo[2]) {

Case 1:imagegif ($this, _img, $SavePath);

Case 2:imagejpeg ($this, _img, $SavePath);

Case 3:imagepng ($this, _img, $SavePath);

}

$FIleInfo [' FileName '] = $FileName;

$FIleInfo [' FilePath '] = $FilePath;

Return $FIleInfo;

}

}

Using the example

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$IMG = new IMG ();

$Options [' Width '] = 300;

$Options [' Height '] = 100;

$Options [' Content '] = "Test Create Img";

$Options [' fontcolor '] = "FF0000";

$Options [' backgroundcolor '] = "aaaaaa";

$IMG, Style ($Options), create_img_jpg ();

if ($IMG-load_img ("/public/images/ad1.png")) {

$FileInfo = $Img Style (Array (' Width ' =>30, ' Height ' =>30)), Create_thumb ("Thumb", "/uploads/images/");

Var_dump ($FileInfo);

}else{

Die ("Load image failed,". $IMG, Loaderr);

}

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1000074.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000074.html techarticle PHP Image processing class library and demo sharing simply wrote a PHP image processing class library, although the function is relatively small, but at present also useless to too advanced, later used to fill it, or who ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.