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

知识技术 2022-04-23 09:51 1768
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/296.html

工作中需要缩放一些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)

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

上一篇: 《建站管家》二次开发文档

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

请先登录后,再发表评论 ~ ~
发表评论
发表评论
发表帖子
广告位招租( ¥5 / 天 )
点击咨询
相关帖子

如果要判断文件是否存在,用函数 is_file(),如果要判断目录是否存在,用函数 is_dir()
用宝塔安装TP程序后,网站前台可以访问,后台无法访问,不是程序问题,而是web服务器环境配置问题(PHP配置),出现这种问题,一般是windows主机安装宝塔造成的,linux安装宝塔不会出现此错误
广告位招租( ¥3 / 天 )
点击咨询
广告位招租( ¥2 / 天 )
点击咨询
广告位招租( ¥1 / 天 )
点击咨询
最新帖子

小朋友,你爸爸是谁啊?这很单纯的,就像我们碰到别人会问:你吃过饭了吗?类似;许多人初次相识,经常会问:你是做什么的啊?你做什么,好像决定你收入的高低,你,资产的范围
1、升级框架到ThinkPHP8.0.3【要求PHP8.1+环境,推荐PHP8.1】 2、启用/禁用调试模式等小细节改进
后台点击生成站点地图 一直转圈圈没有反应,
在线客服