打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PHP函数库範例(1)
是否为电子邮件格式

<?
        
function PF_isEmail($String){
            if (
preg_match("/([a-zA-Z0-9])+@+([a-zA-Z0-9])+.+([a-zA-Z0-9])/",$String)){
                return 
true;
            }else{
                return 
false;
            }
        }
?>


寻找两个函数所有不同的元素

<?
function diffElement($arr1,$arr2){
    
$result = array();
    foreach(
$arr1 as $key => $val ){
        if(!
in_array($val$arr2))
            
array_push($result$val);
    }
    foreach(
$arr2 as $key => $val ){
        if(!
in_array($val$arr1))
            
array_push($result$val);
    }
    return 
$result;
}

?>

函数描述及例子

$a = array(1,3,6,7,8);
$b = array(4,3,6,7,0);
print_r(diffElement($a,$b));

 

php获取指定日期所在周的开始和结束日期

<?
function aweek($gdate ""$first 0){
    if(!
$gdate$gdate date("Y-m-d");
    
$w date("w"strtotime($gdate));
    
$dn $w  $w $first 6;
    
$st date("Y-m-d"strtotime("$gdate -".$dn." days"));
    
$en date("Y-m-d"strtotime("$st +6 days"));
    return array(
$st$en);
}
?>

函数描述及例子

$w = aweek('2011-05-11');

 

PHP获取一年内所有周的开始和结束日期

<?
function get_weeks($year) { 
    
$year_start $year "-01-01"
    
$year_end $year "-12-31"
    
$startday strtotime($year_start); 
    if (
intval(date('N'$startday)) != '1') { 
        
$startday=strtotime("next Monday",strtotime($year_start)); //获取年第一周的日期 
    
}
    
$year_mondy date("Y-m-d"$startday); //获取年第一周的日期 
    
$endday strtotime($year_end); 
    if (
intval(date('W'$endday)) == '7') { 
        
$endday=strtotime("last Sunday",strtotime($year_end)); 
    } 
    
$num intval(date('W'$endday)); 
    for (
$i 1$i <= $num$i++) { 
        
$j $i -1
        
$start_date date("Y-m-d"strtotime("$year_mondy $j week ")); 
        
$end_day date("Y-m-d"strtotime("$start_date +6 day")); 
        
$week_array[$i] = array (str_replace("-""."$start_date), str_replace("-""."$end_day)); 
    } 
    return 
$week_array
}
?>

函数描述及例子

$week = get_weeks(2011);

 

PHP获取FLV文件播放时间函数

<?
function BigEndian2Int($byte_word$signed false){
    
$int_value 0;
    
$byte_wordlen strlen($byte_word);
    for (
$i 0$i $byte_wordlen$i++){
        
$int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen $i));
    }
    if (
$signed){
        
$sign_mask_bit 0x80 << (* ($byte_wordlen 1));
        if (
$int_value $sign_mask_bit){
            
$int_value - ($int_value & ($sign_mask_bit 1));
        }
    }
    return 
$int_value;
}

function 
getTime($name){
    if(!
file_exists($name)){return;}
    
$flv_data_length=filesize($name);
    
$fp = @fopen($name'rb');
    
$flv_header fread($fp5);
    
fseek($fp5SEEK_SET);
    
$frame_size_data_length =BigEndian2Int(fread($fp4));
    
$flv_header_frame_length 9;
    if (
$frame_size_data_length $flv_header_frame_length){
        
fseek($fp$frame_size_data_length $flv_header_frame_lengthSEEK_CUR);
    }
    
$duration 0;
    while ((
ftell($fp) + 1) < $flv_data_length){
        
$this_tag_header fread($fp16);
        
$data_length BigEndian2Int(substr($this_tag_header53));
        
$timestamp BigEndian2Int(substr($this_tag_header83));
        
$next_offset ftell($fp) - $data_length;
        if (
$timestamp $duration) {
            
$duration $timestamp;
        }
        
fseek($fp$next_offsetSEEK_SET);
    }
    
fclose($fp);
    return 
$duration;
}

function 
fn($time){
    
$num $time;
    
$sec intval($num 1000);
    
$h intval($sec 3600);
    
$m intval(($sec 3600) / 60);
    
$s intval(($sec 60 ));
    
$tm $h ':' $m ':' $s;
    return 
$tm;
}

?>

函数描述及例子

