PHP image processing class library and demo _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
PHP image processing class library and demonstration sharing. PHP image processing class library and demo sharing a simple PHP image processing class library. although there are few functions, it is not very advanced yet. I will try it later, or which PHP image processing class library and demonstration?

I simply wrote a PHP image processing class library. although it has few functions, it is not very advanced at present. I will use it later, or which of the following functions can I add to my suggestion, if you want to extend the class library, I have time to add it. if you want to extend the class library, you can share it with me. the code can be used now, there are not many things to consider. please let me know if you have any better suggestions. thank you.

Img. 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

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:

* Simple Image class library. all relative paths in this class are based on the website root directory. to modify them, modify the constant _ WEBROOT _.

* Function: specify the text content to create an Image (Chinese characters not supported), create a verification code Image, create a thumbnail, and other functions to be continued

* Method:

* Style (array $ Options) sets the image Style. it is reset to the default Style before each setting.

* Create_Img_Png (): create a PNG image. the relevant attributes are specified by the Style.

* Create_Img_Jpeg (): Creates a JPEG image. the attributes are specified by the Style.

* Create_Verify () creates a verification code Image. the relevant attributes are specified by the Style.

* Get_Verify () obtains the created verification code value, which is MD5 encrypted.

* Load_Img (string $ FilePath) loads the image and creates an image source for other methods to call. the FilePath is the relative path of the image.

* Create_Thumb (string $ FileName, string $ FilePath): create a thumbnail of the image loaded by Load_Img (). FileName is the prefix of the saved image, and FilePath is the relative path of the saved image, does not contain file names (for example,/Uploads/images/Thumb /)

*/

If (! Defined ("_ WEBROOT _") define ("_ WEBROOT _", $ _ SERVER ['document _ root']);

Class Img {

Protected $ _ Img; // Image Source

Protected $ _ FileImg; // the uploaded image source.

Protected $ _ FileInfo; // array of loaded image information

Protected $ _ PicInfo; // array of the width and height of the loaded image

Protected $ _ Rand = 'abcdefghijkmnopqrstuvwxyzabcdefghjklmnopqrstuvwxyz1234567890'; // random factor

Protected $ _ Code = ''; // verification Code

Public $ Width = 300; // Default image Width

Public $ Height = 80; // The default Height of the image

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 (){}

/** Set Image attributes

* @ Param array $ Options attribute array

* @ Return $ this returns the object

*/

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 ', 'Code', '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;

}

/**

* Resetting properties does not provide external access

*/

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, and create an image

* @ Param bool $ BGC specifies whether to create the background color and rectangular block.

*/

Protected function _ Create_Img_GB ($ BGC = True ){

$ This-> _ Img = imagecreatetruecolor ($ this-> Width, $ this-> Height); // create a back scene Source

If ($ BGC ){

Preg_match ("# ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) #", $ this-> BackgroundColor, $ ColorArr); // separate the color values into three groups of hexadecimal numbers.

$ Color = imagecolorallocate ($ this-> _ Img, hexdec ($ ColorArr [1]), hexdec ($ ColorArr [2]), hexdec ($ ColorArr [3]); // add the background color to the Img Image Source

Imagefilledrectangle ($ this-> _ Img, 0, $ this-> Height, $ this-> Width, 0, $ Color); // create an image

}

}

/**

* Create a random verification code

*/

Protected function _ Create_Code (){

$ Len = strlen ($ this-> _ Rand)-1;

For ($ I = 0; $ I <$ this-> Codes; $ I ++ ){

$ This-> _ Code. = $ this-> _ Rand [mt_rand (0, $ Len)];

}

}

/**

* Writing a string to an image does not support Chinese characters.

*/

Protected function _ Write_Text (){

$ FontWidth = imagefontwidth ($ this-> FontSize); // Obtain the width of a character in the font size.

Preg_match_all ('/(.)/us', $ this-> Content, $ TextArr); // separate the Content into an array to count the number of times.

$ FontHeight = imagefontheight ($ this-> FontSize); // Obtain the font size height.

$ X = ceil ($ this-> Width-($ FontWidth * count ($ TextArr [0])/2); // set the distance between the X axis and the left

$ Y = ceil ($ this-> Height + $ FontHeight)/2); // you can specify the distance between the Y axis and the top margin.

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 the text color

Imagettftext ($ this-> _ Img, $ this-> FontSize, 0, $ X, $ Y, $ Color ,__ WEBROOT __. $ this-> Font, $ this-> Content); // write Content

}

/**

* Write a verification code to the image.

*/

Protected function _ Write_Code (){

$ _ X = $ this-> Width/$ this-> Codes; // you can specify the aspect ratio.

For ($ I = 0; $ I <$ this-> Codes; $ I ++) {// cyclically Codes times, each time a verification code value is generated

$ Color = imagecolorallocate ($ this-> _ Img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); // Color of the randomly generated verification code 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]); // generates a verification Code value.

}

}

/**

* Write interference lines to the image

*/

Protected function _ Write_Line () {// generates interference 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 the image type to JPEG.

*/

Protected function _ Img_Jpeg (){

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

Imagejpeg ($ this-> _ Img );

Imagedestroy ($ this-> _ Img );

}

/**

* Set the 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-& gt; BackgroundColor. = dechex (mt_rand (20,155 ));

}

$ This-> _ Create_Img_GB (True );

$ This-> _ Create_Code ();

$ This-> _ Write_Line ();

$ This-> _ Write_Code ();

$ This-> _ Img_Png ();

}

/**

* Obtain the MD5 encrypted verification code externally

* @ Return string

*/

Public function Get_Verify (){

Return md5 ($ this-> _ Code );

}

/**

* Load an image file and obtain image-related information

* @ Param string $ FilePath relative path of the image

* @ Return $ this | the bool returns an object successfully; otherwise, FALSE is returned.

*/

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); break;

Case 2: $ this-> _ FileImg = imagecreatefromjpeg ($ FilePath); break;

Case 3: $ this-> _ FileImg = imagecreatefrompng ($ FilePath); break;

Default: $ this-> LoadErr = "type error, unsupported image type"; Return False;

}

Return True;

}

/**

* Create a thumbnail

* @ Param string $ prefix of the image name saved by FileName

* @ Param string $ FilePath: save the relative path of the image

* @ Return mixed returns the information array of the generated image.

*/

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, $ this-> Width, $ this-> Height, $ this-> _ PicInfo [0], $ this-> _ PicInfo [1]);

Switch ($ this-> _ PicInfo [2]) {

Case 1: imagegif ($ this-> _ Img, $ SavePath); break;

Case 2: imagejpeg ($ this-> _ Img, $ SavePath); break;

Case 3: imagepng ($ this-> _ Img, $ SavePath); break;

}

$ FIleInfo ['filename'] = $ FileName;

$ FIleInfo ['filepath'] = $ FilePath;

Return $ FIleInfo;

}

}

Example

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$ Img = new Img ();

$ Options ['width'] = 300;

$ Options ['body'] = 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 ("failed to load image,". $ Img-> LoadErr );

}

The above is all the content of this article. I hope you will like it.

Ghost has simply written a PHP image processing class library. although it has few functions, it is not very advanced at present. you can fill it out later, or which one...

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.