This article introduces the random verification code generated by php. Now I share it with everyone. Friends in need can refer to it
<?php /** * 生成随机字符串 * @param int $num 生成的随机字符串的个数 * @return str 生成的随机字符串 */ function randStr($num=6) { $str = str_shuffle('abcedfghjkmnpqrstuvwxyzABCEDFGHJKMNPQRSTUVWXYZ23456789'); return substr($str, 0 , $num); } //1.创建画布 $pic = imagecreatetruecolor(80, 50); //2.创建颜料(RGB) $red = imagecolorallocate($pic, 255, 0, 0); $blue = imagecolorallocate($pic, 127, 127, 127); //3.背景填充 imagefill($pic, 0, 0, $red); //4.写入文字 imagestring($pic, 5, 5, 5, randStr(4), $blue); //5. 输出/保存图形 header('Content-type:image/png'); imagepng($pic); //6. 销毁画布(关闭画板) imagedestroy($pic); ?>
Related recommendations:
006-PHP common function encapsulation
The above is the detailed content of 008-php generates random verification code. For more information, please follow other related articles on the PHP Chinese website!