PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
现在写东西都喜欢封装成类.....大家调用一下就行了..我就不说怎么调用了
classresize_image{
private$o_img_width;//原图像宽度
private$o_img_height;//原图像高度
private$n_img_width;//新图像宽度
private$n_img_height;//新图像高度
private$o_img_file;//原图像文件
private$o_img_source;//原图像资源
private$n_img_file;//新图像资源
private$n_img_source;//新图像资源
private$o_to_n_per=0.5;//图像缩放比
//初始化内部变量
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透明色为黑色背景的问题
functionget_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);
}
}
说明:因为先前没想那么多所以声明了很多私有的内部变量以便调用...程序看起来很笨拙啊......
相关文章
- 笛卡尔积 php,PHP笛卡尔积实现算法示例
- Linux下运行PHP脚本的简单步骤(linux运行php脚本)
- 深入学习:PHP如何配置MySQL(php如何配置mysql)
- 数据库MySQL 数据库的 PHP 扩展研究(php扩展mysql)
- 数据深入PHP:从MySQL获取表数据(php获取mysql表)
- Linux下的PHP编程之旅(linux的php)
- 学习PHP与MySQL:一个完美的开始(php与mysql教程)
- MySQL封装,PHP解决方案(php封装mysql)
- 实战案例:CentOS 8上实现PHP-FPM状态页
- 学习PHP程序员必会的Linux技术(php程序员linux)
- 数据库解决PHP连接远程MySQL数据库的问题(php连接远程mysql)
- 让Linux帮助启动PHP服务(linux启动php)
- MySQL实现PHP数组存储的效果(mysql存储数组php)
- PHP MySQL 开发构建实现功能多样的网站(php mysql 类)
- PHP与MySQL完美结合:数据库操作常用语句及优化技巧(php使用mysql)
- PHP连接MSSQL数据库的连接池优化(php mssql连接池)
- 基于PHP和MSSQL的连接设置(php mssql设置)
- MySQL与PHP实现长久连接的完美结合(mysql php长连接)
- Linux 下学习 PHP 开发入门(linux下开发php)
- Oracle和PHP完美配合提升数据库性能(oracle php配置)
- 用PHP实现Redis缓存一个实例(redis缓存实例php)
- Win+Apache+PHP+MySQL+Tcomcat配置
- php下实现一个阿拉伯数字转中文数字的函数
- php实现从ftp服务器上下载文件树到本地电脑的程序
- php程序效率优化的一些策略小结
- PHP和Mysqlweb应用开发核心技术第1部分Php基础-1开始了解php
- PHP与SQL注入攻击防范小技巧
- 关于php正则匹配汉字的方法介绍
- 分享一个php的异常处理程序
- 在CentOS6中安装WordPress(一)安装Apache,Mysql,PHP环境