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 字体的路径。
下载的链接和测试的图片
本文字体的链接 <-点击下载

PHP利用字体向图像生成验证码_第1张图片

你可能感兴趣的:(php)