-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingleChar.php
executable file
·125 lines (125 loc) · 3.56 KB
/
SingleChar.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace FileCMS\Common\Image;
/**
* Creates a single image, by default black on white
*/
use Exception;
use FileCMS\Common\Image\Strategy\ {PlainText,PlainFill};
class SingleChar
{
const MARGIN = 3;
const DEFAULT_FG = [0x00, 0x00, 0x00];
const DEFAULT_BG = [0xFF, 0xFF, 0xFF];
const DEFAULT_TX_X = 25;
const DEFAULT_TX_Y = 75;
const DEFAULT_TX_SIZE = 60;
const DEFAULT_TX_ANGLE = 0;
const DEFAULT_WIDTH = 100;
const DEFAULT_HEIGHT = 100;
const ERR_GD = 'ERROR: GD extension needs to be installed to use CAPTCHA';
public $text = '';
public $fontFile = '';
public $width = 0;
public $height = 0;
public $size = 0;
public $angle = 0.00;
public $textX = 0;
public $textY = 0;
public $fgColor = NULL;
public $bgColor = NULL;
public $image = NULL;
/**
* Builds an image based on config specs
*
* @param string $text
* @param string $fontFile
* @param int $width
* @param int $height
* @param int $size
* @param float $angle
* @param int $textX : x coordinate of text
* @param int $textY : y coordinate of text
*/
public function __construct(
string $text,
string $fontFile,
int $width = self::DEFAULT_WIDTH,
int $height = self::DEFAULT_HEIGHT,
int $size = self::DEFAULT_TX_SIZE,
float $angle = self::DEFAULT_TX_ANGLE,
int $textX = self::DEFAULT_TX_X,
int $textY = self::DEFAULT_TX_Y)
{
if (!function_exists('imagecreate'))
throw new Exception(self::ERR_GD);
$this->text = $text;
$this->fontFile = $fontFile;
$this->width = $width;
$this->height = $height;
$this->size = $size;
$this->angle = $angle;
$this->textX = $textX;
$this->textY = $textY;
$this->image = \imagecreate($width, $height);
$this->fgColor = $this->colorAlloc(self::DEFAULT_FG);
$this->bgColor = $this->colorAlloc(self::DEFAULT_BG);
}
/**
* Randomizes FG color
* Stores value in $this->fgColor
*
*
* @return void
*/
public function randFgColor()
{
$r = rand(0x22, 0x88);
$g = rand(0x22, 0x88);
$b = rand(0x22, 0x88);
$this->fgColor = $this->colorAlloc([$r, $g, $b]);
}
/**
* Allocates a color resource
*
* @param array|int $r,
* @param int $g
* @param int $b]
* @return int $color
*/
public function colorAlloc($r, $g = 0, $b = 0)
{
if (is_array($r)) {
[$r, $g, $b] = $r;
}
return \imagecolorallocate($this->image, $r, $g, $b);
}
/**
* Fills image background
*
*/
public function writeFill()
{
PlainFill::writeFill($this, 0, 0, $this->width, $this->height, $this->fgColor);
PlainFill::writeFill($this, 1, 1, $this->width - self::MARGIN, $this->height - self::MARGIN, $this->bgColor);
}
/**
* Writes text onto image
* See: https://www.php.net/manual/en/function.imagettftext.php
*/
public function writeText()
{
return PlainText::writeText(
$this, $this->size, $this->angle, $this->textX,
$this->textY, $this->fgColor, $this->fontFile, $this->text);
}
/**
* Renders image as PNG
*
* @param string $fn : filename where to write image
* @return bool
*/
public function save(string $fn)
{
return \imagepng($this->image, $fn);
}
}