之前在WEB开发笔记写过一个PHP生成缩略图的函数,虽然那个函数能够生成缩略图,但是有一定的缺陷,在生成PNG缩略图的时候,背景是黑色,今天又写了一个函数来弥补一下。代码很简单,就是imagealphablending($thumb,false);与imagesavealpha($thumb,true);很重要.主要就是把PNG的alpha值保存,不要丢失而已。

函数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<span style="color: #000000;font-weight: bold"><?PHP</span>
<span style="color: #666666;font-style: italic">/*
 *$sourePic:原图路径
 * $smallFileName:小图名称
 * $width:小图宽
 * $heigh:小图高
 * 转载注明 www.chhua.com*/</span>
<span style="color: #000000;font-weight: bold">function</span> pngthumb<span style="color: #009900">(</span><span style="color: #000088">$sourePic</span><span style="color: #339933">,</span><span style="color: #000088">$smallFileName</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: #009900">)</span><span style="color: #009900">{</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>
		 	<a rel="noopener noreferrer nofollow" href="http://www.php.net/imagesavealpha" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagesavealpha</span></a><span style="color: #009900">(</span><span style="color: #000088">$image</span><span style="color: #339933">,</span><span style="color: #009900;font-weight: bold">true</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//这里很重要 意思是不要丢了$sourePic图像的透明色;</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/imagesy" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagesy</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">$thumb</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>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/imagealphablending" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagealphablending</span></a><span style="color: #009900">(</span><span style="color: #000088">$thumb</span><span style="color: #339933">,</span><span style="color: #009900;font-weight: bold">false</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/imagesavealpha" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagesavealpha</span></a><span style="color: #009900">(</span><span style="color: #000088">$thumb</span><span style="color: #339933">,</span><span style="color: #009900;font-weight: bold">true</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//这里很重要,意思是不要丢了$thumb图像的透明色;</span>
			<span style="color: #b1b100">if</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/imagecopyresampled" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagecopyresampled</span></a><span style="color: #009900">(</span><span style="color: #000088">$thumb</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: #009900">)</span><span style="color: #009900">{</span>
			<a rel="noopener noreferrer nofollow" href="http://www.php.net/imagepng" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">imagepng</span></a><span style="color: #009900">(</span><span style="color: #000088">$thumb</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: #009900">}</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">//返回小图路径 转载注明 www.chhua.com</span>
<span style="color: #009900">}</span>
 
pngthumb<span style="color: #009900">(</span><span style="color: #0000ff">"a.png"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"c.png"</span><span style="color: #339933">,</span> <span style="color: #cc66cc">300</span><span style="color: #339933">,</span> <span style="color: #cc66cc">300</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//调用</span>
<span style="color: #000000;font-weight: bold">?></span>