php 缩放png图透明背景变成黑色的解决方法

工作中需要缩放一些png图然后在去Imagecopymerge,可是发现使用了imagecreatetruecolor和imagecopyresampled后发现本来透明的背景图变成了黑色。

转载代码(自http://www.jb51.net/article/56262.htm):

$img = imagecreatetruecolor(200, 200); 
//2.上色 
$color=imagecolorallocate($img,255,255,255); 
//3.设置透明 
imagecolortransparent($img,$color); 
imagefill($img,0,0,$color); 

然后再进行imagecopyresampled和Imagecopymerge就没有问题了

个人项目代码:

class uploadImg {
	var $smallFolder = "../upload/";//缩略图存放路径
	function getInfo($photo) {
		$imageInfo = getimagesize($photo);
		$imgInfo["width"] = $imageInfo[0];
		$imgInfo["height"] = $imageInfo[1];
		$imgInfo["type"] = $imageInfo[2];
		$imgInfo["name"] = basename($photo);
		$name = explode(".",$photo);//将上传前的文件以“.”分开取得文件类型
		$imgCount = count($name);//获得截取的数量
		$imgInfo["extension"] = $name[$imgCount-1];//取得文件的类型
		return $imgInfo;
	}
	function smallImg($photo,$smallFolder,$width=128,$height=128,$limit=false) {
		if($smallFolder!='') $this->smallFolder = $smallFolder;
		$imgInfo = $this->getInfo($photo);
		$newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], ".")).".".$imgInfo["extension"];//新图片名称
		
		if($imgInfo["type"] == 1) {
			$img = imagecreatefromgif($photo);
		} elseif($imgInfo["type"] == 2) {
			$img = imagecreatefromjpeg($photo);
		} elseif($imgInfo["type"] == 3) {
			$img = imagecreatefrompng($photo);
		} else {
			$img = "";
		}
	
		if(empty($img)) return False;
	
		if($limit==true){
			$width = ($width > $imgInfo["width"]) ? $imgInfo["width"] : $width;
			$height = ($height > $imgInfo["height"]) ? $imgInfo["height"] : $height;
			$srcW = $imgInfo["width"];
			$srcH = $imgInfo["height"];
			
			if ($srcW * $width > $srcH * $height) {
				$height = round($srcH * $width / $srcW);
			} else {
				$width = round($srcW * $height / $srcH);
			}
		}
		
		if (function_exists("imagecreatetruecolor")) {
			$newImg = imagecreatetruecolor($width, $height);
			
			/* --- 用以处理缩放png图透明背景变黑色问题 开始 --- */
			if(strtolower($imgInfo["extension"])=='png' || strtolower($imgInfo["extension"])=='gif'){
				$color = imagecolorallocate($newImg,255,255,255);
				imagecolortransparent($newImg,$color);
				imagefill($newImg,0,0,$color);
			}
			/* --- 用以处理缩放png图透明背景变黑色问题 结束 --- */
			
			ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
		} else {
			$newImg = imagecreate($width, $height);
			ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
		}
	
		if ($this->toFile) {
			if (file_exists($this->smallFolder.$newName)) @unlink($this->smallFolder.$newName);
			ImageJPEG($newImg,$this->smallFolder.$newName,100);
			return $newName;
		} else {
			ImageJPEG($newImg);
		}
		
		ImageDestroy($newImg);
		ImageDestroy($img);
		return $newName;
	}
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值