正文

彩印图片处理程序2005-09-14 13:52:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yang/4735.html

分享到:

#!/usr/local/bin/php -q
<?php
Class GIF1{

    /**
    功能:将多个GIF文件合成一个
    参数:
    scr:原文件名列表
    dst:目的文件名
    pos_x,pos_y:合成后的图片位置列表
    delay:图片间隔显示时间
    loop:图片循环次数;0,无限循环
    */
    function ComposeGIF($src = array(),$dst,$pos_x = array(),$pos_y = array(),$delay=100,$loop=0){
        $s = GetFileSize($dst);
        $src_image_w=$s[0];
        $src_image_h=$s[1];
        //$cmdline = "convert -delay $delay -size ".$src_image_w."x".$src_image_h." -page +0+0 $dst";
        for($i=0;$i<count($src);$i++){
            //$cmdline .= " -page +".$pos_x[$i]."+".$pos_y[$i]." ".$src[$i];
            $cmdline .= "composite -geometry +".($pos_x[$i]+0)."+".($pos_y[$i]+0)." '". $src[$i]."' $dst $dst";
            $x=exec($cmdline);
        }
    }

    /**
    功能:将一个字符串写入图片文件
    参数:
    str:字符串
    src:源文件名
    dst:目的文件名
    pos_x,pos_y:要写入图片文件的位置
    color:填充字的颜色
    font:选用的字体
    fontsize:选用的字号
    */
    function Char2GIF($str,$src,$dst,$pos_X,$pos_Y,$color,$font,$fontsize=22){
        $utf_str = $this->gb2utf8($str);
        //echo $pos_X.",".$pos_Y;////////////////////////////////////////////
        //$pos_Y=$pos_Y*1;
        $cmdline = "convert -pointsize $fontsize -encoding unicode -fill \"$color\" -font \"$font\" -draw \"text ".($pos_X+0).",".($pos_Y+0)." '".$utf_str."'\" $src $dst";
        echo $cmdline."\n";
        $x=exec($cmdline);
    }

    /**
    功能:创建一个新文件并返回文件名
    参数:
    paht:文件路径
    pre:文件名前缀
    suf:文件名后缀
    */
    function create_new_file($path,$pre,$suf){
        $tmpfname = tempnam($path,$pre);
        rename($tmpfname, $tmpfname.$suf);
        $tmpfname.=$suf;
        return $tmpfname;
    }

    /**
    GB2312到UTF编码转换函数
    */
    function gb2utf8($gb)
    {
        if(!trim($gb))
        return $gb;
        $filename="./gb2312.txt";
        $tmp=file($filename);
        $codetable=array();
        while(list($key,$value)=each($tmp))
        $codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
        $utf8="";
        while($gb){
            if (ord(substr($gb,0,1))>127){
                $tthis=substr($gb,0,2);
                $gb=substr($gb,2,strlen($gb)-2);
                $utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));
            }
            else{
                $tthis=substr($gb,0,1);
                $gb=substr($gb,1,strlen($gb)-1);
                $utf8.=$this->u2utf8($tthis);
            }
        }
        return $utf8;
    }
    function u2utf8($c)
    {
        $str="";
        if ($c < 0x80)
        {
            $str.=$c;
        }
        else if ($c < 0x800)
        {
            $str.=chr(0xC0 | $c>>6);
            $str.=chr(0x80 | $c & 0x3F);
        }
        else if ($c < 0x10000)
        {
            $str.=chr(0xE0 | $c>>12);
            $str.=chr(0x80 | $c>>6 & 0x3F);
            $str.=chr(0x80 | $c & 0x3F);
        }
        else if ($c < 0x200000)
        {
            $str.=chr(0xF0 | $c>>18);
            $str.=chr(0x80 | $c>>12 & 0x3F);
            $str.=chr(0x80 | $c>>6 & 0x3F);
            $str.=chr(0x80 | $c & 0x3F);
        }
        return $str;
    }

    /**
    功能:将一个字符串写入图片文件,实现图文混排的内部函数每次调用生成一张JPG,以便最终合成为多帧动画GIF
    参数:
    string:字符串
    background:选用的背景图,源文件名,未使用
    dst:目的文件名
    font:选用的字体编号,0表示随机选择,-1表示顺序选择
    fontsize:选用的字号
    type:印章类型,左起位 1,阴刻阳刻;2,方形圆形
    */
    function CreateIMG_in($string,$type,$background,$dst,$font,$fontsize=48){
        $font_array = array();
        $font_array[0] = "/www/webroot/caizi/font/stxinwei.ttf";
        $font_array[1] = "/www/webroot/caizi/font/MJ1GFM.TTF";
        $font_array[2] = "/www/webroot/caizi/font/simkai.ttf";
        $font_array[3] = "/www/webroot/caizi/font/yin2.TTF";
        $font_array[4] = "/www/webroot/caizi/font/yin6.TTF";
        $font_array[5] = "/www/webroot/caizi/font/yin4.TTF";
        $font_array[6] = "/www/webroot/caizi/font/yin5.TTF";
        $font_array[7] = "/www/webroot/caizi/font/yin3.TTF";

        if($font==0){//随机选择一个字体
            $font = Floor(rand(0,100)/count($font_array));
        }
        $font = $font_array[$font];//生成汉字使用的字体
        switch(Floor($type/10)){//生成汉字色
            case 1://阴刻,字体用白色
                $string_color = "#FFFFFF";
                break;
            case 2://阳刻,字体用红色
                $string_color = "#FF0000";
                break;
        }
        $fp = file("/www/webroot/caizi/pos.txt");
        $pos = explode(";",$fp[$type%10-1]);
        
        for($i=0;$i<count($pos);$i++){
            $str = substr($string,$i*2,2);
            $pos_n = explode(",",$pos[$i]);
            $text_x = $pos_n[0];$text_y = $pos_n[1];
            //echo $str."-".$text_x.",".$text_y;
            $this->Char2GIF($str,$dst,$dst,$text_x,$text_y,$string_color,$font, $fontsize);//写入图片文件
        }
    }

    /**
    功能:该类的唯一一个对外接口,用于生成多帧的印章图片
    string:字符串,写入印章的内容,自动转换为2或4个字
    type:印章类型,左起位 1,阴刻阳刻;2,方形圆形
    dst:目的文件名
    font:选用的字体编号
    fontsize:字号
    n:最终生成的图片要几帧
    */
    function CreateIMG($string,$type,$dst,$font,$fontsize=48,$n=3){
        $fn=array();
        
        switch($type%10){
            case 1:
                $cmd = "convert -delay 100 -size 96x96 ";
                switch(count($string)){
                    case 4:$string.="之印";break;
                    case 6:$string.="印";break;
                }
                $string = substr($string,0,8);
                break;
            case 2:
                $cmd = "convert -delay 100 -size 103x55 ";
                $string = substr($string,0,4);
                break;
            case 3:
            case 4:
                $cmd = "convert -delay 100 -size 128x128 ";
                $string = substr($string,0,12);
                break;
        }
        switch($type){
            case 11:
                $background = "/www/webroot/caizi/red11.gif";
                break;
            case 12:
                $background = "/www/webroot/caizi/red12.gif";
                break;
            case 21:
                $background = "/www/webroot/caizi/red21.gif";
                break;
            case 22:
                $background = "/www/webroot/caizi/red22.gif";
                break;
            case 13:
                $background = "/www/webroot/caizi/heart13.gif";
                break;
            case 14:
                $background = "/www/webroot/caizi/butterfly14.gif";
                break;
        }
        for($i=1;$i<=$n;$i++){
            $fn[$i]=$this->create_new_file("/www/webroot/caizi","tmp",".gif");
            $x=exec("cp $background ".$fn[$i]);
            if($font<0) $this->CreateIMG_in($string,$type,$background,$fn[$i],$i,$fontsize);
            else $this->CreateIMG_in($string,$type,$background,$fn[$i],$font,$fontsize);
            $cmd .= " -page +0+0 ".$fn[$i];
        }
        $cmd .=" -loop 0 $dst";
        echo $cmd."\n";
        $x=exec($cmd);
        for($i=1;$i<=$n;$i++){
            unlink($fn[$i]);
        }
    }
}
$Gif1 =new GIF1();
$fn=$Gif1->create_new_file("/www/webroot/caizi","result",".gif");
//$Gif1->CreateIMG("某某之印",11,$fn,-1,42,3);
$Gif1->CreateIMG($argv[1],$argv[2],$fn,$argv[4],$argv[3],3);
?>

阅读(373) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册