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下载远程文件类(支持断点续传)
Nov 14 PHP
php中使用cookie来保存用户登录信息的实现代码
Mar 08 PHP
php中DOMElement操作xml文档实例演示
Mar 26 PHP
php empty()与isset()区别的详细介绍
Jun 17 PHP
解析php5配置使用pdo
Jul 03 PHP
PHP针对常规模板引擎中与CSS/JSON冲突的解决方法
Aug 19 PHP
ThinkPHP登录功能的实现方法
Aug 20 PHP
destoon实现VIP排名一直在前面排序的方法
Aug 21 PHP
支付宝支付开发――当面付条码支付和扫码支付实例
Nov 04 PHP
PHP基于接口技术实现简单的多态应用完整实例
Apr 26 PHP
解决在laravel中auth建立时候遇到的问题
Oct 15 PHP
php封装实现钉钉机器人报警接口的示例代码
Aug 08 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
smtp邮件发送一例
2006/10/09 PHP
PHP 图片上传实现代码 带详细注释
2010/04/29 PHP
Windows下的PHP安装文件线程安全和非线程安全的区别
2014/04/23 PHP
PHP中strpos、strstr和stripos、stristr函数分析
2016/06/11 PHP
微信开发之php表单微信中自动提交两次问题解决办法
2017/01/08 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
在网页中控制wmplayer播放器
2006/07/01 Javascript
增强的 JavaScript 的 trim 函数的代码
2007/08/13 Javascript
javascript textarea光标定位方法(兼容IE和FF)
2011/03/12 Javascript
javascript判断两个IP地址是否在同一个网段的实现思路
2013/12/13 Javascript
表格奇偶行设置不同颜色的核心JS代码
2013/12/24 Javascript
Js表格万条数据瞬间加载实现代码
2014/02/20 Javascript
JSON取值前判断
2014/12/23 Javascript
Validform+layer实现漂亮的表单验证特效
2016/01/17 Javascript
JS组件Bootstrap实现弹出框效果代码
2016/04/26 Javascript
微信小程序 两种滑动方式(横向滑动,竖向滑动)详细及实例代码
2017/01/13 Javascript
jQuery实现的弹幕效果完整实例
2017/09/06 jQuery
countup.js实现数字动态叠加效果
2019/10/17 Javascript
[05:35]DOTA2英雄梦之声_第13期_拉比克
2014/06/21 DOTA
浅谈Python中带_的变量或函数命名
2017/12/04 Python
Python实现的读取/更改/写入xml文件操作示例
2018/08/30 Python
Python实现字符串中某个字母的替代功能
2019/10/21 Python
详解Django CAS 解决方案
2019/10/30 Python
浅谈SciPy中的optimize.minimize实现受限优化问题
2020/02/29 Python
使用python自动追踪你的快递(物流推送邮箱)
2020/03/17 Python
公司周年庆活动方案
2014/08/25 职场文书
基层党员对照检查材料
2014/08/25 职场文书
思想纪律作风整顿剖析材料
2014/10/11 职场文书
企业年检委托书范本
2014/10/14 职场文书
2014年教师业务工作总结
2014/12/19 职场文书
2015年初中生自我评价范文
2015/03/03 职场文书
2015年机关后勤工作总结
2015/05/26 职场文书
幼儿园中班班级总结
2015/08/10 职场文书
SQL Server中交叉联接的用法详解
2021/04/22 SQL Server
PostgreSQL怎么创建分区表详解
2022/06/25 PostgreSQL
Python 第三方库 openpyxl 的安装过程
2022/12/24 Python