Apr
14

生成签名图片代码     2009

 23:52    577    0   jane 程序 代码 源码 不指定 | |
生成签名图片代码
签名
PHP代码:

<?php
error_reporting(E_ALL & ~E_NOTICE);
$b = date('H:i:s Y-m-d ')."\n小鱼 says: \n这只是个测试,
更多内容很快回来....
please push 'F5'...";

//$backKey = mt_rand(1,10);
$backKey  = 1;

$config = new stdClass();
$config->font = './images/font5.ttf';                  //字体文件
$config->backImg =  "./images/back_$backKey.jpg";        //背景图
$config->borderColor = '009900';                        //边框颜色
$config->textColor = '333333';                                //文字颜色
$config->top = 20;                                                        //顶部位置
$config->left = 20;                                                //左部位置
$config->size = 12;                                                        //文字大小


$t2i = new txt2image($config);
$t2i->show($b);
class txt2image{
        // 图像types 对应表
        protected $types = array(
                        1 => 'gif',
                        2 => 'jpg',
                        3 => 'png',
                        6 => 'bmp'
                    );
    // 参数
    protected $config;
    
    // 构造方法
    public function __construct($config){
            $this->config = $config;        
    }
    
    // html2rgb
    protected function html2rgb($html='000000'){
            $color['r'] = hexdec(substr($html, 0, 2));
            $color['g'] = hexdec(substr($html, 2, 2));
            $color['b'] = hexdec(substr($html, 4, 2));
            return $color;
    }
    
        // imageInfo
        public function getImageInfo($img){
                $info = @getimagesize($img);
                if(isset($this->types[$info[2]])){
                        $info['ext'] = $info['type'] = $this->types[$info[2]];
                } else{
                        $info['ext'] = $info['type'] = 'jpg';
                }
                $info['type'] == 'jpg' && $info['type'] = 'jpeg';
                return $info;
        }
        
        
        
        // show
        public function show($code='TEST'){
                // 加载背景图片
                $info = $this->getImageInfo($this->config->backImg);
                if(!empty($info[0])){
                        $width = $info[0];
                        $height = $info[1];
                        $type  = $info['type'];
                        $ext   = $info['ext'];
                        $fun   = 'imagecreatefrom'.$type;
                        $im = $fun($this->config->backImg);
                } else{
                        $this->error($this->config->backImg.': 背景图片读取失败!');
                }
                !is_file($this->config->font) && $this->error($this->config->font.': 字体文件读取失败!');
                
                // 设定图像的混色模式
                imagealphablending($im, true);        
            // 定义要用到的颜色
            $b_rgb = $this->html2rgb($this->config->borderColor);
            $t_rgb = $this->html2rgb($this->config->textColor);
            $border_color = imagecolorallocate($im, $b_rgb['r'], $b_rgb['g'], $b_rgb['b']);
            $text_color = imagecolorallocate($im, $t_rgb['r'], $t_rgb['g'], $t_rgb['b']);
            // 画边框
            imagerectangle($im,0,0,$width-1,$height-1,$border_color);
            // 写文字
                imagefttext($im, $this->config->size , 0, $this->config->left, $this->config->top,  $text_color, $this->config->font,$code);
                // 输出图片PNG
                header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
                header("Content-type: image/png");
                imagepng($im);
                imagedestroy($im);
        }
        
        // 抛出错误
        protected function error($msg='Error!'){
                die($msg);        
        }
        
}
?>

作者:jane@淘宝网女装新款秋装连衣裙裤子外套上衣_2012时尚女装新款 Ecmall二次开发-PHP技术
地址:http://www.laohucheng.com/post/319/
版权所有©转载时必须以链接形式注明作者和原始出处及本声明!

Tags: 引用(0)
发表评论
昵称 [注册]
密码 游客无需密码
网址
电邮
打开HTML 打开UBB 打开表情 隐藏 记住我