PHP绘图案例讲解验证码制作

<?php
header("Content-type: image/png");//声明浏览器解析为图片
$width=200;
$height=100;
$color1=imagecreate($width,$height); //绘制一个画布


    imagecolorallocate($color1, 60, 60, 40);//设置此选项后,画的点或者是线连接之后回成团,带颜色
    $color_T=imagecolorallocate($color1, rand(200,255), rand(200,255), rand(200,255));//随机数取颜色
    //矩形边框的颜色
    $color_B=imagecolorallocate($color1, rand(200,255), rand(200,255), rand(200,255));
    //字体颜色
    $colorString=imagecolorallocate($color1, rand(10,100), rand(10,100), rand(10,100));
    //画点    循环
    for($i=0;$i<100;$i++){
        imagesetpixel($color1, rand(0,$width-1), rand(0,$height-1),
            imagecolorallocate($color1, rand(100,200), rand(100,200), rand(100,200)));//画点函数
    }
    //画线
    for ($j=0;$j<5;$j++){
        imageline($color1, rand(0,$width-30),rand(0,$height), rand(0,$width/2),rand(0,$height),
            imagecolorallocate($color1, rand(100,200), rand(100,200), rand(100,200)));//画线函数
    }

    //写字
    /* imagestring($color1, 50, 0, 0, 'abcf', $colorString); *///两个不同的写字函数
    imagettftext($color1, 30,20, 0, 40, $colorString,'COMSC.TTF', 'abcv');//两个不同的写字函数
    //-------------------------------------------------------
    imagefill($color1, 0, 0, $color_T);//图像着色
    //画一个矩形
    imagerectangle($color1,0,0,$width-1, $height-1, $color_B);
    imagepng($color1);

 

posted @ 2019-07-06 09:46  一半人生  阅读(189)  评论(0编辑  收藏  举报