php中生成透明背景png缩略图程序
文章介绍了在php生成一个透明背景的png缩略图程序,在php要中处理这个很简单我们只要用到imagealphablending($thumb,false);与imagesavealpha($thumb,true);就可以了,下面看程序.
生成PNG缩略图的时候,背景是黑色,今天又写了一个函数来弥补一下,代码很简单,就是imagealphablending($thumb,false);与imagesavealpha($thumb,true);很重要.主要就是把PNG的alpha值保存,不要丢失而已,代码如下:
<?php /* *$sourePic:原图路径 * $smallFileName:小图名称 * $width:小图宽 * $heigh:小图高 * 转载注明 www.phprm.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; //返回小图路径 } pngthumb("a.png", "c.png", 300, 300); //调用 ?>
上面我们全部使用的是php自带的函数,没使用任何第三方程序了,有需要的朋友可以看看.
永久链接:http://www.phprm.com/tuxiang/fs4422.html
转载随意!带上文章地址吧。