[导入]php上传自动生成缩略图及水印类
思路很大一部分是原创的,但也有一些是COPY网络的,写得不够规范,还请各位大大不要见笑,同时给小弟些意见。
开始第一步:
创建文件夹,布局:
annex:附件(该目录下存放上传的原图片)
|— smallimg:存放缩略图片
|— mark:存放水印图片
include:存放类文件,字体(本程序代码使用的是:04B_08__.TTF)
|— upfile.php:集成简单上传,生成缩略图及水印的类文件信息
|— 04B_08__.TTF:字体文件
test.php:测试文件
进入第二步:
代码研究,希望各位能好好看看,小弟也是初学者,同时也希望各位能提出宝贵意见,小弟定会虚心领教的(写得不好不要拿鸡蛋砸偶噢)
1
-------------------------------------------------------------------------------
2
upfile.php
3![]()
4
<?php
5
class UPImages {
6
var $annexFolder = "annex";//附件存放点,默认为:annex
7
var $smallFolder = "smallimg";//缩略图存放路径,注:必须是放在 $annexFolder下的子目录,默认为:smallimg
8
var $markFolder = "mark";//水印图片存放处
9
var $upFileType = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip
10
var $upFileMax = 1024;//上传大小限制,单位是“KB”,默认为:1024KB
11
var $fontType;//字体
12
var $maxWidth = 500; //图片最大宽度
13
var $maxHeight = 600; //图片最大高度
14![]()
15
function UPImages($annexFolder,$smallFolder,$includeFolder) {
16
$this->annexFolder = $annexFolder;
17
$this->smallFolder = $smallFolder;
18
$this->fontType = $includeFolder."/04B_08__.TTF";
19
}
20![]()
21
function upLoad($inputName) {
22
$imageName = time();//设定当前时间为图片名称
23
if(@empty($_FILES[$inputName]["name"])) die(error("没有上传图片信息,请确认"));
24
$name = explode(".",$_FILES[$inputName]["name"]);//将上传前的文件以“.”分开取得文件类型
25
$imgCount = count($name);//获得截取的数量
26
$imgType = $name[$imgCount-1];//取得文件的类型
27
if(strpos($this->upFileType,$imgType) === false) die(error("上传文件类型仅支持 ".$this->upFileType." 不支持 ".$imgType));
28
$photo = $imageName.".".$imgType;//写入数据库的文件名
29
$uploadFile = $this->annexFolder."/".$photo;//上传后的文件名称
30
$upFileok = move_uploaded_file($_FILES[$inputName]["tmp_name"],$uploadFile);
31
if($upFileok) {
32
$imgSize = $_FILES[$inputName]["size"];
33
$kSize = round($imgSize/1024);
34
if($kSize > ($this->upFileMax*1024)) {
35
@unlink($uploadFile);
36
die(error("上传文件超过 ".$this->upFileMax."KB"));
37
}
38
} else {
39
die(error("上传图片失败,请确认你的上传文件不超过 $upFileMax KB 或上传时间超时"));
40
}
41
return $photo;
42
}
43![]()
44
function getInfo($photo) {
45
$photo = $this->annexFolder."/".$photo;
46
$imageInfo = getimagesize($photo);
47
$imgInfo["width"] = $imageInfo[0];
48
$imgInfo["height"] = $imageInfo[1];
49
$imgInfo["type"] = $imageInfo[2];
50
$imgInfo["name"] = basename($photo);
51
return $imgInfo;
52
}
53![]()
54
function smallImg($photo,$width=128,$height=128) {
55
$imgInfo = $this->getInfo($photo);
56
$photo = $this->annexFolder."/".$photo;//获得图片源
57
$newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], "."))."_thumb.jpg";//新图片名称
58
if($imgInfo["type"] == 1) {
59
$img = imagecreatefromgif($photo);
60
} elseif($imgInfo["type"] == 2) {
61
$img = imagecreatefromjpeg($photo);
62
} elseif($imgInfo["type"] == 3) {
63
$img = imagecreatefrompng($photo);
64
} else {
65
$img = "";
66
}
67
if(empty($img)) return False;
68![]()
69
$width = ($width > $imgInfo["width"]) ? $imgInfo["width"] : $width;
70
$height = ($height > $imgInfo["height"]) ? $imgInfo["height"] : $height;
71
$srcW = $imgInfo["width"];
72
$srcH = $imgInfo["height"];
73
if ($srcW * $width > $srcH * $height) {
74
$height = round($srcH * $width / $srcW);
75
} else {
76
$width = round($srcW * $height / $srcH);
77
}
78
if (function_exists("imagecreatetruecolor")) {
79
$newImg = imagecreatetruecolor($width, $height);
80
ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
81
} else {
82
$newImg = imagecreate($width, $height);
83
ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
84
}
85![]()
86
if ($this->toFile) {
87
if (file_exists($this->annexFolder."/".$this->smallFolder."/".$newName)) @unlink($this->annexFolder."/".$this->smallFolder."/".$newName);
88
ImageJPEG($newImg,$this->annexFolder."/".$this->smallFolder."/".$newName);
89
return $this->annexFolder."/".$this->smallFolder."/".$newName;
90
} else {
91
ImageJPEG($newImg);
92
}
93
ImageDestroy($newImg);
94
ImageDestroy($img);
95
return $newName;
96
}
97![]()
98
function waterMark($photo,$text) {
99
$imgInfo = $this->getInfo($photo);
100
$photo = $this->annexFolder."/".$photo;
101
$newName = substr($imgInfo["name"], 0, strrpos($imgInfo["name"], ".")) . "_mark.jpg";
102
switch ($imgInfo["type"]) {
103
case 1:
104
$img = imagecreatefromgif($photo);
105
break;
106
case 2:
107
$img = imagecreatefromjpeg($photo);
108
break;
109
case 3:
110
$img = imagecreatefrompng($photo);
111
break;
112
default:
113
return False;
114
}
115
if (empty($img)) return False;
116![]()
117
$width = ($this->maxWidth > $imgInfo["width"]) ? $imgInfo["width"] : $this->maxWidth;
118
$height = ($this->maxHeight > $imgInfo["height"]) ? $imgInfo["height"] : $this->maxHeight;
119
$srcW = $imgInfo["width"];
120
$srcH = $imgInfo["height"];
121
if ($srcW * $width > $srcH * $height) {
122
$height = round($srcH * $width / $srcW);
123
} else {
124
$width = round($srcW * $height / $srcH);
125
}
126![]()
127
if (function_exists("imagecreatetruecolor")) {
128
$newImg = imagecreatetruecolor($width, $height);
129
ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
130
} else {
131
$newImg = imagecreate($width, $height);
132
ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
133
}
134
135
$white = imageColorAllocate($newImg, 255, 255, 255);
136
$black = imageColorAllocate($newImg, 0, 0, 0);
137
$alpha = imageColorAllocateAlpha($newImg, 230, 230, 230, 40);
138
ImageFilledRectangle($newImg, 0, $height-26, $width, $height, $alpha);
139
ImageFilledRectangle($newImg, 13, $height-20, 15, $height-7, $black);
140
ImageTTFText($newImg, 4.9, 0, 20, $height-14, $black, $this->fontType, $text[0]);
141
ImageTTFText($newImg, 4.9, 0, 20, $height-6, $black, $this->fontType, $text[1]);
142![]()
143
if($this->toFile) {
144
if (file_exists($this->annexFolder."/".$this->markFolder."/".$newName)) @unlink($this->annexFolder."/".$this->markFolder."/".$newName);
145
ImageJPEG($newImg,$this->annexFolder."/".$this->markFolder."/".$newName);
146
return $this->annexFolder."/".$this->markFolder."/".$newName;
147
} else {
148
ImageJPEG($newImg);
149
}
150
ImageDestroy($newImg);
151
ImageDestroy($img);
152
return $newName;
153
}
154
}
155
?>
156![]()
157
-------------------------------------------------------------------------
158![]()
159
test.php
160
<?php
161
$annexFolder = "annex";
162
$smallFolder = "smallimg";
163
$markFolder = "mark";
164
$includeFolder = "include";
165![]()
166
require("./".$includeFolder."/upfile.php");
167
$img = new UPImages($annexFolder,$smallFolder,$includeFolder);
168
$text = array("www.qinggan.net","all rights reserved");
169
if(@$_GET["go"]) {
170
$photo = $img->upLoad("upfile");
171
$img->maxWidth = $img->maxHeight = 350;//设置生成水印图像值
172
$img->toFile = true;
173
$newSmallImg = $img->smallImg($photo);
174
$newMark = $img->waterMark($photo,$text);
175
echo "<img src='".$newSmallImg."' border='0'><br><br>";
176
echo "<img src='".$newMark."' border='0'><br><br>";
177
echo "<a href='./test.php'>继续上传</a>";
178
} else {
179
?>
180
<form method="post" action="./test.php?go=go" enctype="multipart/form-data">
181
<input type="file" name="upfile"><br><br>
182
<input type="submit" value="上传">
183
</form>
184
<?php
185
}
186
?>
187![]()
188![]()
-------------------------------------------------------------------------------2
upfile.php3

