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

南国老符 南国老符 最后更新于:2022-04-23 浏览:2025

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

$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;
	}
}

点赞 (0)

收藏 (0)
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/296.html

本站部分信息来源网络,如有侵权,请联系QQ:1062129401删除。

上一篇: webpack4压缩js和打包html

下一篇: PHP把图片转换成圆形png(头像处理)

请先登录后,再发表评论 ~ ~
发表评论
评论 (0)

发表帖子
广告位招租( ¥5 / 天 )
点击咨询
最新帖子

南国老符
2025-05-26 16:54
南国老符
2024-09-29 10:29
南国老符
2023-12-15 21:41
广告位招租( ¥5 / 天 )
点击咨询
最近评论

U
user1379
回复:版本号多少?
U
user1379
回复:新版本修复了,下载新版本即可
U
user1379
U
user1304
回复:默认没有这个功能,你可以自行二次开发后期可能会考虑在站群版本上加入此功能及商城功能
Y
yuanis90
回复:后台->语言->全局管理->模型管理->编辑->启用关键词内链即可
Y
yuanis90
回复:后台已经启用了,但是前台产品说明的关分键词还是没有自动链接到,
购买VIP
购买授权
余额充值
发表帖子
客服微信