search
Homephp教程PHP源码PHP高自定义性安全验证码

php代码

<?php
public function captcha(){
    $font_dir   = $_SERVER ["DOCUMENT_ROOT"] . "your_ttf_file.ttf";	// 字体库
    $img_w      = 58;   // 设置图片宽
    $img_h      = 20;   // 设置图片高
    $font_size  = 11;   // 字体大小
    $angle_l    = -10;  // 左偏角
    $angle_r    = 10;   // 右偏角
    $code_str   = "ABCDEFGHJKLMNPQRSTUVWXYZ36";
    $word_len   = 4;    // 验证码位数
    $padding    = 5;    // 每两个文字之间间隔
    $margin     = 2;    // 左侧边距
    $base_line  = 15;   // 文字基线位置
    $base_line_offset = 2;  // 基准线偏移量
    $pixel_num  = 3;    // 杂点数目基数
    $pixel_color= 8;    // 杂点只有 $pixel_color 种颜色  总的杂点数为$pixel_num*$pixel_color
    $noise_font_size = 1; // 杂点字体大小
	$session_key= "my_captcha";	//自定义session键名

    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Pragma: no-cache");
    header("Cache-control: private");
    header(&#39;Content-Type: image/png&#39;);

    session_start();
    $word = "";
    $code_str_len = strlen($code_str) - 1;
    for ($i = 0; $i < $word_len; $i++) {
        $word.= $code_str[rand(0, $code_str_len)];
    }
    $_SESSION [$session_key] = strtolower($word);
    $image = imagecreatetruecolor($img_w, $img_h);
    imagefilledrectangle($image, 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate($image, mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255)));
  
    //绘制杂点
    for($i = 0; $i < $pixel_color; $i++){ 
        $noise_color = imagecolorallocate( $image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) );
        for($j = 0; $j < $pixel_num; $j++) {
            imagestring( $image,  $noise_font_size, mt_rand(-10, $img_w), mt_rand(-10, $img_h), $code_str[mt_rand(0, $code_str_len)], $noise_color );
        }
    }
    
    //绘制文字
    for ($i = 0; $i < $word_len; ++$i) {           
        $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
        imagettftext($image, $font_size, mt_rand($angle_l, $angle_r), $margin, mt_rand($base_line-$base_line_offset, $base_line+$base_line_offset), $color, $font_dir, mb_substr($word, $i, 1, &#39;utf-8&#39;));
        $margin += (imagefontwidth($font_size) + $padding);
    }

    imagepng($image);
    imagedestroy($image);
    exit;
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),