4
<?php5
class UPImages {6
var $annexFolder = "annex";//附件存放点,默认为:annex7
var $smallFolder = "smallimg";//缩略图存放路径,注:必须是放在 $annexFolder下的子目录,默认为:smallimg8
var $markFolder = "mark";//水印图片存放处9
var $upFileType = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip10
var $upFileMax = 1024;//上传大小限制,单位是“KB”,默认为:1024KB11
var $fontType;//字体12
var $maxWidth = 500; //图片最大宽度 13
var $maxHeight = 600; //图片最大高度 14

15
function UPImages($annexFolder,$smallFolder,$includeFolder) {16
$this->annexFolder = $annexFolder;17
$this->smallFolder = $smallFolder;18
$this->fontType = $includeFolder."/04B_08__.TTF";19
}20

21
function upLoad($inputName) {22
$imageName = time();//设定当前时间为图片名称23
if(@empty($_FILES[$inputName]["name"])) die(error("没有上传图片信息,请确认"));24
$name = explode(".",$_FILES[$inputName]["name"]);//将上传前的文件以“.”分开取得文件类型25
$imgCount = count($name);//获得截取的数量26
$imgType = $name[$imgCount-1];//取得文件的类型27
if(strpos($this->upFileType,$imgType) === false) die(error("上传文件类型仅支持 ".$this->upFileType." 不支持 ".$imgType));28
$photo = $imageName.".".$imgType;//写入数据库的文件名29
$uploadFile = $this->annexFolder."/".$photo;//上传后的文件名称30
$upFileok = move_uploaded_file($_FILES[$inputName]["tmp_name"],$uploadFile);31
if($upFileok) {32
$imgSize = $_FILES[$inputName]["size"];33
$kSize = round($imgSize/1024);34
if($kSize > ($this->upFileMax*1024)) {35
@unlink($uploadFile);36
die(error("上传文件超过 ".$this->upFileMax."KB"));37
}38
} else {39
die(error("上传图片失败,请确认你的上传文件不超过 $upFileMax KB 或上传时间超时"));40
}41
return $photo;42
}43

44
function getInfo($photo) {45
$photo = $this->annexFolder."/".$photo;46
$imageInfo = getimagesize($photo);47
$imgInfo["width"] = $imageInfo[0];48
$imgInfo["height"] = $imageInfo[1];49
$imgInfo["type"] = $imageInfo[2];50
$imgInfo["name"] = basename($photo);51
return $imgInfo;52
}53

54
function smallImg($photo,$width=128,$height=128) {55
$imgInfo = $this->getInfo($photo);56
$photo = $this->annexFolder."/".$photo;//获得图片源57
$newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], "."))."_thumb.jpg";//新图片名称58
if($imgInfo["type"] == 1) {59
$img = imagecreatefromgif($photo);60
} elseif($imgInfo["type"] == 2) {61
$img = imagecreatefromjpeg($photo);62
} elseif($imgInfo["type"] == 3) {63
$img = imagecreatefrompng($photo);64
} else {65
$img = "";66
}67
if(empty($img)) return False;68

