1
<?php
2
//====================================================
3
// FileName:GDImage.inc.php
4
// Summary: 图片处理程序
5
//====================================================
6
7
class GDImage
8
{
9
var $sourcePath; //图片存储路径
10
var $galleryPath; //图片缩略图and水印图存储路径
11
var $toFile = false; //是否生成文件
12
var $fontName; //使用的英文TTF字体名称
13
var $fontName_GBK;
14
var $maxWidth = 500; //图片最大宽度
15
var $maxHeight = 600; //图片最大高度
16
17
//==========================================
18
// 函数: GDImage($sourcePath ,$galleryPath, $fontPath)
19
// 功能: constructor
20
// 参数: $sourcePath 图片源路径(包括最后一个"/")
21
// 参数: $galleryPath 生成图片的路径
22
// 参数: $fontPath 字体路径
23
//==========================================
24
function GDImage($sourcePath, $galleryPath, $fontPath)
25
{
26
$this->sourcePath = $sourcePath;
27
$this->galleryPath = $galleryPath;
28
$this->fontName = $fontPath . "04B_08__.TTF";
29
$this->fontName_GBK = $fontPath . "汉仪综艺体简.ttf";
30
}
31
32
//==========================================
33
// 函数: makeThumb($sourFile,$width=128,$height=128)
34
// 功能: 生成缩略图(输出到浏览器)
35
// 参数: $sourFile 图片源文件
36
// 参数: $width 生成缩略图的宽度
37
// 参数: $height 生成缩略图的高度
38
// 返回: 0 失败 成功时返回生成的图片路径
39
//==========================================
40
function makeThumb($sourFile,$width=102,$height=78)
41
{
42
$imageInfo = $this->getInfo($sourFile);
43
$sourFile = $this->sourcePath . $sourFile;
44
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_thumb.jpg";
45
switch ($imageInfo["type"])
46
{
47
case 1: //gif
48
$img = imagecreatefromgif($sourFile);
49
break;
50
case 2: //jpg
51
$img = imagecreatefromjpeg($sourFile);
52
break;
53
case 3: //png
54
$img = imagecreatefrompng($sourFile);
55
break;
56
default:
57
return 0;
58
break;
59
}
60
if (!$img)
61
return 0;
62
63
$width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width;
64
$height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height;
65
//$srcW = $imageInfo["width"];
66
//$srcH = $imageInfo["height"];
67
//if ($srcW * $width > $srcH * $height)
68
//$height = round($srcH * $width / $srcW);
69
//else
70
//$width = round($srcW * $height / $srcH);
71
//*
72
if (function_exists("imagecreatetruecolor")) //GD2.0.1
73
{
74
$new = imagecreatetruecolor($width, $height);
75
ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
76
}
77
else
78
{
79
$new = imagecreate($width, $height);
80
ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
81
}
82
//*/
83
if ($this->toFile)
84
{
85
if (file_exists($this->galleryPath . $newName))
86
unlink($this->galleryPath . $newName);
87
ImageJPEG($new, $this->galleryPath . $newName);
88
return $this->galleryPath . $newName;
89
}
90
else
91
{
92
ImageJPEG($new);
93
}
94
ImageDestroy($new);
95
ImageDestroy($img);
96
97
}
98
//==========================================
99
// 函数: waterMark($sourFile, $text)
100
// 功能: 给图片加水印
101
// 参数: $sourFile 图片文件名
102
// 参数: $text 文本数组(包含二个字符串)
103
// 返回: 1 成功 成功时返回生成的图片路径
104
//==========================================
105
function waterMark($sourFile, $text)
106
{
107
$imageInfo = $this->getInfo($sourFile);
108
$sourFile = $this->sourcePath . $sourFile;
109
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_mark.jpg";
110
switch ($imageInfo["type"])
111
{
112
case 1: //gif
113
$img = imagecreatefromgif($sourFile);
114
break;
115
case 2: //jpg
116
$img = imagecreatefromjpeg($sourFile);
117
break;
118
case 3: //png
119
$img = imagecreatefrompng($sourFile);
120
break;
121
default:
122
return 0;
123
break;
124
}
125
if (!$img)
126
return 0;
127
128
$width =$this->maxWidth;
129
$height =$this->maxHeight;
130
$srcW = $imageInfo["width"];
131
$srcH = $imageInfo["height"];
132
if ($srcW * $width > $srcH * $height)
133
$height = round($srcH * $width / $srcW);
134
else
135
$width = round($srcW * $height / $srcH);
136
if (function_exists("imagecreatetruecolor")) //GD2.0.1
137
{
138
$new = imagecreatetruecolor($width, $height);
139
ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
140
}
141
else
142
{
143
$new = imagecreate($width, $height);
144
ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
145
}
146
$white = imageColorAllocate($new, 255, 255, 255);
147
$black = imageColorAllocate($new, 0, 0, 0);
148
$hoariness = imageColorAllocateAlpha($new, 255, 235, 250,70);
149
$alpha = imageColorAllocateAlpha($new, 230, 230, 230, 60);
150
ImageFilledRectangle($new, 0, $height-26, $width, $height, $alpha);
151
ImageFilledRectangle($new, 13, $height-20, 15, $height-7, $black);
152
ImageTTFText($new, 40, 0, 10, $height-100, $hoariness, $this->fontName_GBK, $text[0]);
153
ImageTTFText($new, 4.9, 0, 20, $height-14, $black, $this->fontName, $text[1]);
154
ImageTTFText($new, 4.9, 0, 20, $height-6, $black, $this->fontName, $text[2]);
155
if ($this->toFile)
156
{
157
if (file_exists($this->galleryPath . $newName))
158
@unlink($this->galleryPath . $newName);
159
ImageJPEG($new, $this->galleryPath . $newName);
160
return $this->galleryPath . $newName;
161
}
162
else
163
{
164
ImageJPEG($new);
165
}
166
clearstatcache();
167
ImageDestroy($new);
168
ImageDestroy($img);
169
}
170
//==========================================
171
// 函数: displayThumb($file)
172
// 功能: 显示指定图片的缩略图
173
// 参数: $file 文件名
174
// 返回: 0 图片不存在
175
//==========================================
176
function displayThumb($file)
177
{
178
$thumbName = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg";
179
$file = $this->galleryPath . $thumbName;
180
if (!file_exists($file))
181
return 0;
182
clearstatcache();
183
return $file;
184
}
185
//==========================================
186
// 函数: displayMark($file)
187
// 功能: 显示指定图片的水印图
188
// 参数: $file 文件名
189
// 返回: 0 图片不存在
190
//==========================================
191
function displayMark($file)
192
{
193
$markName = substr($file, 0, strrpos($file, ".")) . "_mark.jpg";
194
$file = $this->galleryPath . $markName;
195
if (!file_exists($file))
196
return 0;
197
return $file;
198
}
199
//==========================================
200
// 函数: getInfo($file)
201
// 功能: 返回图像信息
202
// 参数: $file 文件路径
203
// 返回: 图片信息数组
204
//==========================================
205
function getInfo($file)
206
{
207
$file = $this->sourcePath . $file;
208
$data = getimagesize($file);
209
$imageInfo["width"] = $data[0];
210
$imageInfo["height"]= $data[1];
211
$imageInfo["type"] = $data[2];
212
$imageInfo["name"] = basename($file);
213
return $imageInfo;
214
}
215
}
216
?>
217
<?php2
//==================================================== 3
// FileName:GDImage.inc.php 4
// Summary: 图片处理程序5
//==================================================== 6

