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分页函数
Jul 08 PHP
通过文字传递创建的图形按钮
Oct 09 PHP
PHP聊天室技术
Oct 09 PHP
基于php验证码函数的使用示例
May 03 PHP
分享一段php获取linux服务器状态的代码
May 27 PHP
PHP中substr函数字符串截取用法分析
Jan 07 PHP
PHP简单实现无限级分类的方法
May 13 PHP
PHP pear安装配置教程
May 14 PHP
利用PHP抓取百度阅读的方法示例
Dec 18 PHP
深入讲解PHP的对象注入(Object Injection)
Mar 01 PHP
php中Ioc(控制反转)和Di(依赖注入)
May 07 PHP
Thinkphp5结合layer弹窗定制操作结果页面
Jul 07 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
新版mysql+apache+php Linux安装指南
2006/10/09 PHP
Laravel 4 初级教程之视图、命名空间、路由
2014/10/30 PHP
PHP屏蔽过滤指定关键字的方法
2014/11/03 PHP
Discuz!X中SESSION机制实例详解
2015/09/23 PHP
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
2016/05/19 PHP
利用NodeJS的子进程(child_process)调用系统命令的方法分享
2013/06/05 NodeJs
jQuery图片轮播的具体实现
2013/09/11 Javascript
javascript关于运动的各种问题经典总结
2015/04/27 Javascript
Bootstrap组件系列之福利篇几款好用的组件(推荐)
2016/06/23 Javascript
bootstarp modal框居中显示的实现代码
2017/02/18 Javascript
JS数组去重(4种方法)
2017/03/27 Javascript
解决AngualrJS页面刷新导致异常显示问题
2017/04/20 Javascript
解析Vue 2.5的Diff算法
2017/11/28 Javascript
利用angular、react和vue实现相同的面试题组件
2018/02/19 Javascript
jquery的 filter()方法使用教程
2018/03/22 jQuery
详解Vue文档中几个易忽视部分的剖析
2018/03/24 Javascript
JavaScript EventEmitter 背后的秘密 完整版
2018/03/29 Javascript
python中根据字符串调用函数的实现方法
2016/06/12 Python
Python爬虫DNS解析缓存方法实例分析
2017/06/02 Python
Python类中的魔法方法之 __slots__原理解析
2019/08/26 Python
Python字节单位转换实例
2019/12/05 Python
Python中的sys.stdout.write实现打印刷新功能
2020/02/21 Python
python+opencv边缘提取与各函数参数解析
2020/03/09 Python
python 将视频 通过视频帧转换成时间实例
2020/04/23 Python
浅谈django channels 路由误导
2020/05/28 Python
详解如何解决H5开发使用wx.hideMenuItems无效果不生效
2021/01/20 HTML / CSS
英国领先的办公用品供应商:Viking
2016/08/01 全球购物
高品质和独特的产品世界:Creations and Collections
2018/01/07 全球购物
安德玛菲律宾官网:Under Armour菲律宾
2020/07/28 全球购物
军校制空专业毕业生自我鉴定
2013/11/16 职场文书
工厂门卫岗位职责
2013/11/25 职场文书
《老王》教学反思
2014/02/23 职场文书
优秀电子工程系毕业生求职信
2014/05/24 职场文书
民主生活会意见
2015/06/05 职场文书
小学教师见习总结
2015/06/23 职场文书
Golang原生rpc(rpc服务端源码解读)
2022/04/07 Golang