PHP上传图片并生成缩略图的类

以前我写过一个PHP生成缩略图的函数,今天又写了一个集PHP上传图片,生成缩略图,同时上传并生成缩略图的类,希望能给你带来帮助。
在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
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> img <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$fileType</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">fileType</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"image/png"</span><span style="color: #339933">=></span><span style="color: #0000ff">"png"</span><span style="color: #339933">,</span><span style="color: #0000ff">"image/jpeg"</span><span style="color: #339933">=></span><span style="color: #0000ff">"jpg"</span><span style="color: #339933">,</span>
	<span style="color: #0000ff">"image/x-png"</span><span style="color: #339933">=></span><span style="color: #0000ff">"png"</span><span style="color: #339933">,</span><span style="color: #0000ff">"image/pjpeg"</span><span style="color: #339933">=></span><span style="color: #0000ff">"jpg"</span><span style="color: #339933">,</span><span style="color: #0000ff">"image/gif"</span><span style="color: #339933">=></span><span style="color: #0000ff">"gif"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//文件类型数组</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> upLoad<span style="color: #009900">(</span><span style="color: #000088">$formName</span><span style="color: #339933">,</span><span style="color: #000088">$upLoadDir</span><span style="color: #339933">,</span><span style="color: #000088">$size</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//图片上传方法</span>
		<span style="color: #666666;font-style: italic">//参数说说:表单文件域名字,上传路径,控制大小KB  是否生成小图</span>
 
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/file_exists" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">file_exists</span></a><span style="color: #009900">(</span><span style="color: #000088">$upLoadDir</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"<script>alert('请确认图片上传路径是否存在再上传图片!');history.go(-1);</script>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$_FILES</span><span style="color: #009900">[</span><span style="color: #0000ff">"<span style="color: #006699;font-weight: bold">$formName</span>"</span><span style="color: #009900">]</span><span style="color: #009900">[</span><span style="color: #0000ff">"size"</span><span style="color: #009900">]</span><span style="color: #339933">></span><span style="color: #000088">$size</span><span style="color: #339933">*</span><span style="color: #cc66cc">1000</span><span style="color: #009900">)</span> <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//判断 图片字节大小 </span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"<script>alert('图片大小 不能超过"</span><span style="color: #339933">.</span><span style="color: #000088">$size</span><span style="color: #339933">.</span><span style="color: #0000ff">"KB');history.go(-1);</script>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
 
		<span style="color: #000088">$imgType</span><span style="color: #339933">=</span><span style="color: #000088">$_FILES</span><span style="color: #009900">[</span><span style="color: #0000ff">"<span style="color: #006699;font-weight: bold">$formName</span>"</span><span style="color: #009900">]</span><span style="color: #009900">[</span><span style="color: #0000ff">"type"</span><span style="color: #009900">]</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取文件类型</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array_key_exists" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_key_exists</span></a><span style="color: #009900">(</span><span style="color: #000088">$imgType</span><span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">fileType</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"<script>alert('图片类型不合法');history.go(-1);</script>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
 
		<span style="color: #000088">$filename</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/time" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">time</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
		<span style="color: #000088">$imgDir</span><span style="color: #339933">=</span><span style="color: #000088">$upLoadDir</span><span style="color: #339933">.</span><span style="color: #000088">$filename</span><span style="color: #339933">.</span><span style="color: #0000ff">"."</span><span style="color: #339933">.</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">fileType</span><span style="color: #009900">[</span><span style="color: #0000ff">"<span style="color: #006699;font-weight: bold">$imgType</span>"</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/move_uploaded_file" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">move_uploaded_file</span></a><span style="color: #009900">(</span><span style="color: #000088">$_FILES</span><span style="color: #009900">[</span><span style="color: #0000ff">"<span style="color: #006699;font-weight: bold">$formName</span>"</span><span style="color: #009900">]</span><span style="color: #009900">[</span><span style="color: #0000ff">"tmp_name"</span><span style="color: #009900">]</span><span style="color: #339933">,</span><span style="color: #000088">$imgDir</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #b1b100">return</span> <span style="color: #000088">$imgDir</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span><span style="color: #b1b100">else</span> <span style="color: #009900">{</span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"<script>alert('图片上传失败,请重试!');history.go(-1);</script>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>	
	<span style="color: #009900">}</span>
	<span style="color: #666666;font-style: italic">//转载请注明出处:www.chhua.com</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> smallPic<span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #339933">,</span><span style="color: #000088">$width</span><span style="color: #339933">,</span><span style="color: #000088">$heigh</span><span style="color: #339933">,</span><span style="color: #000088">$smallPicDir</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//生成缩略图</span>
		<span style="color: #666666;font-style: italic">//参数说明 大图地址,生成宽度,高度, 缩略图存放路径</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/file_exists" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">file_exists</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"<script>alert('原始图片不存在,不能生成缩略图!');history.go(-1);</script>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/file_exists" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">file_exists</span></a><span style="color: #009900">(</span><span style="color: #000088">$smallPicDir</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"<script>alert('请确认指定缩略图路径是否存在!');history.go(-1);</script>"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/exit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">exit</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
		<span style="color: #000088">$BigImg</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/getimagesize" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">getimagesize</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取图像信息</span>
		<span style="color: #b1b100">switch</span> <span style="color: #009900">(</span><span style="color: #000088">$BigImg</span><span style="color: #009900">[</span><span style="color: #cc66cc">2</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//判断图像类型</span>
		 <span style="color: #b1b100">case</span> <span style="color: #cc66cc">1</span><span style="color: #339933">:</span>
		 	<span style="color: #000088">$image</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/imagecreatefromgif" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagecreatefromgif</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//从大图创建GIF图像</span>
		 	<span style="color: #b1b100">break</span><span style="color: #339933">;</span>
		 <span style="color: #b1b100">case</span> <span style="color: #cc66cc">2</span><span style="color: #339933">:</span>
		 	<span style="color: #000088">$image</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/imagecreatefromjpeg" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagecreatefromjpeg</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//从大图创建JPG图像</span>
		 	<span style="color: #b1b100">break</span><span style="color: #339933">;</span>
		 <span style="color: #b1b100">case</span> <span style="color: #cc66cc">3</span><span style="color: #339933">:</span>
		 	<span style="color: #000088">$image</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/imagecreatefrompng" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagecreatefrompng</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//从大图创建PNG图像</span>
		 	<span style="color: #b1b100">break</span><span style="color: #339933">;</span>		
		<span style="color: #009900">}</span>
		<span style="color: #000088">$BigWidth</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/imagesx" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagesx</span></a><span style="color: #009900">(</span><span style="color: #000088">$image</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取大图的宽度</span>
		<span style="color: #000088">$BigHeigh</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/imagesx" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagesx</span></a><span style="color: #009900">(</span><span style="color: #000088">$image</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取大图的高度</span>
		<span style="color: #000088">$smallPic</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/imagecreatetruecolor" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagecreatetruecolor</span></a><span style="color: #009900">(</span><span style="color: #000088">$width</span><span style="color: #339933">,</span><span style="color: #000088">$heigh</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//创建一个真彩色的小图</span>
		<span style="color: #000088">$smallFileName</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/substr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">substr</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #339933">,</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/strrpos" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strrpos</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #339933">,</span><span style="color: #0000ff">"/"</span><span style="color: #009900">)</span><span style="color: #339933">+</span><span style="color: #cc66cc">1</span><span style="color: #339933">,</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/strlen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strlen</span></a><span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取文件名</span>
		<span style="color: #000088">$smallFileName</span><span style="color: #339933">=</span><span style="color: #000088">$smallPicDir</span><span style="color: #339933">.</span><span style="color: #000088">$smallFileName</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//小图存放的完整路径</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/imagecopyresized" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagecopyresized</span></a><span style="color: #009900">(</span><span style="color: #000088">$smallPic</span><span style="color: #339933">,</span><span style="color: #000088">$image</span><span style="color: #339933">,</span><span style="color: #cc66cc">0</span><span style="color: #339933">,</span><span style="color: #cc66cc">0</span><span style="color: #339933">,</span><span style="color: #cc66cc">0</span><span style="color: #339933">,</span><span style="color: #cc66cc">0</span><span style="color: #339933">,</span><span style="color: #000088">$width</span><span style="color: #339933">,</span><span style="color: #000088">$heigh</span><span style="color: #339933">,</span><span style="color: #000088">$BigWidth</span><span style="color: #339933">,</span><span style="color: #000088">$BigHeigh</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//从大图复制图像到小图</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/imagejpeg" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagejpeg</span></a><span style="color: #009900">(</span><span style="color: #000088">$smallPic</span><span style="color: #339933">,</span><span style="color: #000088">$smallFileName</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出小图到指定的小图路径</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$smallFileName</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//返回小图路径</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> upLoadAndToSmall<span style="color: #009900">(</span><span style="color: #000088">$formname</span><span style="color: #339933">,</span><span style="color: #000088">$uploadDir</span><span style="color: #339933">,</span><span style="color: #000088">$size</span><span style="color: #339933">,</span><span style="color: #000088">$SmallWidth</span><span style="color: #339933">,</span><span style="color: #000088">$SmallHeigh</span><span style="color: #339933">,</span><span style="color: #000088">$SmallDir</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #666666;font-style: italic">//上传图片并生成缩略图,参数说明:表单名字,上传路径,限制大小KB,缩略图宽度,缩略图宽度,缩略图保存目录	</span>
		<span style="color: #000088">$fileName</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">upLoad</span><span style="color: #009900">(</span><span style="color: #000088">$formname</span><span style="color: #339933">,</span><span style="color: #000088">$uploadDir</span><span style="color: #339933">,</span><span style="color: #000088">$size</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//上传</span>
		<span style="color: #000088">$pic</span><span style="color: #009900">[</span><span style="color: #0000ff">"BigPIC"</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$fileName</span><span style="color: #339933">;</span>
		<span style="color: #000088">$smallFileName</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">smallPic</span><span style="color: #009900">(</span><span style="color: #000088">$fileName</span><span style="color: #339933">,</span><span style="color: #000088">$SmallWidth</span><span style="color: #339933">,</span><span style="color: #000088">$SmallHeigh</span><span style="color: #339933">,</span><span style="color: #000088">$SmallDir</span><span style="color: #009900">)</span><span style="color: #339933">;</span>	<span style="color: #666666;font-style: italic">//生成缩略图</span>
		<span style="color: #000088">$pic</span><span style="color: #009900">[</span><span style="color: #0000ff">"SmallPic"</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$smallFileName</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$pic</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//返回大小图的路径数组</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">?></span>