PHP利用字体向图像生成验证码

imagettftext — 用 TrueType 字体向图像写入文本

public function drawImg() {
        // Set the content-type
        header('Content-Type: image/png');
        // Create the canvas
        $im = imagecreatetruecolor(150, 30);
        
        // Set the color, where the gradient color is set
        $white = imagecolorallocate($im, 255, 255, 255);
        $grey = imagecolorallocate($im, 128, 128, 128);
        $black = imagecolorallocate($im, 0, 0, 0);
        
        imagefilledrectangle($im, 0, 0, 399, 29, $white);
        // The text to draw
        $text = 'di~di 9527';
        // Replace path by your own font path
        $font = '/statics/4.ttf';
        // Add some shadow to the text  
        // Note: imagerotate 此函数仅在与 GD 库捆绑编译的 PHP 版本中可用。
        imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
        // Add the text
        imagettftext($im, 20, 0, 10, 22, $black, $font, $text);
        // Using imagepng() results in clearer text compared with imagejpeg()
        imagepng($im);
        // destroy
        imagedestroy($im);
    }

下面介绍一下该函数的参数(字数太多嫌麻烦直接抄手册的)
image
由图象创建函数(例如imagecreatetruecolor())返回的图象资源。
size
字体的尺寸。根据 GD 的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)。
angle
角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
x
由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 “top left” 为 0, 0。
y
Y 坐标。它设定了字体基线的位置,不是字符的最底端。
color
颜色索引。使用负的颜色索引值具有关闭防锯齿的效果。见 imagecolorallocate()。
fontfile
是想要使用的 TrueType 字体的路径。
下载的链接和测试的图片
本文字体的链接 <-点击下载

该方法生成的图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值