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 数组使用详解 推荐
Jun 02 PHP
PHP基础知识回顾
Aug 16 PHP
基于PHP对XML的操作详解
Jun 07 PHP
IIS安装Apache伪静态插件的具体操作图文
Jul 01 PHP
php 删除目录下N分钟前创建的所有文件的实现代码
Aug 10 PHP
PHP迭代器的内部执行过程详解
Nov 12 PHP
PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享
Sep 27 PHP
php生成zip文件类实例
Apr 07 PHP
PHP弹出对话框技巧详细解读
Sep 26 PHP
Symfony2安装的方法(2种方法)
Feb 04 PHP
Json_decode 解析json字符串为NULL的解决方法(必看)
Feb 17 PHP
PHP工厂模式简单实现方法示例
May 23 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
php app支付宝回调(异步通知)详解
2018/07/25 PHP
ThinkPHP3.2.3框架邮件发送功能图文实例详解
2019/04/23 PHP
ThinkPhP+Apache+PHPstorm整合框架流程图解
2020/11/23 PHP
js直接编辑当前cookie的脚本
2008/09/14 Javascript
jQuery实现的立体文字渐变效果
2010/05/17 Javascript
jquery.validate使用攻略 第三部
2010/07/01 Javascript
seajs中模块的解析规则详解和模块使用总结
2014/03/12 Javascript
jquery对象和javascript对象即DOM对象相互转换
2014/08/07 Javascript
JavaScript异步加载浅析
2014/12/28 Javascript
微信小程序 UI布局常用技巧整理总结
2016/12/05 Javascript
js获取json中key所对应的value值的简单方法
2020/06/17 Javascript
Angular.js中定时器循环的3种方法总结
2017/04/27 Javascript
JS身份证信息验证正则表达式
2017/06/12 Javascript
ES6解构赋值的功能与用途实例分析
2017/10/31 Javascript
使用vue的transition完成滑动过渡的示例代码
2018/06/25 Javascript
Vue指令之 v-cloak、v-text、v-html实例详解
2019/08/08 Javascript
layui 数据表格复选框实现单选功能的例子
2019/09/19 Javascript
浅谈vue 锚点指令v-anchor的使用
2019/11/13 Javascript
Angular5整合富文本编辑器TinyMCE的方法(汉化+上传)
2020/05/26 Javascript
vue中全局路由守卫中替代this操作(this.$store/this.$vux)
2020/07/24 Javascript
[01:10]3.19DOTA2发布会 三代刀塔人第一代
2014/03/25 DOTA
python实现的简单RPG游戏流程实例
2015/06/28 Python
利用python获取某年中每个月的第一天和最后一天
2016/12/15 Python
人机交互程序 python实现人机对话
2017/11/14 Python
pycharm远程调试openstack代码
2017/11/21 Python
python+opencv识别图片中的圆形
2020/03/25 Python
django 删除数据库表后重新同步的方法
2018/05/27 Python
凯蒂·佩里个人女鞋品牌:Katy Perry Collections
2019/04/04 全球购物
"火柴棍式"程序员面试题
2014/03/16 面试题
九州传奇上机题
2014/07/10 面试题
医学检验专业大学生求职信
2013/11/18 职场文书
建筑专业毕业生推荐信
2013/11/21 职场文书
2014年英语教研组工作总结
2014/12/06 职场文书
Java GUI编程菜单组件实例详解
2022/04/07 Java/Android
《总之就是很可爱》新作短篇动画《总之就是很可爱~制服~》将于2022年夏天播出
2022/04/07 日漫