PHP生成图片

/**
 * 自定义分享图函数
 * @param  [type] $source [背景图地址]
 * @param  [type] $text1  [姓名]
 * @param  [type] $text2  [电话]
 * @param  [type] $text3  [授权码]
 * @param  [type] $text4  [有效日期]
 * @param  string $font   [字体]
 * @return [type]         [return图片路径]
 */
function generateImg($source, $text1, $text2, $text3, $text4, $font = './msyh.ttf') {


    @$date = '' . date ( 'Ymd' ) . '/';
    $img = $date . md5 ( $source . $text1 . $text2 . $text3 . $text4 ) . '.jpg';
    if (file_exists ( './' . $img )) {
        return $img;
    }
 
    $main = imagecreatefromjpeg ( $source );
 
    $width = imagesx ( $main );
    $height = imagesy ( $main );
 
    $target = imagecreatetruecolor ( $width, $height );
 
    $white = imagecolorallocate ( $target, 255, 255, 255 );
    imagefill ( $target, 0, 0, $white );
 
    imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );
 
    $fontSize = 17;//磅值字体
    $fontColor = imagecolorallocate ( $target, 14, 2, 12);//字的RGB颜色

    $namelen = mb_strlen($text1,"utf-8");
    if($namelen == 2){
        $x = 425;
    }else{
        $x = 410;
    }
    $fontBox = imagettfbbox($fontSize, 0, $font, $text1);//文字水平居中实质
    imagettftext ( $target, 23, 0, $x, 505, $fontColor, $font, $text1 );
 
    $fontBox = imagettfbbox($fontSize, 0, $font, $text2);
    imagettftext ( $target, $fontSize, 0, 378, 634, $fontColor, $font, $text2 );
 
    $fontBox = imagettfbbox($fontSize, 0, $font, $text3);
    imagettftext ( $target, $fontSize, 0, 378, 678, $fontColor, $font, $text3 );

    $fontBox = imagettfbbox($fontSize, 0, $font, $text4);
    imagettftext ( $target, $fontSize, 0, 378, 721, $fontColor, $font, $text4 );
 
    //imageantialias($target, true);//抗锯齿,有些PHP版本有问题,谨慎使用
 
    // imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontColor );//画三角形
    // imageline($target, 100, 200, 20, 142, $fontColor);//画线
    // imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//画矩形
 
    //bof of 合成图片
    // $child1 = imagecreatefromjpeg ( 'http://gtms01.alicdn.com/tps/i1/T1N0pxFEhaXXXxK1nM-357-88.jpg' );
    // imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
    //eof of 合成图片
 
    @mkdir ( './' . $date );
    imagejpeg ( $target, './' . $img, 95 );
 
    imagedestroy ( $main );
    imagedestroy ( $target );
    // imagedestroy ( $child1 );
    return $img;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值