PHP的一个完美GIF等比缩放类,附带去除缩放黑背景


Posted in PHP onApril 01, 2014

现在写东西都喜欢封装成类.....大家调用一下就行了..我就不说怎么调用了

<?php
class resize_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透明色为黑色背景的问题
   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);
   }
}

说明:因为先前没想那么多所以声明了很多私有的内部变量以便调用...程序看起来很笨拙啊......

PHP 相关文章推荐
PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
Dec 05 PHP
php中的boolean(布尔)类型详解
Oct 28 PHP
php smarty truncate UTF8乱码问题解决办法
Jun 13 PHP
PHP中feof()函数实例测试
Aug 23 PHP
php打印输出棋盘的实现方法
Dec 23 PHP
PHP判断来访是搜索引擎蜘蛛还是普通用户的代码小结
Sep 14 PHP
PHP反射API示例分享
Oct 08 PHP
php str_getcsv把字符串解析为数组的实现方法
Apr 05 PHP
Laravel学习教程之View模块详解
Sep 18 PHP
PHP聊天室简单实现方法详解
Dec 08 PHP
PHP进阶学习之类的自动加载机制原理分析
Jun 18 PHP
laravel在中间件内生成参数并且传递到控制器中的2种姿势
Oct 15 PHP
PHP把网页保存为word文件的三种方法
Apr 01 #PHP
php时间戳转换的示例
Mar 31 #PHP
php使用curl存储cookie的示例
Mar 31 #PHP
php过滤敏感词的示例
Mar 31 #PHP
php根据年月获取季度的方法
Mar 31 #PHP
PHP调用VC编写的COM组件实例
Mar 29 #PHP
php定义数组和使用示例(php数组的定义方法)
Mar 29 #PHP
You might like
php弹出对话框实现重定向代码
2014/01/23 PHP
PHP安装memcached扩展笔记
2015/05/28 PHP
深入浅析Yii admin的权限控制
2016/08/31 PHP
PHP面向对象程序设计重载(overloading)操作详解
2019/06/13 PHP
laravel 模型查询按照whereIn排序的示例
2019/10/16 PHP
解决表单中第一个非隐藏的元素获得焦点的一个方案
2009/10/26 Javascript
URL地址中的#符号使用说明
2011/02/12 Javascript
用js来定义浏览器中一个左右浮动元素相对于页面主体宽度的位置的函数
2012/01/21 Javascript
Lazy Load 延迟加载图片的jQuery插件中文使用文档
2012/10/18 Javascript
javascript的offset、client、scroll使用方法详解
2012/12/25 Javascript
js showModalDialog参数的使用详解
2014/01/07 Javascript
浅谈js和css内联外联注意事项
2016/06/30 Javascript
利用jQuery的动画函数animate实现豌豆发射效果
2016/08/28 Javascript
Vue.js实现微信过渡动画左右切换效果
2017/06/13 Javascript
详解javascript函数写法大全
2019/03/25 Javascript
[02:53]2018年度DOTA2最佳战队-完美盛典
2018/12/17 DOTA
[44:22]完美世界DOTA2联赛循环赛 FTD vs PXG BO2第一场 11.01
2020/11/02 DOTA
Python中分数的相关使用教程
2015/03/30 Python
以windows service方式运行Python程序的方法
2015/06/03 Python
Python WXPY实现微信监控报警功能的代码
2017/10/20 Python
python的构建工具setup.py的方法使用示例
2017/10/23 Python
python3 图片referer防盗链的实现方法
2018/03/12 Python
Python实现简单http服务器
2018/04/12 Python
python SQLAlchemy的Mapping与Declarative详解
2019/07/04 Python
为什么从Python 3.6开始字典有序并效率更高
2019/07/15 Python
解决pyinstaller打包运行程序时出现缺少plotly库问题
2020/06/02 Python
CSS去掉A标签(链接)虚线框的方法
2014/04/01 HTML / CSS
五年级语文教学反思
2014/01/30 职场文书
校长先进事迹材料
2014/02/01 职场文书
暑假社会实践心得体会
2014/09/02 职场文书
交通事故赔偿起诉书
2015/05/20 职场文书
Java实现多线程聊天室
2021/06/26 Java/Android
JavaScript 中for/of,for/in 的详细介绍
2021/11/17 Javascript
分享几个简单MySQL优化小妙招
2022/03/31 MySQL
sql server偶发出现死锁的解决方法
2022/04/10 SQL Server
vue特效之翻牌动画
2022/04/20 Vue.js