Home  >  Article  >  Backend Development  >  PHP实现生成透明背景的PNG缩略图函数分享_PHP

PHP实现生成透明背景的PNG缩略图函数分享_PHP

WBOY
WBOYOriginal
2016-06-01 11:52:09740browse

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

函数如下:

<&#63;PHP
/*
 *$sourePic:原图路径
 * $smallFileName:小图名称
 * $width:小图宽
 * $heigh:小图高
 * 转载注明 www.chhua.com*/
function pngthumb($sourePic,$smallFileName,$width,$heigh){
	$image=imagecreatefrompng($sourePic);//PNG
		 	imagesavealpha($image,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
		 	$BigWidth=imagesx($image);//大图宽度
			$BigHeigh=imagesy($image);//大图高度
			$thumb = imagecreatetruecolor($width,$heigh);
			imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
			imagesavealpha($thumb,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
			if(imagecopyresampled($thumb,$image,0,0,0,0,$width,$heigh,$BigWidth,$BigHeigh)){
			imagepng($thumb,$smallFileName);}
			return $smallFileName;//返回小图路径 转载注明 www.chhua.com
}
 
pngthumb("a.png", "c.png", 300, 300);//调用
&#63;>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn