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 19 PHP
nginx+php-fpm配置文件的组织结构介绍
Nov 07 PHP
PHP中header和session_start前不能有输出原因分析
Jan 11 PHP
PH P5.2至5.5、5.6的新增功能详解
Jul 14 PHP
PHP防止表单重复提交的几种常用方法汇总
Aug 19 PHP
PHP使用DirectoryIterator显示下拉文件列表的方法
Mar 13 PHP
深入理解PHP 数组之count 函数
Jun 13 PHP
Fleaphp常见函数功能与用法示例
Nov 15 PHP
thinkPHP中配置的读取与C方法详解
Dec 05 PHP
ThinkPHP中create()方法自动验证表单信息
Apr 28 PHP
thinkPHP5框架实现基于ajax的分页功能示例
Jun 12 PHP
PHP使用Redis队列执行定时任务实例讲解
Mar 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
新浪新闻小偷
2006/10/09 PHP
基于PHP生成静态页的实现方法
2013/05/10 PHP
php生成图片缩略图功能示例
2017/02/22 PHP
PHP大文件分片上传的实现方法
2018/10/28 PHP
laravel 数据迁移与 Eloquent ORM的实现方法
2019/04/12 PHP
lib.utf.js
2007/08/21 Javascript
Node.js开发指南中的简单实例(mysql版)
2013/09/17 Javascript
jQuery中parents()方法用法实例
2015/01/07 Javascript
NodeJS使用jQuery选择器操作DOM
2015/02/13 NodeJs
jQuery插件Tmpl的简单使用方法
2015/04/27 Javascript
简介JavaScript中toUpperCase()方法的使用
2015/06/06 Javascript
基于jQuery倾斜打开侧边栏菜单特效代码
2015/09/15 Javascript
整理Javascript基础语法学习笔记
2015/11/29 Javascript
Bootstrap编写一个同时适用于PC、平板、手机的登陆页面
2016/06/30 Javascript
浅谈mvvm-simple双向绑定简单实现
2018/04/18 Javascript
jQuery实现鼠标移到某个对象时弹出显示层功能
2018/08/23 jQuery
Node.js如何优雅的封装一个实用函数的npm包的方法
2019/04/29 Javascript
js如何获取访问IP、地区、当前操作浏览器
2019/07/23 Javascript
JS中的算法与数据结构之字典(Dictionary)实例详解
2019/08/20 Javascript
python操作mysql数据库
2017/03/05 Python
Python eval的常见错误封装及利用原理详解
2019/03/26 Python
python 实现手机自动拨打电话的方法(通话压力测试)
2019/08/08 Python
python requests库的使用
2021/01/06 Python
PyQt5通过信号实现MVC的示例
2021/02/06 Python
CSS3实现图片抽屉式效果的示例代码
2019/11/06 HTML / CSS
HTML5地理定位与第三方工具百度地图的应用
2016/11/17 HTML / CSS
美国最大的半成品净菜电商:Blue Apron(蓝围裙)
2018/04/27 全球购物
销售类个人求职信范文
2013/09/25 职场文书
卫校护理专业毕业生求职信
2013/11/26 职场文书
音乐教育感言
2014/03/05 职场文书
法语专业求职信
2014/07/20 职场文书
房屋鉴定委托书范本
2014/09/23 职场文书
大学生实训报告总结
2014/11/05 职场文书
人与自然观后感
2015/06/16 职场文书
python随机打印成绩排名表
2021/06/23 Python
MySQL数据库查询进阶之多表查询详解
2022/04/08 MySQL