69
$width = ($width > $imgInfo["width"]) ? $imgInfo["width"] : $width; 70
$height = ($height > $imgInfo["height"]) ? $imgInfo["height"] : $height; 71
$srcW = $imgInfo["width"]; 72
$srcH = $imgInfo["height"]; 73
if ($srcW * $width > $srcH * $height) {74
$height = round($srcH * $width / $srcW);75
} else {76
$width = round($srcW * $height / $srcH);77
}78
if (function_exists("imagecreatetruecolor")) {79
$newImg = imagecreatetruecolor($width, $height);80
ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);81
} else {82
$newImg = imagecreate($width, $height);83
ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);84
}85

86
if ($this->toFile) {87
if (file_exists($this->annexFolder."/".$this->smallFolder."/".$newName)) @unlink($this->annexFolder."/".$this->smallFolder."/".$newName);88
ImageJPEG($newImg,$this->annexFolder."/".$this->smallFolder."/".$newName);89
return $this->annexFolder."/".$this->smallFolder."/".$newName;90
} else {91
ImageJPEG($newImg);92
}93
ImageDestroy($newImg);94
ImageDestroy($img);95
return $newName;96
}97

98
function waterMark($photo,$text) {99
$imgInfo = $this->getInfo($photo);100
$photo = $this->annexFolder."/".$photo;101
$newName = substr($imgInfo["name"], 0, strrpos($imgInfo["name"], ".")) . "_mark.jpg";102
switch ($imgInfo["type"]) {103
case 1:104
$img = imagecreatefromgif($photo);105
break;106
case 2:107
$img = imagecreatefromjpeg($photo);108
break;109
case 3:110
$img = imagecreatefrompng($photo);111
break;112
default:113
return False;114
}115
if (empty($img)) return False;116

117
$width = ($this->maxWidth > $imgInfo["width"]) ? $imgInfo["width"] : $this->maxWidth; 118
$height = ($this->maxHeight > $imgInfo["height"]) ? $imgInfo["height"] : $this->maxHeight; 119
$srcW = $imgInfo["width"]; 120
$srcH = $imgInfo["height"]; 121
if ($srcW * $width > $srcH * $height) {122
$height = round($srcH * $width / $srcW);123
} else {124
$width = round($srcW * $height / $srcH);125
}126

127
if (function_exists("imagecreatetruecolor")) {128
$newImg = imagecreatetruecolor($width, $height);129
ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);130
} else {131
$newImg = imagecreate($width, $height);132
ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);133
}134
135
$white = imageColorAllocate($newImg, 255, 255, 255);136
$black = imageColorAllocate($newImg, 0, 0, 0);137
$alpha = imageColorAllocateAlpha($newImg, 230, 230, 230, 40);138
ImageFilledRectangle($newImg, 0, $height-26, $width, $height, $alpha);139
ImageFilledRectangle($newImg, 13, $height-20, 15, $height-7, $black);140
ImageTTFText($newImg, 4.9, 0, 20, $height-14, $black, $this->fontType, $text[0]);141
ImageTTFText($newImg, 4.9, 0, 20, $height-6, $black, $this->fontType, $text[1]);142

