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