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 相关文章推荐
PHP5.0对象模型探索之抽象方法和抽象类
Sep 05 PHP
提取HTML标签
Oct 09 PHP
phpMyAdmin 安装配置方法和问题解决
Jun 08 PHP
php session和cookie使用说明
Apr 07 PHP
php文档更新介绍
Jul 22 PHP
php文件怎么打开 如何执行php文件
Dec 21 PHP
如何使用FireFox插件FirePHP调试PHP
Jul 23 PHP
PHP中使用json数据格式定义字面量对象的方法
Aug 20 PHP
PDO防注入原理分析以及使用PDO的注意事项总结
Oct 23 PHP
Laravel 4.2 中队列服务(queue)使用感受
Oct 30 PHP
Zend Framework数据库操作方法实例总结
Dec 11 PHP
php 数据结构之链表队列
Oct 17 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
PHP5各个版本的新功能和新特性总结
2014/03/16 PHP
PHP多个文件上传到服务器实例
2014/10/29 PHP
PHP实现上传文件并存进数据库的方法
2015/07/16 PHP
PHP实现的简单缓存类
2015/07/29 PHP
PHP多维数组元素操作类的方法
2016/11/14 PHP
js以对象为索引的关联数组
2010/07/04 Javascript
非阻塞动态加载javascript广告实现代码
2010/11/17 Javascript
用JQuery实现表格隔行变色和突出显示当前行的代码
2012/02/10 Javascript
jquery解决图片路径不存在执行替换路径
2013/02/06 Javascript
JS(JQuery)操作Array的相关方法介绍
2014/02/11 Javascript
JS实现网页背景颜色与select框中颜色同时变化的方法
2015/02/27 Javascript
jquery实现实时改变网页字体大小、字体背景色和颜色的方法
2015/08/05 Javascript
jQuery实现的手机发送验证码倒计时效果代码分享
2015/08/24 Javascript
javascript中SetInterval与setTimeout的定时器用法
2015/08/24 Javascript
使用CDN和AJAX加速WordPress中jQuery的加载
2015/12/05 Javascript
js实现搜索框关键字智能匹配代码
2020/03/26 Javascript
javascript jquery对form元素的常见操作详解
2016/06/12 Javascript
图片懒加载插件实例分享(含解析)
2017/01/09 Javascript
js中DOM三级列表(代码分享)
2017/03/20 Javascript
webpack配置proxyTable时pathRewrite无效的解决方法
2018/12/13 Javascript
浅析Python中将单词首字母大写的capitalize()方法
2015/05/18 Python
通过数据库向Django模型添加字段的示例
2015/07/21 Python
Python heapq使用详解及实例代码
2017/01/25 Python
python 统计一个列表当中的每一个元素出现了多少次的方法
2018/11/14 Python
Python输出\u编码将其转换成中文的实例
2018/12/15 Python
pytorch对梯度进行可视化进行梯度检查教程
2020/02/04 Python
python实现从尾到头打印单链表操作示例
2020/02/22 Python
在Tensorflow中实现leakyRelu操作详解(高效)
2020/06/30 Python
CSS3制作轮播图的一种方法
2019/11/11 HTML / CSS
澳大利亚购买太阳镜和眼镜网站:Glamoureyes
2020/09/22 全球购物
毕业自我评价范文
2013/11/17 职场文书
设备管理实施方案
2014/05/31 职场文书
2014离婚协议书范文
2014/09/10 职场文书
民政工作个人总结
2015/02/28 职场文书
优质服务标语口号
2015/12/26 职场文书
前端监听websocket消息并实时弹出(实例代码)
2021/11/27 Javascript