Depending on gregwar/captcha (see #42)
This commit is contained in:
parent
95b28d9cec
commit
aed2fa343e
|
@ -1,196 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\CaptchaBundle\Generator;
|
||||
|
||||
/**
|
||||
* Builds a new captcha image
|
||||
* Uses the fingerprint parameter, if one is passed, to generate the same image
|
||||
*
|
||||
* @author Gregwar <g.passault@gmail.com>
|
||||
* @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
|
||||
*/
|
||||
class CaptchaBuilder
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $fingerprint = array();
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $useFingerprint = false;
|
||||
|
||||
/**
|
||||
* Generate the image
|
||||
*/
|
||||
public function build($width, $height, $font, $phrase, $fingerprint = null)
|
||||
{
|
||||
if (null !== $fingerprint) {
|
||||
$this->fingerprint = $fingerprint;
|
||||
$this->useFingerprint = true;
|
||||
} else {
|
||||
$this->fingerprint = array();
|
||||
$this->useFingerprint = false;
|
||||
}
|
||||
|
||||
$i = imagecreatetruecolor($width, $height);
|
||||
$col = imagecolorallocate($i, $this->rand(0, 110), $this->rand(0, 110), $this->rand(0, 110));
|
||||
|
||||
imagefill($i, 0, 0, 0xFFFFFF);
|
||||
|
||||
// Draw random lines
|
||||
for ($t = 0; $t < 10; $t++) {
|
||||
$tcol = imagecolorallocate($i, 100 + $this->rand(0, 150), 100 + $this->rand(0, 150), 100 + $this->rand(0, 150));
|
||||
$Xa = $this->rand(0, $width);
|
||||
$Ya = $this->rand(0, $height);
|
||||
$Xb = $this->rand(0, $width);
|
||||
$Yb = $this->rand(0, $height);
|
||||
imageline($i, $Xa, $Ya, $Xb, $Yb, $tcol);
|
||||
}
|
||||
|
||||
// Write CAPTCHA text
|
||||
$size = $width / strlen($phrase);
|
||||
$box = imagettfbbox($size, 0, $font, $phrase);
|
||||
$textWidth = $box[2] - $box[0];
|
||||
$textHeight = $box[1] - $box[7];
|
||||
|
||||
imagettftext($i, $size, 0, ($width - $textWidth) / 2, ($height - $textHeight) / 2 + $size, $col, $font, $phrase);
|
||||
|
||||
// Distort the image
|
||||
$X = $this->rand(0, $width);
|
||||
$Y = $this->rand(0, $height);
|
||||
$phase = $this->rand(0, 10);
|
||||
$scale = 1.3 + $this->rand(0, 10000) / 30000;
|
||||
$contents = imagecreatetruecolor($width, $height);
|
||||
|
||||
for ($x = 0; $x < $width; $x++) {
|
||||
for ($y = 0; $y < $height; $y++) {
|
||||
$Vx = $x - $X;
|
||||
$Vy = $y - $Y;
|
||||
$Vn = sqrt($Vx * $Vx + $Vy * $Vy);
|
||||
|
||||
if ($Vn != 0) {
|
||||
$Vn2 = $Vn + 4 * sin($Vn / 8);
|
||||
$nX = $X + ($Vx * $Vn2 / $Vn);
|
||||
$nY = $Y + ($Vy * $Vn2 / $Vn);
|
||||
} else {
|
||||
$nX = $X;
|
||||
$nY = $Y;
|
||||
}
|
||||
$nY = $nY + $scale * sin($phase + $nX * 0.2);
|
||||
|
||||
$p = $this->bilinearInterpolate($nX - floor($nX), $nY - floor($nY),
|
||||
$this->getCol($i, floor($nX), floor($nY)),
|
||||
$this->getCol($i, ceil($nX), floor($nY)),
|
||||
$this->getCol($i, floor($nX), ceil($nY)),
|
||||
$this->getCol($i, ceil($nX), ceil($nY)));
|
||||
|
||||
if ($p == 0) {
|
||||
$p = 0xFFFFFF;
|
||||
}
|
||||
|
||||
imagesetpixel($contents, $x, $y, $p);
|
||||
}
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFingerprint()
|
||||
{
|
||||
return $this->fingerprint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random number or the next number in the
|
||||
* fingerprint
|
||||
*/
|
||||
protected function rand($min, $max)
|
||||
{
|
||||
if (!is_array($this->fingerprint)) {
|
||||
$this->fingerprint = array();
|
||||
}
|
||||
|
||||
if ($this->useFingerprint) {
|
||||
$value = current($this->fingerprint);
|
||||
next($this->fingerprint);
|
||||
} else {
|
||||
$value = mt_rand($min, $max);
|
||||
$this->fingerprint[] = $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @param $nw
|
||||
* @param $ne
|
||||
* @param $sw
|
||||
* @param $se
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function bilinearInterpolate($x, $y, $nw, $ne, $sw, $se)
|
||||
{
|
||||
list($r0, $g0, $b0) = $this->getRGB($nw);
|
||||
list($r1, $g1, $b1) = $this->getRGB($ne);
|
||||
list($r2, $g2, $b2) = $this->getRGB($sw);
|
||||
list($r3, $g3, $b3) = $this->getRGB($se);
|
||||
|
||||
$cx = 1.0 - $x;
|
||||
$cy = 1.0 - $y;
|
||||
|
||||
$m0 = $cx * $r0 + $x * $r1;
|
||||
$m1 = $cx * $r2 + $x * $r3;
|
||||
$r = (int)($cy * $m0 + $y * $m1);
|
||||
|
||||
$m0 = $cx * $g0 + $x * $g1;
|
||||
$m1 = $cx * $g2 + $x * $g3;
|
||||
$g = (int)($cy * $m0 + $y * $m1);
|
||||
|
||||
$m0 = $cx * $b0 + $x * $b1;
|
||||
$m1 = $cx * $b2 + $x * $b3;
|
||||
$b = (int)($cy * $m0 + $y * $m1);
|
||||
|
||||
return ($r << 16) | ($g << 8) | $b;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $image
|
||||
* @param $x
|
||||
* @param $y
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getCol($image, $x, $y)
|
||||
{
|
||||
$L = imagesx($image);
|
||||
$H = imagesy($image);
|
||||
if ($x < 0 || $x >= $L || $y < 0 || $y >= $H) {
|
||||
return 0xFFFFFF;
|
||||
}
|
||||
|
||||
return imagecolorat($image, $x, $y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $col
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getRGB($col)
|
||||
{
|
||||
return array(
|
||||
(int)($col >> 16) & 0xff,
|
||||
(int)($col >> 8) & 0xff,
|
||||
(int)($col) & 0xff,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,9 @@ use Symfony\Component\Finder\Finder;
|
|||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
|
||||
use Gregwar\Captcha\CaptchaBuilderInterface;
|
||||
use Gregwar\Captcha\PhraseBuilderInterface;
|
||||
|
||||
/**
|
||||
* Uses configuration parameters to call the services that generate captcha images
|
||||
*
|
||||
|
@ -48,11 +51,11 @@ class CaptchaGenerator
|
|||
/**
|
||||
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
|
||||
* @param \Symfony\Component\Routing\RouterInterface $router
|
||||
* @param CaptchaBuilder $builder
|
||||
* @param ImageFileHandler $imageFileHandler
|
||||
* @param CaptchaBuilderInterface $builder
|
||||
* @param ImageFileHandlerInterface $imageFileHandler
|
||||
* @param string $whitelistKey
|
||||
*/
|
||||
public function __construct(SessionInterface $session, RouterInterface $router, CaptchaBuilder $builder, PhraseBuilder $phraseBuilder, ImageFileHandler $imageFileHandler, $whitelistKey)
|
||||
public function __construct(SessionInterface $session, RouterInterface $router, CaptchaBuilderInterface $builder, PhraseBuilderInterface $phraseBuilder, ImageFileHandler $imageFileHandler, $whitelistKey)
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->router = $router;
|
||||
|
@ -106,9 +109,8 @@ class CaptchaGenerator
|
|||
$options['width'],
|
||||
$options['height'],
|
||||
$options['font'],
|
||||
$this->getPhrase($key, $options),
|
||||
$fingerprint
|
||||
);
|
||||
)->getGd();
|
||||
|
||||
if ($options['keep_value']) {
|
||||
$this->session->set($key . '_fingerprint', $this->builder->getFingerprint());
|
||||
|
@ -139,6 +141,7 @@ class CaptchaGenerator
|
|||
|
||||
$phrase = $this->phraseBuilder->build($options['length'], $options['charset']);
|
||||
$this->session->set($key, $phrase);
|
||||
$this->captchaBuilder->setPhrase($phrase);
|
||||
|
||||
return $phrase;
|
||||
}
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\CaptchaBundle\Generator;
|
||||
|
||||
/**
|
||||
* Generates random phrase
|
||||
*
|
||||
* @author Gregwar <g.passault@gmail.com>
|
||||
*/
|
||||
class PhraseBuilder
|
||||
{
|
||||
public function build($length = 5, $charset = 'abcdefghijklmnopqrstuvwxyz0123456789')
|
||||
{
|
||||
$phrase = '';
|
||||
$chars = str_split($charset);
|
||||
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$phrase .= $chars[array_rand($chars)];
|
||||
}
|
||||
|
||||
return $phrase;
|
||||
}
|
||||
}
|
|
@ -27,7 +27,7 @@ services:
|
|||
- %gregwar_captcha.config.expiration%
|
||||
|
||||
gregwar_captcha.captcha_builder:
|
||||
class: Gregwar\CaptchaBundle\Generator\CaptchaBuilder
|
||||
class: Gregwar\Captcha\CaptchaBuilder
|
||||
|
||||
gregwar_captcha.phrase_builder:
|
||||
class: Gregwar\CaptchaBundle\Generator\PhraseBuilder
|
||||
class: Gregwar\Captcha\PhraseBuilder
|
||||
|
|
|
@ -17,7 +17,8 @@
|
|||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
"php": ">=5.3.0",
|
||||
"gregwar/captcha": "dev-master"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
|
|
Loading…
Reference in New Issue