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中使用灵巧的体系结构
Oct 09 PHP
PHP MSSQL 存储过程的方法
Dec 24 PHP
PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法
Mar 10 PHP
PHP的cURL库功能简介 抓取网页、POST数据及其他
Apr 07 PHP
PHP+Memcache实现wordpress访问总数统计(非插件)
Jul 04 PHP
PHP基于数组实现的分页函数实例
Aug 20 PHP
yii实现CheckBox复选框在同一行显示的方法
Dec 03 PHP
分享50个提高PHP执行效率的技巧
Dec 26 PHP
Yii rules常用规则示例
Mar 15 PHP
php微信开发接入
Aug 27 PHP
PHP实现加密文本文件并限制特定页面的存取的效果
Oct 21 PHP
ThinkPHP框架实现的MySQL数据库备份功能示例
May 24 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
星际实力自我测试
2020/03/04 星际争霸
php 注册时输入信息验证器的实现详解
2013/07/05 PHP
php站内搜索关键词变亮的实现方法
2014/12/30 PHP
thinkPHP5.0框架自动加载机制分析
2017/03/18 PHP
yii2 resetful 授权验证详解
2017/05/18 PHP
JavaScript arguments 多参传值函数
2010/10/24 Javascript
一些主流JS框架中DOMReady事件的实现小结
2011/02/12 Javascript
Nodejs Post请求报socket hang up错误的解决办法
2014/09/25 NodeJs
jQuery如何获取同一个类标签的所有值(默认无法获取)
2014/09/25 Javascript
用Move.js配合创建CSS3动画的入门指引
2015/07/22 Javascript
JavaScript中数组去除重复的三种方法
2016/04/22 Javascript
JavaScript第一篇之实现按钮全选、功能
2016/08/21 Javascript
基于vue-cli创建的项目的目录结构及说明介绍
2017/11/23 Javascript
如何获取TypeScript的声明文件.d.ts
2018/05/01 Javascript
微信小程序实现的一键复制功能示例
2019/04/24 Javascript
扫微信小程序码实现网站登陆实现解析
2019/08/20 Javascript
Vuex的各个模块封装的实现
2020/06/05 Javascript
VUE动态生成word的实现
2020/07/26 Javascript
python使用paramiko模块实现ssh远程登陆上传文件并执行
2014/01/27 Python
使用Python3 编写简单信用卡管理程序
2016/12/21 Python
python解决汉字编码问题:Unicode Decode Error
2017/01/19 Python
django站点管理详解
2017/12/12 Python
TensorFlow搭建神经网络最佳实践
2018/03/09 Python
通过shell+python实现企业微信预警
2019/03/07 Python
django一对多模型以及如何在前端实现详解
2019/07/24 Python
Python函数中的可变长参数详解
2019/09/12 Python
Django 限制访问频率的思路详解
2019/12/24 Python
深入解析HTML5的IndexedDB索引数据库
2015/09/14 HTML / CSS
DC Shoes官网:美国滑板鞋和服饰品牌
2017/09/03 全球购物
美国浴缸、水槽和水龙头购物网站:Vintage Tub & Bath
2019/11/05 全球购物
职业规划实施方案
2014/06/10 职场文书
2014大学辅导员工作总结
2014/12/02 职场文书
世界水日宣传活动总结
2015/02/09 职场文书
2015年乡镇平安建设工作总结
2015/05/13 职场文书
导游词之河北白洋淀
2020/01/15 职场文书
SpringBoot2零基础到精通之异常处理与web原生组件注入
2022/03/22 Java/Android