PHP學習筆記-簡單的面向過程寫的驗證碼
阿新 • • 發佈:2017-10-11
php
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017\10\10 0010 * Time: 19:44 */ //生成隨機驗證碼 $strNumber = join(‘‘,range(0,9)); $strChar = join(‘‘,range(‘a‘,‘z‘)); $str = $strNumber.$strChar.strtoupper($strChar); $str = substr(str_shuffle($str),‘0‘,4); header("Content-type: image/png"); //創建畫布 $width = 100; $height = 50; $image = imagecreate($width,$height); //創建顏色 $bgcolor = imagecolorallocate($image,rand(0,20),rand(0,20),rand(0,20)); //備用顏色(幹擾線) $colorone = imagecolorallocate($image,rand(100,155),rand(200,155),rand(100,155));; //幹擾線 for($j=0; $j<10; $j++){ imageline($image, rand(0,$width), rand(0,$height-20), rand(0,$width), rand(0,$height), $colorone); } //循環添加字符到畫布 每個字符的坐標為(總寬度 / 驗證碼的個數) for($i=0; $i<4; $i++){ //備用顏色(文字) $fontcolor= imagecolorallocate($image,rand(0,255),rand(100,200),rand(180,255)); $x = (($width / 4) * $i) + $width / 10; $y = rand(10,$height-20); imagechar($image, 5, $x, $y,$str[$i], $fontcolor); } imagepng($image); imagedestroy($image);
本文出自 “YBT-PHP” 博客,請務必保留此出處http://ybtphp.blog.51cto.com/13257319/1971298
PHP學習筆記-簡單的面向過程寫的驗證碼