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 HTML代码串 截取实现代码
Jun 29 PHP
自动把纯文本转换成Web页面的php代码
Aug 27 PHP
IP138 IP地址查询小偷实现代码
Feb 15 PHP
php设计模式 State (状态模式)
Jun 26 PHP
PHP新手NOTICE错误常见解决方法
Dec 07 PHP
Youku 视频绝对地址获取的方法详解
Jun 26 PHP
ThinkPHP CURD方法之limit方法详解
Jun 18 PHP
php+mysql查询优化简单实例
Jan 13 PHP
php+mysql数据库查询实例
Jan 21 PHP
php中各种定义变量的方法小结
Oct 18 PHP
ThinkPHP3.2.3框架邮件发送功能图文实例详解
Apr 23 PHP
php实现简单的守护进程创建、开启与关闭操作
Aug 13 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来写记数器(详细介绍)
2006/10/09 PHP
解决控件遮挡问题:关于有窗口元素和无窗口元素
2007/01/28 PHP
PHP中对于浮点型的数据需要用不同的方法解决
2014/03/11 PHP
PHP getID3类的使用方法学习笔记【附getID3源码下载】
2019/10/18 PHP
Javascript 面向对象之重载
2010/05/04 Javascript
js获取当前页面路径示例讲解
2014/01/08 Javascript
通过js为元素添加多项样式,浏览器全兼容写法
2014/08/30 Javascript
node.js中的fs.realpathSync方法使用说明
2014/12/16 Javascript
jquery实现的淡入淡出下拉菜单效果
2015/08/25 Javascript
简单理解vue中实例属性vm.$els
2016/12/01 Javascript
使用vue.js实现联动效果的示例代码
2017/01/10 Javascript
angularjs $http实现form表单提交示例
2017/06/09 Javascript
vue修改vue项目运行端口号的方法
2017/08/04 Javascript
深入理解Vue生命周期、手动挂载及挂载子组件
2017/09/27 Javascript
jquery自定义显示消息数量
2017/12/19 jQuery
jQuery中的$是什么意思及 $. 和 $().的区别
2018/04/20 jQuery
详解vue-cli项目开发/生产环境代理实现跨域请求
2019/07/23 Javascript
微信小程序bindtap事件与冒泡阻止详解
2019/08/08 Javascript
JavaScript实现联动菜单特效
2020/01/07 Javascript
[57:28]2018DOTA2亚洲邀请赛 4.6 淘汰赛 TNC vs Liquid 第一场
2018/04/10 DOTA
python实现simhash算法实例
2014/04/25 Python
Python获取邮件地址的方法
2015/07/10 Python
Python数据结构之翻转链表
2017/02/25 Python
PyQt打开保存对话框的方法和使用详解
2019/02/27 Python
python定时按日期备份MySQL数据并压缩
2019/04/19 Python
对Python中TKinter模块中的Label组件实例详解
2019/06/14 Python
Django使用unittest模块进行单元测试过程解析
2019/08/02 Python
网络工程系信息安全技术专业大学生求职信
2013/10/22 职场文书
产品销售计划书
2014/05/04 职场文书
2014年个人售房协议书
2014/10/30 职场文书
万里长城导游词
2015/01/30 职场文书
公司人力资源管理制度
2015/08/05 职场文书
小学体育跳绳课教学反思
2016/02/16 职场文书
创业不要错过,这4种餐饮新模式
2019/07/18 职场文书
高中语文教材(文学文化常识大全一)
2019/08/13 职场文书
第四次工业革命,打工人与机器人的竞争
2022/04/21 数码科技