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来处理多个提交任务
May 08 PHP
openPNE常用方法分享
Nov 29 PHP
PHP设计模式 注册表模式
Feb 05 PHP
详解 PHP加密解密字符串函数附源码下载
Dec 18 PHP
解决ThinkPHP下使用上传插件Uploadify浏览器firefox报302错误的方法
Dec 18 PHP
PHP正则获取页面所有图片地址
Mar 23 PHP
Yii2使用swiftmailer发送邮件的方法
May 03 PHP
PHP使用自定义方法实现数组合并示例
Jul 07 PHP
PHP调试及性能分析工具Xdebug详解
Feb 09 PHP
php实现页面纯静态的实例代码
Jun 21 PHP
用PHP的反射实现委托模式的讲解
Mar 22 PHP
Laravel框架实现文件上传的方法分析
Sep 29 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 5.0对象模型深度探索之类的静态成员
2008/03/27 PHP
五款常用mysql slow log分析工具的比较分析
2011/05/22 PHP
thinkphp的CURD和查询方式介绍
2013/12/19 PHP
学习php设计模式 php实现工厂模式(factory)
2015/12/07 PHP
浅谈php中变量的数据类型判断函数
2017/03/04 PHP
Laravel 集成 Geetest验证码的方法
2018/05/14 PHP
php 根据URL下载远程图片、压缩包、pdf等文件到本地
2019/07/26 PHP
jQuery 前的按键判断代码
2010/03/19 Javascript
js 多种变量定义(对象直接量,数组直接量和函数直接量)
2010/05/24 Javascript
jquery获取特定name所有选中的checkbox,支持IE9标准模式
2013/03/18 Javascript
javascript中的取反再取反~~没有意义
2014/04/06 Javascript
nodejs开发微博实例
2015/03/25 NodeJs
AngularJs中route的使用方法和配置
2016/02/04 Javascript
js数组操作方法总结(必看篇)
2016/11/22 Javascript
Bootstrap基本样式学习笔记之表单(3)
2016/12/07 Javascript
详解JavaScript 中getElementsByName在IE中的注意事项
2017/02/21 Javascript
深入浅析Vue全局组件与局部组件的区别
2018/06/15 Javascript
JS多个表单数据提交下的serialize()应用实例分析
2019/08/27 Javascript
React+Redux实现简单的待办事项列表ToDoList
2019/09/29 Javascript
Vue执行方法,方法获取data值,设置data值,方法传值操作
2020/08/05 Javascript
[02:37]2015国际邀请赛选手档案—LGD.Xiao8
2015/07/28 DOTA
Python中列表、字典、元组、集合数据结构整理
2014/11/20 Python
详解Python 数据库的Connection、Cursor两大对象
2018/06/25 Python
判断python字典中key是否存在的两种方法
2018/08/10 Python
Python3+Appium实现多台移动设备操作的方法
2019/07/05 Python
Python jieba库用法及实例解析
2019/11/04 Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
2020/01/20 Python
国际领先的学术出版商:Springer
2017/01/11 全球购物
Moda Italia荷兰:意大利男士服装
2019/08/31 全球购物
随机分配座位,共50个学生,使学号相邻的同学座位不能相邻
2014/01/18 面试题
党员组织生活会发言材料
2014/10/17 职场文书
幼儿园教师求职信
2015/03/20 职场文书
大学生入党自传2015
2015/06/26 职场文书
MySql子查询IN的执行和优化的实现
2021/08/02 MySQL
Vue.js中v-for指令的用法介绍
2022/03/13 Vue.js
springboot+rabbitmq实现智能家居实例详解
2022/07/23 Java/Android