olakunlevpn/phpcapcha is a PHP 8.0+ library for generating secure, customizable CAPTCHA images using the GD extension. It creates a GdImage resource, applies visual manipulations (noise, lines, wave distortion), and outputs the result in PNG format.
Namespace: Olakunlevpn\Captcha
The class maintains internal state for customization. All properties are modified via chainable setter methods.
| Property | Type | Default | Description |
|---|---|---|---|
$fontPath |
string |
null (Auto-detects) |
Path to .ttf file or directory of fonts. |
$length |
int |
6 |
Number of characters (ignored in Math mode). |
$type |
string |
'mixed' |
Generator mode: 'mixed', 'alpha', 'numeric', 'math'. |
$textColor |
array |
[0, 0, 0] |
RGB array for text. |
$backgroundColor |
array |
[255, 255, 255] |
RGB array for background. |
$distortion |
array |
['enabled' => true...] |
Wave effect configuration. |
$noise |
array |
['enabled' => true...] |
Dot noise configuration. |
$lines |
array |
['enabled' => true...] |
Line noise configuration. |
Responsibility:
- Validates font path (throws
Exceptionif missing). - Generates the challenge code/text based on
$type. - Creates a blank GD image of specified dimensions.
- Fills background color.
- Applies noise (dots) if enabled.
- Applies lines if enabled.
- Renders text with random angles and offsets.
- Applies wave distortion if enabled.
Responsibility:
- Sends HTTP headers (
Content-Type: image/png,Cache-Control: no-store). - Outputs the image binary stream.
- Destroys the image resource to free memory.
Returns: A Data URI string (e.g., data:image/png;base64,...).
Responsibility: Writes the generated image to the file system.
Returns: The valid answer for the current captcha (e.g., "8" for a math problem "5 + 3").
- Logic: Generates two random integers (1-9) and an operator (currently defaults to
+). - Display: Renders
"5 + 3". - Code: Stores
"8".
- If a directory path is passed to
setFont(), the library scans for*.ttffiles and randomly selects one for the current instance.
- Algorithm: Sinusoidal wave mapping.
- Formula:
y_new = y + amplitude * sin(x / period) - Effect: shifts pixels vertically based on horizontal position to create a ripple effect.