echo fn(getTime("VTS_01_5.flv"));

 

PHP图片处理类:缩略,裁剪,圆角,倾斜

<?
/*
    图片处理类:缩略,裁剪,圆角,倾斜
    网站:http://www.13980.com
*/
class resizeimage
{
   
//图片类型
   
var $type;
   
//实际宽度
   
var $width;
   
//实际高度
   
var $height;
   
//改变后的宽度
   
var $resize_width;
   
//改变后的高度
   
var $resize_height;
   
//是否裁图
   
var $cut;
   
//源图象
   
var $srcimg;
   
//目标图象地址
   
var $dstimg;
   
//圆角源
   
var $corner;
   var 
$im;

function 
resizeimage($img$corner$wid$hei,$c$corner_radius$angle)
   {
       
$this->srcimg $img;
       
$this->corner $corner;
       
$this->resize_width $wid;
       
$this->resize_height $hei;
       
$this->cut $c;
       
$this->corner_radius $corner_radius;
       
$this->angle $angle;
       
//图片的类型
       
$this->type substr(strrchr($this->srcimg,"."),1);
       
//初始化图象
       
$this->initi_img();
       
//目标图象地址
       
$this -> dst_img();
       
//--
       
$this->width imagesx($this->im);
       
$this->height imagesy($this->im);
       
//生成图象
       
$this->newimg();
       
ImageDestroy ($this->im);
   }
   function 
newimg()
   {
       
//改变后的图象的比例
       
$resize_ratio = ($this->resize_width)/($this->resize_height);
       
//实际图象的比例
       
$ratio = ($this->width)/($this->height);
       if((
$this->cut)=="1")
       
//裁图
       
{
           if(
$ratio>=$resize_ratio)
           
//高度优先
           
{
               
$newimg imagecreatetruecolor($this->resize_width,$this->resize_height);
               
imagecopyresampled($newimg$this->im0000$this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
               
$tmp $this->rounded_corner($newimg,$this->resize_width);
               
imagepng ($tmp,$this->dstimg);
           }
           if(
$ratio<$resize_ratio)
           
//宽度优先
           
{
               
$newimg imagecreatetruecolor($this->resize_width,$this->resize_height);
               
imagecopyresampled($newimg$this->im0000$this->resize_width$this->resize_height$this->width, (($this->width)/$resize_ratio));
               
$tmp $this->rounded_corner($newimg);
               
imagepng ($tmp,$this->dstimg);
           }
       }
       else
       
//不裁图
       
{
           if(
$ratio>=$resize_ratio)
           {
               
$newimg imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
               
imagecopyresampled($newimg$this->im0000$this->resize_width, ($this->resize_width)/$ratio$this->width$this->height);
               
ImageJpeg ($newimg,$this->dstimg);
           }
           if(
$ratio<$resize_ratio)
           {
               
$newimg imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
               
imagecopyresampled($newimg$this->im0000, ($this->resize_height)*$ratio$this->resize_height$this->width$this->height);
               
ImageJpeg ($newimg,$this->dstimg);
           }
       }
   }
   
//初始化图象
   
function initi_img()
   {
       if(
$this->type=="jpg")
       {
           
$this->im imagecreatefromjpeg($this->srcimg);
       }
       if(
$this->type=="gif")
       {
           
$this->im imagecreatefromgif($this->srcimg);
       }
       if(
$this->type=="png")
       {
           
$this->im imagecreatefrompng($this->srcimg);
       }
   }

   
//处理圆角
   
function rounded_corner($image,$size)
   {
        
$topleft true;
        
$bottomleft true;
        
$bottomright true;
        
$topright true;
        
$corner_source imagecreatefrompng('rounded_corner.png');
        
$corner_width imagesx($corner_source);  
        
$corner_height imagesy($corner_source);  
        
$corner_resized ImageCreateTrueColor($this->corner_radius$this->corner_radius);
        
ImageCopyResampled($corner_resized$corner_source0000$this->corner_radius$this->corner_radius$corner_width$corner_height);
        
$corner_width imagesx($corner_resized);  
        
$corner_height imagesy($corner_resized);  
        
$white ImageColorAllocate($image,255,255,255);
        
$black ImageColorAllocate($image,0,0,0);

        
//顶部左圆角
        
if ($topleft == true) {
            
$dest_x 0;  
            
$dest_y 0;  
            
imagecolortransparent($corner_resized$black); 
            
imagecopymerge($image$corner_resized$dest_x$dest_y00$corner_width$corner_height100);
        } 

        
//下部左圆角
        
if ($bottomleft == true) {
            
$dest_x 0;  
            
$dest_y $size $corner_height
            
$rotated imagerotate($corner_resized900);
            
imagecolortransparent($rotated$black); 
            
imagecopymerge($image$rotated$dest_x$dest_y00$corner_width$corner_height100);  
        }

        
//下部右圆角
        
if ($bottomright == true) {
            
$dest_x $size $corner_width;  
            
$dest_y $size $corner_height;  
            
$rotated imagerotate($corner_resized1800);
            
imagecolortransparent($rotated$black); 
            
imagecopymerge($image$rotated$dest_x$dest_y00$corner_width$corner_height100);  
        }

        
//顶部右圆角
        
if ($topright == true) {
            
$dest_x $size $corner_width;  
            
$dest_y 0;
            
$rotated imagerotate($corner_resized2700);
            
imagecolortransparent($rotated$black); 
            
imagecopymerge($image$rotated$dest_x$dest_y00$corner_width$corner_height100);  
        }
        
$image imagerotate($image$this->angle$white);
        return 
$image
   }

   
//图象目标地址
   
function dst_img()
   {
       
$full_length strlen($this->srcimg);
       
$type_length strlen($this->type);
       
$name_length $full_length-$type_length;
       
$name         substr($this->srcimg,0,$name_length-1);
       
$this->dstimg $name."_small.png";
   }
}

//resizeimage("图片地址", "处理后的宽度", "处理后的高度", "是否裁剪", "圆角度数", "倾斜度");
$img_file 'Sunset.jpg';
$corner 'rounded_corner.png';
$resizeimage = new resizeimage($img_file$corner"80""80""1""6""0");
?>
<img src="Sunset_small.png" border="0">

 

PHP 缓存函数

function array2str($array, $level = 0) {
    if(!is_array($array)) {
        return "'".$array."'";
    }
    if(is_array($array) && function_exists('var_export')) {
        return var_export($array, true);
    }

    $space = '';
    for($i = 0; $i <= $level; $i++) {
        $space .= "\t";
    }
    $evaluate = "Array\n$space(\n";
    $comma = $space;
    if(is_array($array)) {
        foreach($array as $key => $val) {
            $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
            $val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
            if(is_array($val)) {
                $evaluate .= "$comma$key => ".array2str($val, $level + 1);
            } else {
                $evaluate .= "$comma$key => $val";
            }
            $comma = ",\n$space";
        }
    }
    $evaluate .= "\n$space)";
    return $evaluate;
}

//typeid :文章类别
//nums:文章数量
//len:标题截取长度
//contentlen:内容截取长度
//zhiding:置顶级别
//attach:是否取附件
function getnews($typeid=0,$nums=1,$len=30,$contentlen=100,$zhiding=0,$attach=0){
    global $xy_db;
    $len = 100;
    $mkey = md5($typeid.$nums.$len.$contentlen.$zhiding.$attach);
    if(intval($nums)<1) $nums=1;
    $path = SITE_ROOT_PATH.'./temp/cache/'.$mkey.'.php';
    if(is_file($path)){
        @require_once($path);
        if(($expiration+60)>=time()){
            if(is_array($cachenews)){
                $j=0;
                for($i=0;$i<count($cachenews);$i++){
                    if($j<$nums){
                        $data[] = $cachenews[$i];
                        $j++;
                    }
                }
                return $data;
            }
        }
    }
    $time = time();
    if($typeid){
        $cd.=" and type_id='$typeid'";    
    }
    if($zhiding){
        $cd.=" and level='$zhiding' ";
    }
    if($attach){
        $cd.=" and attach_num>0 ";    
    }
    
    if(empty($typeid)){
        $cd.=" and (xy_id='".XY_ID."' or xy_id=0) ";
    }else{
        $cd.=" and xy_id='".XY_ID."' ";
    }
    
    $query=$xy_db->query("select * from xy_news where 1 $cd
     order by orders desc, id desc limit 0,$nums ");
    $cachenews = array();
    $i=0;
    $j=0;
    while($a = $xy_db->fetch_array($query)){
        if($attach){
            $r = $xy_db->fetch_first("select * from xy_news_attach where news_id='$a[id]' and is_image=1 limit 0,1");    
            $a['picurl'] = $r['file_url'];
        }
        $a['addtime'] = date('m-d',$a['addtime']);
        $a['content'] = cutstr(strip_tags($a['content']),$contentlen);
        $a['fullsubject'] = $a['subject'];
        $a['linkurl'] = 'news_detail.html?id='.$a['id'];
        $cachenews[$i] = $a;
        $data1[] = $cachenews[$i];
        if($i<$nums){
            $cachenews[$i]['subject'] = cutstr($cachenews[$i]['subject'],$len);
            $data[] = $cachenews[$i];    
        }
        $i++;
    }
    if($cachenews){
        $cachenews = '<?php ';
        $cachenews .= '
$expiration ''.time().'\';'."\n\n";
        
$cachenews .= "\$cachenews = ".array2str($data1).";\n\n";
        
$cachenews .= ' ?>';
        if(
$fp = @fopen($path'w')) {
            @
flock($fp2);
            
fwrite($fp$cachenews);
            
fclose($fp);
        }
    }
    return 
$data;
}

 

PHP格式化数据,防止注入函数

<?
//格式化数据(防止注入)
function site_addslashes($string$force 0) {
    !
defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC'get_magic_quotes_gpc());
    if(!
MAGIC_QUOTES_GPC || $force) {
        if(
is_array($string)) {
            foreach(
$string as $key => $val) {
                
$string[$key] = daddslashes($val$force);
            }
        } else {
            
$string addslashes($string);
        }
    }
    return 
$string;
}

?>

 

PHP 设置COOKIE,并且加密COOKIE函数


<?
/*
设置COOKIE
eg:site_setcookie('username', 'bibinet');
*/
function site_setCookie($key$value ''$life COOKIE_LIFE_TIME$prefix 0$httponly false) {
    global 
$_SERVER;
    
$value=escape($value);
    
$value=strToHex($value);
    
$timestamp time();
    
$key = ($prefix  COOKIE_PRE '').$key;
    if(
$value == '' || $life 0) {
        
$value '';
        
$life = -1;
    }
    
$life $life  $timestamp $life : ($life  $timestamp 31536000 0);
    
$secure $_SERVER['SERVER_PORT'] == 443  0;
    
//echo "$key, $value, $life, ".COOKIE_LIFE_TIME.", ".COOKIE_PATH.", ".COOKIE_DOMAIN.", $secure, $httponly";
    
return setcookie($key$value$lifeCOOKIE_PATHCOOKIE_DOMAIN$secure$httponly);
}

function 
site_getCookie($key,$type=1) {
    global 
$_SERVER;
    
$value=$_COOKIE[$key];
    if (
$type){
        
$value=hexToStr($value);
        
$value=unescape($value);    
    }else{
        
$value=unescape($value);    
    }
    return 
$value;
}

function 
strToHex($string){
    
$hex="";   
    for(
$i=0;$i<strlen($string);$i++)
    
$hex.=dechex(ord($string[$i]));
    
$hex=strtoupper($hex);
    return 
$hex;
}

function 
hexToStr($hex){   
    
$string="";
    for (
$i=0;$i<strlen($hex)-1;$i+=2)
    
$string.=chr(hexdec($hex[$i].$hex[$i+1]));
    return 
$string;   
}

function 
escape($str){
    
preg_match_all("/[\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+/e",$str,$r); 
    
//匹配utf-8字符, 
    
$str $r[0]; 
    
$l count($str); 
    for(
$i=0$i <$l$i++){
        
$value ord($str[$i][0]);
        if(
$value 223){
            
$str[$i] = rawurlencode(utf8_decode($str[$i])); 
            
//先将utf8编码转换为ISO-8859-1编码的单字节字符,urlencode单字节字符. 
            //utf8_decode()的作用相当于iconv("UTF-8","CP1252",$v)。
        
}else{
            
$str[$i] = "%u".strtoupper(bin2hex(iconv("UTF-8","UCS-2",$str[$i])));
        }
    } 
    return 
join("",$str); 
}

function 
unescape($str){
    
$ret '';
    
$len strlen($str);
    for (
$i 0$i $len$i++){
        if (
$str[$i] == '%' && $str[$i+1] == 'u'){
            
$val hexdec(substr($str$i+24));
            if (
$val 0x7f$ret .= chr($val);
            else if(
$val 0x800$ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
            else 
$ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f)); $i += 5; }
            else if (
$str[$i] == '%') { $ret .= urldecode(substr($str$i3)); $i += 2; }
            else 
$ret .= $str[$i];
        }
        return 
$ret;
}

?>
 
 

PHP缩略图类,可生成BMP格式

<?
class resizeimage 

   
//图片类型 
   
var $type
   
//实际宽度 
   
var $width
   
//实际高度 
   
var $height
   
//改变后的宽度 
   
var $resize_width
   
//改变后的高度 
   
var $resize_height
   
//是否裁图 
   
var $cut
   
//源图象 
   
var $srcimg
   
//目标图象地址 
   
var $dstimg
   
//临时建的图象 
   
var $im
   
//生成的文件名后缀
   
var $extstr;

   function 
resizeimage($img$wid$hei,$extstr,$c=0
   { 
       
$this->srcimg $img
       
$this->resize_width $wid
       
$this->resize_height $hei
       
$this->cut $c
       
$this->extstr $extstr;
       
//图片的类型 
       
$this->type substr(strrchr($this->srcimg,"."),1); 
       
//初始化图象 
       
$this->initi_img(); 
       
//目标图象地址 
       
$this -> dst_img(); 
       
$this->width = @imagesx($this->im); 
       
$this->height = @imagesy($this->im); 
       
//生成图象 
       
$this->newimg(); 
       @
ImageDestroy ($this->im); 
   } 
   function 
newimg() 
   { 
       
//改变后的图象的比例 
       
$resize_ratio = ($this->resize_width)/($this->resize_height); 
       
//实际图象的比例 
       
if($this->height>0)
       
$ratio = ($this->width)/($this->height); 
       if((
$this->cut)=="1"
       
//裁图 
       

           if(
$ratio>=$resize_ratio
           
//高度优先 
           

               
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height); 
               @
imagecopyresampled($newimg$this->im0000$this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); 
               @
ImageJpeg ($newimg,$this->dstimg); 
           } 
           if(
$ratio<$resize_ratio
           
//宽度优先 
           

               
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height); 
               @
imagecopyresampled($newimg$this->im0000$this->resize_width$this->resize_height$this->width, (($this->width)/$resize_ratio)); 
               @
ImageJpeg ($newimg,$this->dstimg); 
           } 
       } 
       else 
       
//不裁图 
       

           if(
$ratio>=$resize_ratio
           { 
               
$newimg = @imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
               @
imagecopyresampled($newimg$this->im0000$this->resize_width, ($this->resize_width)/$ratio$this->width$this->height); 
               
ImageJpeg ($newimg,$this->dstimg); 
           } 
           if(
$ratio<$resize_ratio
           { 
               
$newimg = @imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
               @
imagecopyresampled($newimg$this->im0000, ($this->resize_height)*$ratio$this->resize_height$this->width$this->height); 
               @
ImageJpeg ($newimg,$this->dstimg); 
           } 
       } 
   } 
   
//初始化图象 
   
function initi_img() 
   { 
          
$type=strtolower($this->type);//转换成小写,否则不写扩展名生成不了。
       
       
if($type=="jpg" || $type=="jpeg" || $type=="jpe"
       { 
           
$this->im = @imagecreatefromjpeg($this->srcimg); 
       } 
       if(
$type=="gif"
       { 
           
$this->im = @imagecreatefromgif($this->srcimg); 
       } 
       if(
$type=="png"
       { 
           
$this->im = @imagecreatefrompng($this->srcimg); 
       }
       if(
$type=="bmp")
       {
           
$this->im $this->imagecreatefrombmp($this->srcimg);
       }
   } 
   
   
    function 
imagecreatefrombmp($p_sFile){
        
$file fopen($p_sFile,"rb");
        
$read fread($file,10);
        while(!
feof($file)&&($read<>""))
        
$read .= fread($file,1024);
        
$temp unpack("H*",$read);
        
$hex =  $temp[1];
        
$header substr($hex,0,108);
        if (
substr($header,0,4)=="424d"){
            
$header_parts str_split($header,2);
            
$width hexdec($header_parts[19].$header_parts[18]);
            
$height hexdec($header_parts[23].$header_parts[22]);
            unset(
$header_parts);
        }
        
$x 0;
        
$y 1;
        
$image imagecreatetruecolor($width,$height);
        
$body substr($hex,108);
        
$body_size = (strlen($body)/2);
        
$header_size = ($width*$height);
        
$usePadding = ($body_size>($header_size*3)+4);
        for (
$i=0;$i<$body_size;$i+=3){
            if (
$x>=$width){
                if (
$usePadding)
                
$i    +=    $width%4;
                
$x    =    0;
                
$y++;
                if (
$y>$height)
                break;
            }
            
$i_pos    =    $i*2;
            
$r        =    hexdec($body[$i_pos+4].$body[$i_pos+5]);
            
$g        =    hexdec($body[$i_pos+2].$body[$i_pos+3]);
            
$b        =    hexdec($body[$i_pos].$body[$i_pos+1]);
            
$color    =    imagecolorallocate($image,$r,$g,$b);
            
imagesetpixel($image,$x,$height-$y,$color);
            
$x++;
        }
        unset(
$body);
        return 
$image;
    }
               
   
   
   
//图象目标地址 
   
function dst_img() 
   { 
       
$full_length  strlen($this->srcimg); 
       
$type_length  strlen($this->type); 
       
$name_length  $full_length-$type_length
       
$name         substr($this->srcimg,0,$name_length-1); 
       
$this->dstimg $name.$this->extstr.'.'.$this->type
   }
   
   static function 
get_url($img,$extstr){
       
$imgs explode('.',$img);
       
$ext end($imgs);
       
$full_length  strlen($img); 
       
$type_length  strlen($ext); 
       
$name_length  $full_length-$type_length
       
$name         substr($img,0,$name_length-1); 
       return 
$name.$extstr.'.'.$ext
   }

?>

 

 

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;
    var 
$use_global_in        false;
    var 
$x                0;
    var 
$y                0;
    var 
$ch                0;
    var 
$fin            0;
    var 
$fout            '';
    var 
$loop            0;
    var 
$delay            0;
    var 
$width            0;
    var 
$height            0;
    var 
$trans1             255;
    var 
$trans2             255;
    var 
$trans3             255;
    var 
$disposal            2;
    var 
$out_color_table_size    0;
    var 
$local_color_table_flag    0;
    var 
$global_color_table_size    0;
    var 
$out_color_table_sizecode    0;
    var 
$global_color_table_sizecode0;
    var 
$gif            = array(0x470x490x46);
    var 
$buffer            = array();
    var 
$local_in            = array();
    var 
$global_in            = array();
    var 
$global_out            = array();
    var 
$logical_screen_descriptor    = array();

    function 
GifMerge($images$t1$t2$t3$loop$dl$xpos$ypos$model) {
        if(
$model) {
            
$this->mod $model;
        }
        if(
$loop > -1) {
            
$this->loop floor($loop 1);
            
$this->use_loop true;
        }
        if(
$t1 > -&& $t2 > -&& $t3 > -1) {
            
$this->trans1 $t1;
            
$this->trans2 $t2;
            
$this->trans3 $t3;
            
$this->transparent true;
        }
        for(
$i 0$i count($images); $i++) {
            
$dl[$i]    ? $this->delay $dl[$i] : $this->delay $this->dly;
            
$xpos[$i] ? $this->$xpos[$i] : $this->0;
            
$ypos[$i] ? $this->$ypos[$i] : $this->0;
            
$this->start_gifmerge_process($images[$i]);
        }
        
$this->fout .= "\x3b";
    }

    function 
start_gifmerge_process($fp) {
        if(
$this->mod == 'C_FILE') {
            if(!
$this->fin fopen($fp'rb')) {
                return;
            }
        } elseif(
$this->mod == 'C_MEMORY') {
            
$this->ch 0;
            
$this->fin $fp;
        }
        
$this->getbytes(6);
        if(!
$this->arrcmp($this->buffer$this->gif3)) {
            return;
        }
        
$this->getbytes(7);
        if(
$this->first$this->logical_screen_descriptor $this->buffer;
        
$this->global_color_table_sizecode $this->buffer[4] & 0x07;
        
$this->global_color_table_size << $this->global_color_table_sizecode;
        if(
$this->buffer[4] & 0x80) {
            
$this->getbytes(($this->global_color_table_size));
            for(
$i 0$i < (($this->global_color_table_size)); $i++) {
                
$this->global_in[$i] = $this->buffer[$i];
            }
            if(
$this->out_color_table_size == 0) {
                
$this->out_color_table_size $this->global_color_table_size;
                
$out_color_table_sizecode $this->global_color_table_sizecode;
                
$this->global_out $this->global_in;
            }
            if(
$this->global_color_table_size != $this->out_color_table_size || $this->arrcmp($this->global_out$this->global_in, ($this->global_color_table_size))) {
                
$this->use_global_in true;
            }
        }
        for(
$loop true$loop;) {
            
$this->getbytes(1);
            switch(
$this->buffer[0]) {
                case 
0x21:
                    
$this->read_extension();
                    break;
                case 
0x2c:
                    
$this->read_image_descriptor();
                    break;
                case 
0x3b:
                    
$loop false;
                break;
                default:
                    
$loop false;
            }
        }
        if(
$this->mod == 'C_FILE') {
            
fclose($this->fin);
        }
    }

    function 
read_image_descriptor() {
        
$this->getbytes(9);
        
$head $this->buffer;
            
$this->local_color_table_flag = ($this->buffer[8] & 0x80) ? true false;
            if(
$this->local_color_table_flag) {
            
$sizecode $this->buffer[8] & 0x07;
            
$size << $sizecode;
            
$this->getbytes($size);
            for(
$i 0$i < ($size); $i++) {
                    
$this->local_in[$i] = $this->buffer[$i];
                }
                if(
$this->out_color_table_size == 0) {
                    
$this->out_color_table_size $size;
                    
$out_color_table_sizecode $sizecode;
                for(
$i 0$i < ($size); $i++)
                {
                    
$this->global_out[$i] = $this->local_in[$i];
                }
                }
            }
            if(
$this->first) {
            
$this->first false;
            
$this->fout .= "\x47\x49\x46\x38\x39\x61";
            if(
$this->width && $this->height) {
                
$this->logical_screen_descriptor[0] = $this->width 0xFF;
                
$this->logical_screen_descriptor[1] = ($this->width 0xFF00) >> 8;
                
$this->logical_screen_descriptor[2] = $this->height 0xFF;
                
$this->logical_screen_descriptor[3] = ($this->height 0xFF00) >> 8;
            }
            
$this->logical_screen_descriptor[4] |= 0x80;
            
$this->logical_screen_descriptor[5] &= 0xF0;
            
$this->logical_screen_descriptor[6] |= $this->out_color_table_sizecode;
            
$this->putbytes($this->logical_screen_descriptor7);
                
$this->putbytes($this->global_out, ($this->out_color_table_size 3));
            if(
$this->use_loop) {
                
$ns[0] = 0x21;
                
$ns[1] = 0xFF;
                
$ns[2] = 0x0B;
                
$ns[3] = 0x4e;
                
$ns[4] = 0x45;
                
$ns[5] = 0x54;
                
$ns[6] = 0x53;
                
$ns[7] = 0x43;
                
$ns[8] = 0x41;
                
$ns[9] = 0x50;
                
$ns[10] = 0x45;
                
$ns[11] = 0x32;
                
$ns[12] = 0x2e;
                
$ns[13] = 0x30;
                
$ns[14] = 0x03;
                
$ns[15] = 0x01;
                
$ns[16] = $this->loop 255;
                    
$ns[17] = $this->loop >> 8;
                
$ns[18] = 0x00;
                
$this->putbytes($ns19);
            }
            }
          if(
$this->use_global_in) {
            
$outtable $this->global_in;
            
$outsize $this->global_color_table_size;
            
$outsizecode $this->global_color_table_sizecode;
        } else {
            
$outtable $this->global_out;
            
$outsize $this->out_color_table_size;
        }
        if(
$this->local_color_table_flag) {
            if(
$size == $this->out_color_table_size && !$this->arrcmp($this->local_in$this->global_out$size)) {
                
$outtable $global_out;
                
$outsize $this->out_color_table_size;
            } else {
                
$outtable $this->local_in;
                
$outsize $size;
                
$outsizecode $sizecode;
            }
        }
        
$use_trans false;
        if(
$this->transparent) {
            for(
$i 0$i $outsize$i++) {
                if(
$outtable[$i] == $this->trans1 && $outtable [$i 1] == $this->trans2 && $outtable [$i 2] == $this->trans3) {
                    break;
                }
            }
            if(
$i $outsize) {
                
$transindex $i;
                
$use_trans true;
            }
        }
        if(
$this->delay || $use_trans) {
            
$this->buffer[0] = 0x21;
            
$this->buffer[1] = 0xf9;
            
$this->buffer[2] = 0x04;
            
$this->buffer[3] = ($this->disposal << 2) + ($use_trans  0);
            
$this->buffer[4] = $this->delay 0xff;
            
$this->buffer[5] = ($this->delay 0xff00) >> 8;
            
$this->buffer[6] = $use_trans  $transindex 0;
            
$this->buffer[7] = 0x00;
            
$this->putbytes($this->buffer,8);
        }
        
$this->buffer[0] = 0x2c;
        
$this->putbytes($this->buffer,1);
        
$head[0] = $this->0xff;
        
$head[1] = ($this->0xff00) >> 8;
        
$head[2] = $this->0xff;
        
$head[3] = ($this->0xff00) >> 8;
        
$head[8] &= 0x40;
        if(
$outtable != $this->global_out) {
            
$head[8] |= 0x80;
            
$head[8] |= $outsizecode;
        }
        
$this->putbytes($head,9);
        if(
$outtable != $this->global_out) {
            
$this->putbytes($outtable, ($outsize));
        }
        
$this->getbytes(1);
        
$this->putbytes($this->buffer,1);
        for(;;) {
            
$this->getbytes(1);
            
$this->putbytes($this->buffer,1);
            if((
$u $this->buffer[0]) == 0) {
                break;
            }
            
$this->getbytes($u);
            
$this->putbytes($this->buffer$u);
            }
    }

    function 
read_extension() {
        
$this->getbytes(1);
        switch(
$this->buffer[0]) {
            case 
0xf9:
                    
$this->getbytes(6);
                   break;
            case 
0xfe:
                    for(;;) {
                    
$this->getbytes(1);
                            if((
$u $this->buffer[0]) == 0) {
                            break;
                        }
                    
$this->getbytes($u);
                    }
                break;
            case 
0x01:
                    
$this->getbytes(13);
                    for(;;) {
                    
$this->getbytes(0);
                            if((
$u $this->buffer[0]) == 0) {
                            break;
                        }
                    
$this->getbytes($u);
                    }
                break;
                   case 
0xff:
                    
$this->getbytes(9);
                    
$this->getbytes(3);
                    for(;;) {
                    
$this->getbytes(1);
                            if(!
$this->buffer[0]) {
                                break;
                            }
                            
$this->getbytes($this->buffer[0]);
                        }
                break;
            default:
                    for(;;) {
                    
$this->getbytes(1);
                        if(!
$this->buffer[0]) {
                            break;
                        }
                    
$this->getbytes($this->buffer[0]);
                }
            }
    }

    function 
arrcmp($b$s$l) {
        for(
$i 0$i $l$i++) {
            if(
$s{$i} != $b{$i}) {
                return 
false;
            }
        }
        return 
true;
    }

    function 
getbytes($l) {
        for(
$i 0$i $l$i++) {
            if(
$this->mod == 'C_FILE') {
                    
$bin unpack('C*'fread($this->fin1));
                    
$this->buffer[$i] = $bin[1];
                } elseif(
$this->mod == 'C_MEMORY') {
                        
$bin unpack('C*'substr($this->fin$this->ch1));
                        
$this->buffer[$i] = $bin[1];
                        
$this->ch++;
                }
        }
        return 
$this->buffer;
    }

    function 
putbytes($s$l) {
        for(
$i 0$i $l$i++) {
            
$this->fout .= pack('C*'$s[$i]);
        }
    }

    function 
getAnimation() {
        return 
$this->fout;
    }
}

?>

 

PHP常用测试函数

<?
function bug($str)
    {
        
$fp fopen("./bug_log.txt","a+");
        
flock($fpLOCK_EX) ;
        
fwrite($fp,$str."\n");
        
flock($fpLOCK_UN); 
        
fclose($fp);
    }
?>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
解决canvas转base64/jpeg时透明区域变成黑色背景的方法
extjs应用echarts自适应网页
博客素材--网页经典代码 - 走进我的空间 心灵深处的呼唤 男人情感家园 - gzqhzt...
判断移动设备的横屏和竖屏最佳方案
三种幻灯片切换的特效源代码
golang实现PDF图转片
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服