生成中奖文字图片

   //调用 
   public function cp(){        
        $this->_createPic(285,141,18,30,75,'block','电动自行车一辆3500km/h及安全帽一个');
    }

    /**
     * @param int $imgW  画布的宽度
     * @param int $imgH  画布的高度
     * @param int $imgTestSize  字体的尺寸为像素尺寸(GD1)或点(磅)尺寸(GD2)。
     * @param int $imgTextX  由 x,y 所表示的坐标定义了第一个字符的基本点
     * @param int $imgTestY
     * @param string $imgBackgroundColor 画布的背景颜色  分两种 :block 表示黑色  其它表示为白色
     * @param string $text  要显示的文字内容
     */
    private  function _createPic($imgW=285,$imgH=141,$imgTestSize=20,$imgTextX=50,$imgTestY=75,$imgBackgroundColor='block',$text= "我叫苏三"){
        $font =WEB_ROOT .'Lib/ORG/simsun.ttf';
        $font=str_replace("\\", "/",$font);
        $im = imagecreate($imgW, $imgH);
        if($imgBackgroundColor==='block'){
            $bg = imagecolorallocate($im,  0, 0, 0); //设置画布的背景为白色
            $black = imagecolorallocate($im,255, 255, 255); //设置一个颜色变量为黑色
        }else{
            $bg = imagecolorallocate($im,255, 255, 255); //设置画布的背景为白色
            $black = imagecolorallocate($im,0, 0, 0); //设置一个颜色变量为黑色
        }

        //$textLen=_strlen($text);
        $textLen=abslength($text);
        //判断是否大于两行
        if($textLen>10){
            $arr[0]=mb_substr($text, 0, 10, 'utf-8');
            //大于两行,进行省略
            if($textLen>=20){
                $arr[1]=mb_substr($text, 10, 9, 'utf-8').'...';
            }else{
                $arr[1]=mb_substr($text, 10, 10, 'utf-8');
            }
            imagettftext($im, $imgTestSize, 0, $imgTextX, $imgTestY-15, $black, $font, $arr[0]);      //输出一个灰色的字符串作为阴影
            imagettftext($im, $imgTestSize, 0, $imgTextX, $imgTestY+15, $black, $font, $arr[1]);      //输出一个灰色的字符串作为阴影
        }else{
            if($textLen>=1 && $textLen<8){
                $imgTextX+=(10-$textLen)*10;//一个字平均占10个像素
            }
            imagettftext($im, $imgTestSize, 0, $imgTextX, $imgTestY, $black, $font, $text);      //输出一个灰色的字符串作为阴影
        }
        header('Content-type:image/png');
        imagepng($im);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值