<?php
class Verify
{
private $fontfile = 'E:\phpstudy_pro\WWW\php7\simkai.ttf';
public $width = 120;
public $height = 50;
public $red = 25;
public $blue = 10;
public $green = 30;
private $img = null;
public function index()
{
session_start();
$im = imagecreatetruecolor($this->width, $this->height);
$this->img = $im;
$color = imagecolorallocate($im, $this->red,$this->green,$this->blue);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 400, 300, 0, $color);
if(!file_exists($this->fontfile))
{
echo "字体文件不存在";
die;
}
$str = $this->random();
for($i=0;$i <= strlen($str);$i++)
{
imagettftext($im, 24, mt_rand(0,5), $i*20, 35, $white, $this->fontfile,substr($str,strlen($str)-$i,1));
}
$this->createLine();
header('Content-type:image/png');
imagepng($im);
imagedestroy($im);
}
protected function random($str = null)
{
if(is_null($str)){
$str = mt_rand(0000,9999);
}
if(strlen($str) != 4){
$str = $str.mt_rand(1,9);
}
$_SESSION['code'] = strrev($str);
$_SESSION['time'] = time() + 30;
return $str;
}
private function createLine()
{
for ($i = 0; $i < 10; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
}
for ($i = 0; $i < 20; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
}
}
}
$verfiy = new Verify();
$verfiy->index();
好好学习,
转载请注明原文地址: https://lol.8miu.com/read-2040.html