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版(5)
Oct 09 PHP
用PHP和ACCESS写聊天室(七)
Oct 09 PHP
Yii中CGridView关联表搜索排序方法实例详解
Dec 03 PHP
10个简化PHP开发的工具
Dec 25 PHP
Thinkphp单字母函数使用指南
May 08 PHP
Yii2中事务的使用实例代码详解
Sep 07 PHP
PHP运行模式汇总
Nov 06 PHP
php指定长度分割字符串str_split函数用法示例
Jan 30 PHP
visual studio code 调试php方法(图文详解)
Sep 15 PHP
thinkPHP5框架导出Excel文件简单操作示例
Aug 03 PHP
php-fpm中max_children的配置
Mar 15 PHP
YII2框架中日志的配置与使用方法实例分析
Mar 18 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获取网站域名和地址的代码
2008/08/17 PHP
php实现MD5加密16位(不要默认的32位)
2013/08/12 PHP
ThinkPHP实现批量删除数据的代码实例
2014/07/02 PHP
PHP中include/require/include_once/require_once使用心得
2016/08/28 PHP
javascript基本语法分析说明
2008/06/15 Javascript
基本jquery的控制tabs打开的数量的代码
2010/10/17 Javascript
jQuery最佳实践完整篇
2011/08/20 Javascript
九种js弹出对话框的方法总结
2013/03/12 Javascript
javascript使用正则控制input输入框允许输入的值方法大全
2014/06/19 Javascript
javascript实现俄罗斯方块游戏的思路和方法
2015/04/27 Javascript
简介AngularJS的HTML DOM支持情况
2015/06/17 Javascript
jQuery实现响应鼠标滚动的动感菜单效果
2015/09/21 Javascript
JavaScript的Backbone.js框架的一些使用建议整理
2016/02/14 Javascript
浅析jquery如何判断滚动条滚到页面底部并执行事件
2016/04/29 Javascript
jQuery继承extend用法详解
2016/10/10 Javascript
Node.js数据库操作之连接MySQL数据库(一)
2017/03/04 Javascript
vue组件从开发到发布的实现步骤
2018/11/11 Javascript
vue实现移动端轻量日期组件不依赖第三方库的方法
2019/04/28 Javascript
ios中视频的最后一桢问题解决
2019/05/14 Javascript
基于canvas实现手写签名(vue)
2020/05/21 Javascript
JavaScript DOM常用操作代码汇总
2020/07/03 Javascript
解决Vue-Router升级导致的Uncaught (in promise)问题
2020/08/07 Javascript
[02:44]2014DOTA2 国际邀请赛中国区预选赛 大神红毯秀
2014/05/25 DOTA
Django中数据库的数据关系:一对一,一对多,多对多
2018/10/21 Python
python实现贪吃蛇游戏
2020/03/21 Python
利用Pycharm + Django搭建一个简单Python Web项目的步骤
2020/10/22 Python
英国花园家具中心:Garden Furniture Centre
2017/08/24 全球购物
定制别致的瑜伽垫:Sugarmat
2019/06/21 全球购物
The North Face官方旗舰店:美国著名户外品牌
2020/09/28 全球购物
如何查询Oracle数据库中已经创建的索引
2013/10/11 面试题
学生自我鉴定模板
2013/12/30 职场文书
自荐信如何制作?
2014/02/21 职场文书
2014年药房工作总结
2014/11/22 职场文书
话题作文之呼唤
2019/12/18 职场文书
MySQL笔记 —SQL运算符
2022/01/18 MySQL
vue-treeselect的基本用法以及解决点击无法出现拉下菜单
2022/04/30 Vue.js