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格式化工具Beautify PHP小小BUG
Apr 24 PHP
php安全之直接用$获取值而不$_GET 字符转义
Jun 03 PHP
php数字游戏 计算24算法
Jun 10 PHP
深入解析php中的foreach问题
Jun 30 PHP
php读取图片内容并输出到浏览器的实现代码
Aug 08 PHP
PHP时间戳 strtotime()使用方法和技巧
Oct 29 PHP
ThinkPHP2.0读取MSSQL提示Incorrect syntax near the keyword 'AS'的解决方法
Jun 25 PHP
php中in_array函数用法分析
Nov 15 PHP
PHP多线程之内部多线程实例分析
Mar 09 PHP
php备份数据库类分享
Apr 14 PHP
PHP测试成功的邮件发送案例
Oct 26 PHP
PHP+MySQL实现无极限分类栏目的方法
Dec 23 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/06 日漫
PHP 类商品秒杀计时实现代码
2010/05/05 PHP
phpMyAdmin出现无法载入 mcrypt 扩展,请检查PHP配置的解决方法
2012/03/26 PHP
解决php使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
2013/07/03 PHP
国产PHP开发框架myqee新手快速入门教程
2014/07/14 PHP
php上传功能集后缀名判断和随机命名(强力推荐)
2015/09/10 PHP
PHP结合Mysql数据库实现留言板功能
2016/03/04 PHP
php防止表单重复提交实例讲解
2019/02/11 PHP
js+div实现图片滚动效果代码
2014/02/10 Javascript
JS网页在线获取鼠标坐标值的方法
2015/02/28 Javascript
javascript继承的六大模式小结
2015/04/13 Javascript
提升jQuery的性能需要做好七件事
2016/01/11 Javascript
JavaScript开发Chrome浏览器扩展程序UI的教程
2016/05/16 Javascript
使用微信内嵌H5网页解决JS倒计时失效问题
2017/01/13 Javascript
jQuery插件Echarts实现的渐变色柱状图
2017/03/23 jQuery
vue.js实现条件渲染的实例代码
2017/06/22 Javascript
微信小程序分享功能之按钮button 边框隐藏和点击隐藏
2018/06/14 Javascript
通过js给网页加上水印背景实例
2019/06/17 Javascript
JS+HTML5本地存储Localstorage实现注册登录及验证功能示例
2020/02/10 Javascript
浅析我对JS延迟异步脚本的思考
2020/10/12 Javascript
Python THREADING模块中的JOIN()方法深入理解
2015/02/18 Python
Python max内置函数详细介绍
2016/11/17 Python
Python with关键字,上下文管理器,@contextmanager文件操作示例
2019/10/17 Python
Python无头爬虫下载文件的实现
2020/04/02 Python
浅谈anaconda python 版本对应关系
2020/10/07 Python
详解css3使用transform出现字体模糊的解决办法
2020/10/16 HTML / CSS
加大码胸罩、内裤和服装:Just My Size
2019/03/21 全球购物
台湾屈臣氏网路商店:Watsons台湾
2020/12/29 全球购物
夜大毕业生自我鉴定
2013/10/31 职场文书
房屋出租协议书
2014/04/10 职场文书
学校创先争优活动总结
2014/08/28 职场文书
2014年学生会工作总结
2014/11/07 职场文书
2016年领导干部正风肃纪心得体会
2015/10/09 职场文书
古诗文之爱国名句(77句)
2019/09/24 职场文书
导游词之丽江普济寺
2019/10/22 职场文书
Navicat连接MySQL错误描述分析
2021/06/02 MySQL