PHP的一个完美GIF等比缩放类,附带去除缩放黑背景

现在写东西都喜欢封装成类.....大家调用一下就行了..我就不说怎么调用了

代码如下:

<?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);

}

}

说明:因为先前没想那么多所以声明了很多私有的内部变量以便调用...程序看起来很笨拙啊......

(0)

相关推荐

  • php判断GIF图片是否为动画的方法

    本文介绍了PHP判断GIF图片是动画的方法,具体步骤如下: 首先,gif动画是gif89格式的,发现文件开头是gif89.但是很多透明图片也是用的gif89格式, GOOGLE到的:可以检查文件中是否包含:chr(0×21).chr(0xff).chr(0×0b).'NETSCAPE2.0' chr(0×21).chr(0xff) 是gif图片中扩展功能段的标头,'NETSCAPE2.0'是扩展功能执行的程序名 程序代码如下: <?php function check($image){ $con

  • PHP使用GIFEncoder类生成gif动态滚动字幕

    今天在公司,经理让做一个滚动字幕.但是,不许生成gif图片.所以上网找了GIFEncoder这个类库.确实很好用,但是,应用过程中也出现了一些问题,现在写在这里,以供后来人参考,少走弯路. 文字滚动分为两种情况.第一种为水平滚动: 复制代码 代码如下: <?php require_once("GIFEncoder.class.php"); $count=0;   //设置默认计数器 while(true){     $str = $_REQUEST['str'] ? $_REQU

  • php调整gif动画图片尺寸示例代码分享

    类的使用demo: 复制代码 代码如下: <?php require_once "roucheng.php";  $gr = new gifresizer; $gr->temp_dir = "keleyi"; $gr->resize("keleyi.gif","keleyi_resized.gif",500,500); ?> 类的源代码,保存为roucheng.php文件: 复制代码 代码如下: <

  • 完美实现GIF动画缩略图的php代码

    下面通过一个取自CS警匪游戏的GIF动画来说明问题: GIF动画图片:old.gif 为了让问题更加清晰,我们先还原动画各帧: 选择一:用PHP中的Imagick模块: 复制代码 代码如下: <?php $image = new Imagick('old.gif'); $i = 0; foreach ($image as $frame) { $frame->writeImage('old_' . $i++ . '.gif'); } ?> 选择二:用ImageMagick提供的conver

  • PHP支持多种格式图片上传(支持jpg、png、gif)

    此处一次支持上传2个图片,上传后生成原图和质量较差的图,原图用于保存质量高的图片,质量差的图用于网页显示. PHP Code 复制代码 代码如下: <?php include_once("db.php"); include_once("dbinfo.php"); $connector = new nmdb($host, $username, $password); $connector -> select_db($database); $work_gro

  • PHP生成Gif图片验证码

    先看效果图  字体及字体文件的路径需要在类中$FontFilePath及$FontFileName中设置.如: 复制代码 代码如下: private static $FontFilePath = "static/font/"; //相对地本代码文件的位置private static $FontFileName = array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf&quo

  • PHP使用GIFEncoder类生成的GIF动态图片验证码

    相信很多人都想过如何用PHP生成GIF动画来实现动态图片验证码,以下是实现过程. ImageCode函数通过GIFEncoder类实现的GIF动画的PHP源代码,有兴趣的朋友可以研究一下. 效果如图: 复制代码 代码如下: /**   * ImageCode 生成GIF图片验证   * @param $string 字符串   * @param $width 宽度   * @param $height 高度   * */   function ImageCode($string = '', $w

  • PHP基于GD库的缩略图生成代码(支持jpg,gif,png格式)

    还是老规矩,直接上代码 <?php /** * 缩略图生成类,使用示例: */ $newimage=new ImageResize(); $newimage->resize("tu.jpg","tu_lit.jpg",1000,1000); echo $newimage->GetLastError(); class ImageResize{ private $localimage;//原图路径 private $remoteimage;//缩略图保

  • PHP之生成GIF动画的实现方法

    代码如下所示: 复制代码 代码如下: <?class GifMerge {     var $ver            = '1.1';     var $dly            = 50;     var $mod            = 'C_FILE';     var $first            = true;     var $use_loop            = false;     var $transparent        = false;    

  • PHP使用GIFEncoder类处理gif图片实例

    下面贴处理的源代码: 复制代码 代码如下: <?php require_once("gifencoder.php");   //载入编码 文件 $gif = new GIFEncoder();              //实例化gif解码对象 $gif->load("test.gif");                    //载入要解码的gif图像 for($i=0;$i<sizeof($gif->IMGS["frames&

随机推荐