PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
php  /  管理员 发布于 7年前   124
//初始化内部变量
function __construct($oldfile,$newfile){
list($width,$height)=getimagesize($oldfile);
$this->o_img_file=$oldfile;
$this->o_img_width=$width;
$this->o_img_height=$height;
$this->n_img_file=$newfile;
}
//等比例缩放并且解决GIF透明色为黑色背景的问题
function get_resize_scaling_img(){
$this->n_img_width=$this->o_img_width*$this->o_to_n_per;
$this->n_img_height=$this->o_img_height*$this->o_to_n_per;
//等比例缩放图片(算法)
if ( $this->n_img_width && ( $this->o_img_width <$this->o_img_height))
{
$this->n_img_width = ( $this->n_img_height/$this->o_img_height) * $this->o_img_width;
}
else
{
$this->n_img_height = ($this->n_img_width / $this->o_img_width) * $this->o_img_height;
}
$this->o_img_source=imagecreatefromgif($this->o_img_file);
//创建一个等比例缩放大小的画布
$this->n_img_source=imagecreatetruecolor($this->o_img_width,$this->n_img_height);
//美化:去除黑色不透明背景
$trans_init=imagecolortransparent($this->o_img_source);
//寻找透明色并且判断是否在总颜色中
if($trans_init>=0 && $trans_init < imagecolorstotal($this->o_img_source)){
//如果在的话则搜索这个颜色的RGB色相
$trans_index=imagecolorsforindex($this->o_img_source,$trans_init);
//找到之后就创建这样一个颜色
$trans_new=imagecolorallocate($this->n_img_source,$trans_index["red"],$trans_index["green"],$trans_index["blue"]);
//然后我们用这个颜色去填充新的图像
imagefill($this->n_img_source,0,0,$trans_new);
//然后我们在把填充色设置为透明
imagecolortransparent($this->n_img_source,$trans_new);
}
//拷贝原图像到新画板上
imagecopyresized($this->n_img_source,$this->o_img_source,0,0,0,0,$this->n_img_width,$this->n_img_height,$this->o_img_width,$this->o_img_height);
return $this->n_img_source;
}
//最终销毁资源
function __destruct(){
imagedestroy($this->o_img_source);
imagedestroy($this->n_img_source);
}
}
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号