-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.class.php
More file actions
72 lines (62 loc) · 1.67 KB
/
code.class.php
File metadata and controls
72 lines (62 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
class code{
private $letter="abcdefgABCDEFG";
private $letterNum=4;
public $width=200;
public $height=80;
public $lineNum=10;
public $imgType="png";
public function output(){
header("content-type:image/".$this->imgType);
$out="image".$this->imgType;
//1.创建画布->颜色填充
$this->create();
//2.创建文字->随机->颜色
$this->createText();
//3.干扰线->随机->颜色
$this->createLine(5);
imagepng($this->img);
}
private function create(){
$this->img=imagecreatetruecolor($this->width,$this->height);
$bgcolor=$this->getColor();
imagefill($this->img,0,0,$bgcolor);
}
private function createText(){
$str=$this->getRandom($this->letterNum);
for($i=0;$i<4;$i++){
$textcolorr=$this->getTextColor();
imagefttext($this->img, 40, 0, 50*$i, 55, $textcolorr,"font.ttf",$str[$i]);
}
}
private function createLine($num){
$bgcolorr=$this->getColor();
for($i=0;$i<$num;$i++){
imageline($this->img,mt_rand(0,20),mt_rand(0,80),mt_rand(180,$this->width),mt_rand(0,$this->height),$bgcolorr);
}
}
private function getColor(){
for($i=0;$i<3;$i++){
$arr[$i]=mt_rand(0,128);
}
$bgcolor=imagecolorallocate($this->img,$arr[0],$arr[1],$arr[2]);
return $bgcolor;
}
private function getTextColor(){
for($i=0;$i<4;$i++){
$arr[$i]=mt_rand(128,255);
}
$bgcolor=imagecolorallocate($this->img,$arr[0],$arr[1],$arr[2]);
return $bgcolor;
}
private function getRandom($num){
$strs=Array();
for ($i = 0; $i < $num; $i++) {
$strs[$i]= $this->letter[mt_rand(0,strlen($this->letter)-1)];
}
return $strs;
}
}
$a=new code();
$r=$a->output();
?>