PHP batch generation of image thumbnails

Source: Internet
Author: User

PHP batch generation of image thumbnails

This example describes how to generate image thumbnails in batches in PHP. Share it with you for your reference. The details are as follows:

?

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

<? Php

// Use PHP to generate batch image thumbnails

Function mkdirs ($ dirname, $ mode = 0777)

// Create a directory (directory, [mode])

{

If (! Is_dir ($ dirname ))

{

Mkdirs ($ dirname, $ mode); // If the directory does not exist, recursively create

Return mkdir ($ dirname, $ mode );

}

Return true;

}

Function savefile ($ filename, $ content = '')

// Save the file (file, [content])

{

If (function_exists (file_put_contents ))

{

File_put_contents ($ filename, $ content );

}

Else

{

$ Fp = fopen ($ filename, "wb ");

Fwrite ($ fp, $ content );

Fclose ($ fp );

}

}

Function getsuffix ($ filename) // get the filename suffix

{

Return end (explode (".", $ filename ));

}

Function checksuffix ($ filename, $ arr) // whether the parameter is of the allowed type (currently, allowed)

{

If (! Is_array ($ arr ))

{

$ Arr = explode (",", str_replace ("", "", $ arr ));

}

Return in_array ($ filename, $ arr )? 1: 0;

}

Class image

{

Var $ src; // Source Address

Var $ newsrc; // path of the New Graph (after localization)

Var $ allowtype = array (". gif", ". jpg", ". png", ". jpeg"); // allowed image types

Var $ regif = 0; // whether to scale down the GIF. If it is 0, it is not processed.

Var $ keep = 0; // whether to retain the source file (1 is reserved, 0 is MD5)

Var $ over = 0; // whether the existing image can be overwritten. If the value is 0, the existing image cannot be overwritten.

Var $ dir; // image source directory

Var $ newdir; // processed directory

Function _ construct ($ olddir = null, $ newdir = null)

{

$ This-> dir = $ olddir? $ Olddir: "./images/temp ";

$ This-> newdir = $ newdir? $ Newdir: "./images/s ";

}

Function reNames ($ src)

{

$ Md5file = substr (md5 ($ src), 10, 10). strrchr ($ src ,".");

// After the MD5 file name (for example, 3293okoe.gif)

$ Md5file = $ this-> w. "_". $ this-> h. "_". $ md5file;

// Processed file name

Return $ this-> newdir. "/". $ md5file;

// Save the source image and MD5 file name to the new directory.

}

Function Mini ($ src, $ w, $ h, $ q = 80)

// Generate a thumbnail Mini (image address, width, height, quality)

{

$ This-> src = $ src;

$ This-> w = $ w;

$ This-> h = $ h;

If (strrchr ($ src, ".") = ". gif" & $ this-> regif = 0)

// Whether to process GIF images

{

Return $ this-> src;

}

If ($ this-> keep = 0) // whether to retain the source file. By default, the source file is not retained.

{

$ Newsrc = $ this-> reNames ($ src); // renamed file address

}

Else // keep original name

{

$ Src = str_replace ("\", "/", $ src );

$ Newsrc = $ this-> newdir. strrchr ($ src ,"/");

}

If (file_exists ($ newsrc) & $ this-> over = 0)

// If it already exists, return the address directly.

{

Return $ newsrc;

}

If (strstr ($ src, "http ://")&&! Strstr ($ src, $ _ SERVER ['HTTP _ host'])

// If it is a network file, save it first

{

$ Src = $ this-> getimg ($ src );

}

$ Arr = getimagesize ($ src); // obtain image attributes

$ Width = $ arr [0];

$ Height = $ arr [1];

$ Type = $ arr [2];

Switch ($ type)

{

Case 1: // 1 = GIF,

$ Im = imagecreatefromgif ($ src );

Break;

Case 2: // 2 = JPG

$ Im = imagecreatefromjpeg ($ src );

Break;

Case 3: // 3 = PNG

$ Im = imagecreatefrompng ($ src );

Break;

Default:

Return 0;

}

// Process the thumbnail

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

$ K1 = round ($ h/$ w, 2 );

$ K2 = round ($ height/$ width, 2 );

If ($ k1 <$ k2)

{

$ Width_a = $ width;

$ Height_a = round ($ width * $ k1 );

$ Sw = 0;

$ Sh = ($ height-$ height_a)/2;

}

Else

{

$ Width_a = $ height/$ k1;

$ Height_a = $ height;

$ Sw = ($ width-$ width_a)/2;

$ Sh = 0;

}

// Generate an image

If (function_exists (imagecopyresampled ))

{

Imagecopyresampled ($ nim, $ im, 0, $ sw, $ sh, $ w, $ h, $ width_a, $ height_a );

}

Else

{

Imagecopyresized ($ nim, $ im, 0, $ sw, $ sh, $ w, $ h, $ width_a, $ height_a );

}

If (! Is_dir ($ this-> newdir ))

{

Mkdir ($ this-> newdir );

}

Switch ($ type) // Save the image

{

Case 1:

$ Rs = imagegif ($ nim, $ newsrc );

Break;

Case 2:

$ Rs = imagejpeg ($ nim, $ newsrc, $ q );

Break;

Case 3:

$ Rs = imagepng ($ nim, $ newsrc );

Break;

Default:

Return 0;

}

Return $ newsrc; // return the processed path

}

Function getimg ($ filename)

{

$ Md5file = $ this-> dir. "/". substr (md5 ($ filename), 10, 10). strrchr ($ filename ,".");

If (file_exists ($ md5file ))

{

Return $ md5file;

}

// Get the file and return the new path

$ Img = file_get_contents ($ filename );

If ($ img)

{

If (! Is_dir ($ this-> dir ))

{

Mkdir ($ this-> dir );

}

Savefile ($ md5file, $ img );

Return $ md5file;

}

}

Function reImg ($ src, $ w, $ h, $ q)

// Convert the thumbnail (the file name and structure remain unchanged)

{

$ This-> keep = 1;

Return $ this-> Mini ($ src, $ w, $ h, $ q );

// Address generated by return

}

}

$ Image = new image ();

Echo $ image-> reImg ("images/zht.jpg", 75, 80 );

Echo "<br/> ";

Echo $ image-> reImg ("images/m89w.jpg", 80 );

Echo "<br/> ";

Echo $ image-> getimg ("./images/s/zht.jpg ");

?>

I hope this article will help you with php programming.

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.