Skip to content

Commit

Permalink
- Bugfix in checkConfig
Browse files Browse the repository at this point in the history
- more advanced checks in checkConfig
  • Loading branch information
pstimpel committed Nov 19, 2018
1 parent 322a980 commit a0dcefc
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
10 changes: 9 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@

'size' => array('width' => 200, 'height' => 50), //dimensions of returned graphics

'fontsize' => 25 // size of font
'fontsize' => 25, // size of font

'numberOfLines' => 6, //numbers of lines to draw for adding some confusion to the image

'thicknessOfLines' => 2, //thickness of lines to draw for adding some confusion to the image, in px

'linecolor' => array('r' => 128, 'g' => 128, 'b' => 128) //text color of lines across image; values or r, g or b
// can be from 0 to 255 each

);
93 changes: 90 additions & 3 deletions php/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function checkConfig(&$phpcaptchaConfig) {

$phpcaptchaConfig['stringLength'] = 6;

} else {

$phpcaptchaConfig['stringLength'] = round($phpcaptchaConfig['stringLength'],0);

}

if(!isset($phpcaptchaConfig['charsToUse']) || strlen($phpcaptchaConfig['charsToUse']) < 10) {
Expand All @@ -21,7 +25,7 @@ function checkConfig(&$phpcaptchaConfig) {

}

if(!isset($phpcaptchaConfig['strictLowerCase'])) {
if(!isset($phpcaptchaConfig['strictLowerCase']) || !is_bool($phpcaptchaConfig['strictLowerCase'])) {

$phpcaptchaConfig['strictLowerCase'] = true;

Expand All @@ -32,8 +36,8 @@ function checkConfig(&$phpcaptchaConfig) {
!isset($phpcaptchaConfig['bgcolor']['g']) ||
!isset($phpcaptchaConfig['bgcolor']['b']) ||
!is_numeric($phpcaptchaConfig['bgcolor']['r']) ||
!is_numeric($phpcaptchaConfig['bgcolor']['r']) ||
!is_numeric($phpcaptchaConfig['bgcolor']['g']) ||
!is_numeric($phpcaptchaConfig['bgcolor']['b']) ||
$phpcaptchaConfig['bgcolor']['r'] < 0 ||
$phpcaptchaConfig['bgcolor']['r'] > 255 ||
$phpcaptchaConfig['bgcolor']['g'] < 0 ||
Expand All @@ -44,15 +48,21 @@ function checkConfig(&$phpcaptchaConfig) {

$phpcaptchaConfig['bgcolor'] = array('r' => 0, 'g' => 0, 'b' => 0);

} else {

$phpcaptchaConfig['bgcolor']['r'] = round($phpcaptchaConfig['bgcolor']['r'],0);
$phpcaptchaConfig['bgcolor']['g'] = round($phpcaptchaConfig['bgcolor']['g'],0);
$phpcaptchaConfig['bgcolor']['b'] = round($phpcaptchaConfig['bgcolor']['b'],0);

}

if(!isset($phpcaptchaConfig['textcolor']) ||
!isset($phpcaptchaConfig['textcolor']['r']) ||
!isset($phpcaptchaConfig['textcolor']['g']) ||
!isset($phpcaptchaConfig['textcolor']['b']) ||
!is_numeric($phpcaptchaConfig['textcolor']['r']) ||
!is_numeric($phpcaptchaConfig['textcolor']['r']) ||
!is_numeric($phpcaptchaConfig['textcolor']['g']) ||
!is_numeric($phpcaptchaConfig['textcolor']['b']) ||
$phpcaptchaConfig['textcolor']['r'] < 0 ||
$phpcaptchaConfig['textcolor']['r'] > 255 ||
$phpcaptchaConfig['textcolor']['g'] < 0 ||
Expand All @@ -63,6 +73,37 @@ function checkConfig(&$phpcaptchaConfig) {

$phpcaptchaConfig['textcolor'] = array('r' => 255, 'g' => 255, 'b' => 255);

} else {

$phpcaptchaConfig['textcolor']['r'] = round($phpcaptchaConfig['textcolor']['r'],0);
$phpcaptchaConfig['textcolor']['g'] = round($phpcaptchaConfig['textcolor']['g'],0);
$phpcaptchaConfig['textcolor']['b'] = round($phpcaptchaConfig['textcolor']['b'],0);

}

if(!isset($phpcaptchaConfig['linecolor']) ||
!isset($phpcaptchaConfig['linecolor']['r']) ||
!isset($phpcaptchaConfig['linecolor']['g']) ||
!isset($phpcaptchaConfig['linecolor']['b']) ||
!is_numeric($phpcaptchaConfig['linecolor']['r']) ||
!is_numeric($phpcaptchaConfig['linecolor']['g']) ||
!is_numeric($phpcaptchaConfig['linecolor']['b']) ||
$phpcaptchaConfig['linecolor']['r'] < 0 ||
$phpcaptchaConfig['linecolor']['r'] > 255 ||
$phpcaptchaConfig['linecolor']['g'] < 0 ||
$phpcaptchaConfig['linecolor']['g'] > 255 ||
$phpcaptchaConfig['linecolor']['b'] < 0 ||
$phpcaptchaConfig['linecolor']['b'] > 255
) {

$phpcaptchaConfig['linecolor'] = array('r' => 128, 'g' => 128, 'b' => 128);

} else {

$phpcaptchaConfig['linecolor']['r'] = round($phpcaptchaConfig['linecolor']['r'],0);
$phpcaptchaConfig['linecolor']['g'] = round($phpcaptchaConfig['linecolor']['g'],0);
$phpcaptchaConfig['linecolor']['b'] = round($phpcaptchaConfig['linecolor']['b'],0);

}

if(!isset($phpcaptchaConfig['font']) ||
Expand Down Expand Up @@ -90,12 +131,31 @@ function checkConfig(&$phpcaptchaConfig) {

$phpcaptchaConfig['size'] = array('width' => 200, 'height' => 50);

} else {
$phpcaptchaConfig['size']['width'] = round($phpcaptchaConfig['size']['width'],0);
$phpcaptchaConfig['size']['height'] = round($phpcaptchaConfig['size']['height'],0);
}

if(!isset($phpcaptchaConfig['fontsize']) ||
!is_numeric($phpcaptchaConfig['fontsize']) ||
$phpcaptchaConfig['fontsize'] < 5) {
$phpcaptchaConfig['fontsize'] = 25;
} else {
$phpcaptchaConfig['fontsize'] = round($phpcaptchaConfig['fontsize'], 0);
}

if(!isset($phpcaptchaConfig['numberOfLines']) || !is_numeric($phpcaptchaConfig['numberOfLines'])
|| $phpcaptchaConfig['numberOfLines'] < 0) {
$phpcaptchaConfig['numberOfLines'] = 6;
} else {
$phpcaptchaConfig['numberOfLines'] = round($phpcaptchaConfig['numberOfLines'],0);
}

if(!isset($phpcaptchaConfig['thicknessOfLines']) || !is_numeric($phpcaptchaConfig['thicknessOfLines'])
|| $phpcaptchaConfig['thicknessOfLines'] < 0) {
$phpcaptchaConfig['thicknessOfLines'] = 2;
} else {
$phpcaptchaConfig['thicknessOfLines'] = round($phpcaptchaConfig['thicknessOfLines'],0);
}

try {
Expand Down Expand Up @@ -200,6 +260,33 @@ function renderImage($phpcaptchaConfig, $captchaChallenge) {

imagettftext($im, $phpcaptchaConfig['fontsize'], 0, $xcord, $ycord, $fontcolor, $font, $spacedText);

if($phpcaptchaConfig['numberOfLines']>0) {

$linecolor = imagecolorallocate($im,
$phpcaptchaConfig['linecolor']['r'],
$phpcaptchaConfig['linecolor']['g'],
$phpcaptchaConfig['linecolor']['b']);

for($i=1;$i<=$phpcaptchaConfig['numberOfLines'];$i++) {

$x1 = rand(0,$phpcaptchaConfig['size']['width']);
$y1 = rand(0,$phpcaptchaConfig['size']['height']);

$x2 = rand(0,$phpcaptchaConfig['size']['width']);
$y2 = rand(0,$phpcaptchaConfig['size']['height']);

for($j=0;$j<$phpcaptchaConfig['thicknessOfLines'];$j++) {

imageline($im, $x1, $y1 + $j,
$x2,
$y2 + $j,
$linecolor);


}
}
}

imagepng($im);

imagedestroy($im);
Expand Down
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ PHP Captcha is using GD to generate images. PHP5 and PHP7 are supported.

**fontsize**: The font size to be used, default is 25

**numberOfLines**: The number of lines to draw across the image for creating some small confusion to OCR, default is 6

**thicknessOfLines**: The thickness per line, default is 2

**linecolor.r**: Red value of line color, default is 128, valid from 0 to 255

**linecolor.g**: Green value of line color, default is 128, valid from 0 to 255

**linecolor.b**: Blue value of line color, default is 128, valid from 0 to 255


## How to use it?

There is a [Demo](demo/) available, if you unzipped the package at your webserver. Just navigate to the demo subfolder using your webbrowser. If GD is missing, PHP Captcha will detect it and throw an error. If you misconfigured PHP Captcha, it will try to run on defaults, or throw an error depending on the type of your failure.
Expand Down

0 comments on commit a0dcefc

Please sign in to comment.