7
class GDImage 8
{ 9
var $sourcePath; //图片存储路径 10
var $galleryPath; //图片缩略图and水印图存储路径 11
var $toFile = false; //是否生成文件 12
var $fontName; //使用的英文TTF字体名称 13
var $fontName_GBK;14
var $maxWidth = 500; //图片最大宽度 15
var $maxHeight = 600; //图片最大高度 16

17
//========================================== 18
// 函数: GDImage($sourcePath ,$galleryPath, $fontPath) 19
// 功能: constructor 20
// 参数: $sourcePath 图片源路径(包括最后一个"/") 21
// 参数: $galleryPath 生成图片的路径 22
// 参数: $fontPath 字体路径 23
//========================================== 24
function GDImage($sourcePath, $galleryPath, $fontPath) 25
{ 26
$this->sourcePath = $sourcePath; 27
$this->galleryPath = $galleryPath; 28
$this->fontName = $fontPath . "04B_08__.TTF";29
$this->fontName_GBK = $fontPath . "汉仪综艺体简.ttf";30
} 31

32
//========================================== 33
// 函数: makeThumb($sourFile,$width=128,$height=128) 34
// 功能: 生成缩略图(输出到浏览器) 35
// 参数: $sourFile 图片源文件 36
// 参数: $width 生成缩略图的宽度 37
// 参数: $height 生成缩略图的高度 38
// 返回: 0 失败 成功时返回生成的图片路径 39
//========================================== 40
function makeThumb($sourFile,$width=102,$height=78) 41
{ 42
$imageInfo = $this->getInfo($sourFile); 43
$sourFile = $this->sourcePath . $sourFile; 44
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_thumb.jpg"; 45
switch ($imageInfo["type"]) 46
{ 47
case 1: //gif 48
$img = imagecreatefromgif($sourFile); 49
break; 50
case 2: //jpg 51
$img = imagecreatefromjpeg($sourFile); 52
break; 53
case 3: //png 54
$img = imagecreatefrompng($sourFile); 55
break; 56
default: 57
return 0; 58
break; 59
} 60
if (!$img) 61
return 0; 62

63
$width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width; 64
$height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height; 65
//$srcW = $imageInfo["width"]; 66
//$srcH = $imageInfo["height"]; 67
//if ($srcW * $width > $srcH * $height) 68
//$height = round($srcH * $width / $srcW); 69
//else 70
//$width = round($srcW * $height / $srcH); 71
//* 72
if (function_exists("imagecreatetruecolor")) //GD2.0.1 73
{ 74
$new = imagecreatetruecolor($width, $height); 75
ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 76
} 77
else 78
{ 79
$new = imagecreate($width, $height); 80
ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 81
} 82
//*/ 83
if ($this->toFile) 84
{ 85
if (file_exists($this->galleryPath . $newName)) 86
unlink($this->galleryPath . $newName); 87
ImageJPEG($new, $this->galleryPath . $newName); 88
return $this->galleryPath . $newName; 89
} 90
else 91
{ 92
ImageJPEG($new); 93
} 94
ImageDestroy($new); 95
ImageDestroy($img); 96

97
} 98
//========================================== 99
// 函数: waterMark($sourFile, $text) 100
// 功能: 给图片加水印 101
// 参数: $sourFile 图片文件名 102
// 参数: $text 文本数组(包含二个字符串) 103
// 返回: 1 成功 成功时返回生成的图片路径 104
//========================================== 105
function waterMark($sourFile, $text) 106
{ 107
$imageInfo = $this->getInfo($sourFile); 108
$sourFile = $this->sourcePath . $sourFile; 109
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_mark.jpg"; 110
switch ($imageInfo["type"]) 111
{ 112
case 1: //gif 113
$img = imagecreatefromgif($sourFile); 114
break; 115
case 2: //jpg 116
$img = imagecreatefromjpeg($sourFile); 117
break; 118
case 3: //png 119
$img = imagecreatefrompng($sourFile); 120
break; 121
default: 122
return 0; 123
break; 124
} 125
if (!$img) 126
return 0; 127

128
$width =$this->maxWidth; 129
$height =$this->maxHeight; 130
$srcW = $imageInfo["width"]; 131
$srcH = $imageInfo["height"]; 132
if ($srcW * $width > $srcH * $height) 133
$height = round($srcH * $width / $srcW); 134
else 135
$width = round($srcW * $height / $srcH); 136
if (function_exists("imagecreatetruecolor")) //GD2.0.1 137
{ 138
$new = imagecreatetruecolor($width, $height); 139
ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 140
} 141
else 142
{ 143
$new = imagecreate($width, $height); 144
ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 145
} 146
$white = imageColorAllocate($new, 255, 255, 255); 147
$black = imageColorAllocate($new, 0, 0, 0); 148
$hoariness = imageColorAllocateAlpha($new, 255, 235, 250,70); 149
$alpha = imageColorAllocateAlpha($new, 230, 230, 230, 60); 150
ImageFilledRectangle($new, 0, $height-26, $width, $height, $alpha); 151
ImageFilledRectangle($new, 13, $height-20, 15, $height-7, $black); 152
ImageTTFText($new, 40, 0, 10, $height-100, $hoariness, $this->fontName_GBK, $text[0]); 153
ImageTTFText($new, 4.9, 0, 20, $height-14, $black, $this->fontName, $text[1]); 154
ImageTTFText($new, 4.9, 0, 20, $height-6, $black, $this->fontName, $text[2]); 155
if ($this->toFile) 156
{ 157
if (file_exists($this->galleryPath . $newName)) 158
@unlink($this->galleryPath . $newName); 159
ImageJPEG($new, $this->galleryPath . $newName); 160
return $this->galleryPath . $newName; 161
} 162
else 163
{ 164
ImageJPEG($new); 165
}166
clearstatcache();167
ImageDestroy($new); 168
ImageDestroy($img); 169
} 170
//========================================== 171
// 函数: displayThumb($file) 172
// 功能: 显示指定图片的缩略图 173
// 参数: $file 文件名 174
// 返回: 0 图片不存在 175
//========================================== 176
function displayThumb($file) 177
{ 178
$thumbName = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg"; 179
$file = $this->galleryPath . $thumbName; 180
if (!file_exists($file)) 181
return 0; 182
clearstatcache();183
return $file;184
} 185
//========================================== 186
// 函数: displayMark($file) 187
// 功能: 显示指定图片的水印图 188
// 参数: $file 文件名 189
// 返回: 0 图片不存在 190
//========================================== 191
function displayMark($file) 192
{ 193
$markName = substr($file, 0, strrpos($file, ".")) . "_mark.jpg"; 194
$file = $this->galleryPath . $markName; 195
if (!file_exists($file)) 196
return 0; 197
return $file;198
} 199
//========================================== 200
// 函数: getInfo($file) 201
// 功能: 返回图像信息 202
// 参数: $file 文件路径 203
// 返回: 图片信息数组 204
//========================================== 205
function getInfo($file) 206
{ 207
$file = $this->sourcePath . $file; 208
$data = getimagesize($file); 209
$imageInfo["width"] = $data[0]; 210
$imageInfo["height"]= $data[1]; 211
$imageInfo["type"] = $data[2]; 212
$imageInfo["name"] = basename($file); 213
return $imageInfo; 214
} 215
}216
?>217

浙公网安备 33010602011771号