143
if($this->toFile) {144
if (file_exists($this->annexFolder."/".$this->markFolder."/".$newName)) @unlink($this->annexFolder."/".$this->markFolder."/".$newName);145
ImageJPEG($newImg,$this->annexFolder."/".$this->markFolder."/".$newName);146
return $this->annexFolder."/".$this->markFolder."/".$newName;147
} else {148
ImageJPEG($newImg);149
}150
ImageDestroy($newImg);151
ImageDestroy($img);152
return $newName;153
}154
}155
?>156

157
-------------------------------------------------------------------------158

159
test.php160
<?php161
$annexFolder = "annex";162
$smallFolder = "smallimg";163
$markFolder = "mark";164
$includeFolder = "include";165

166
require("./".$includeFolder."/upfile.php");167
$img = new UPImages($annexFolder,$smallFolder,$includeFolder); 168
$text = array("www.qinggan.net","all rights reserved");169
if(@$_GET["go"]) {170
$photo = $img->upLoad("upfile");171
$img->maxWidth = $img->maxHeight = 350;//设置生成水印图像值172
$img->toFile = true;173
$newSmallImg = $img->smallImg($photo);174
$newMark = $img->waterMark($photo,$text);175
echo "<img src='".$newSmallImg."' border='0'><br><br>";176
echo "<img src='".$newMark."' border='0'><br><br>";177
echo "<a href='./test.php'>继续上传</a>";178
} else {179
?>180
<form method="post" action="./test.php?go=go" enctype="multipart/form-data">181
<input type="file" name="upfile"><br><br>182
<input type="submit" value="上传">183
</form>184
<?php185
}186
?>187

188

文章来源:http://www.phpweblog.net/fuyongjie/archive/2008/04/10/3175.html

浙公网安备 33010602011771号