[Configuration] Adding font (fixes #9)

This commit is contained in:
Gregwar 2011-12-02 19:07:28 +01:00
parent 124f3f8ece
commit 726f21e2ad
4 changed files with 24 additions and 5 deletions

View File

@ -22,6 +22,7 @@ class Configuration implements ConfigurationInterface
->scalarNode('length')->defaultValue(5)->end() ->scalarNode('length')->defaultValue(5)->end()
->scalarNode('width')->defaultValue(120)->end() ->scalarNode('width')->defaultValue(120)->end()
->scalarNode('height')->defaultValue(40)->end() ->scalarNode('height')->defaultValue(40)->end()
->scalarNode('font')->defaultValue(__DIR__.'/../Generator/Font/captcha.ttf')->end()
->scalarNode('keep_value')->defaultValue(true)->end() ->scalarNode('keep_value')->defaultValue(true)->end()
->scalarNode('charset')->defaultValue('abcdefhjkmnprstuvwxyz23456789')->end() ->scalarNode('charset')->defaultValue('abcdefhjkmnprstuvwxyz23456789')->end()
->scalarNode('as_file')->defaultValue(false)->end() ->scalarNode('as_file')->defaultValue(false)->end()

View File

@ -27,6 +27,12 @@ class CaptchaGenerator {
*/ */
public $gcFreq; public $gcFreq;
/**
* Captcha Font
* @var string
*/
public $font;
/** /**
* Maximum age of images in minutes * Maximum age of images in minutes
* @var int * @var int
@ -39,15 +45,19 @@ class CaptchaGenerator {
*/ */
public $value; public $value;
public function __construct($value, $imageFolder, $webPath, $gcFreq, $expiration) public function __construct($value, $imageFolder, $webPath, $gcFreq, $expiration, $font)
{ {
$this->value = $value; $this->value = $value;
$this->imageFolder = $imageFolder; $this->imageFolder = $imageFolder;
$this->webPath = $webPath; $this->webPath = $webPath;
$this->gcFreq = intval($gcFreq); $this->gcFreq = intval($gcFreq);
$this->expiration = intval($expiration); $this->expiration = intval($expiration);
$this->font = $font;
} }
/**
* Get the captcha embeded code
*/
public function getCode($width = 120, $height = 40) public function getCode($width = 120, $height = 40)
{ {
return 'data:image/jpeg;base64,'.base64_encode($this->generate($width, $height)); return 'data:image/jpeg;base64,'.base64_encode($this->generate($width, $height));
@ -112,7 +122,7 @@ class CaptchaGenerator {
// Write CAPTCHA text // Write CAPTCHA text
$size = $width/strlen($this->value); $size = $width/strlen($this->value);
$font = __DIR__.'/Font/captcha.ttf'; $font = $this->font;
$box = imagettfbbox($size, 0, $font, $this->value); $box = imagettfbbox($size, 0, $font, $this->value);
$txt_width = $box[2] - $box[0]; $txt_width = $box[2] - $box[0];
$txt_height = $box[1] - $box[7]; $txt_height = $box[1] - $box[7];

View File

@ -101,6 +101,7 @@ You can define the following type option :
* **height**: the height of the captcha image (default=40) * **height**: the height of the captcha image (default=40)
* **length**: the length of the captcha (number of chars, default 5) * **length**: the length of the captcha (number of chars, default 5)
* **charset**: the charset used for code generation (default=abcdefhjkmnprstuvwxyz23456789) * **charset**: the charset used for code generation (default=abcdefhjkmnprstuvwxyz23456789)
* **font**: the font to use (default=Generator/Font/captcha.ttf)
* **keep_value**: the value will be the same until the form is posted, even if the page is refreshed (default=true) * **keep_value**: the value will be the same until the form is posted, even if the page is refreshed (default=true)
* **as_file**: if set to true an image file will be created instead of embedding to please IE6/7 (default=false) * **as_file**: if set to true an image file will be created instead of embedding to please IE6/7 (default=false)
* **image_folder**: name of folder for captcha images relative to public web folder in case **as_file** ist set to true (default="captcha") * **image_folder**: name of folder for captcha images relative to public web folder in case **as_file** ist set to true (default="captcha")

View File

@ -74,13 +74,19 @@ class CaptchaType extends AbstractType
* Frequence of garbage collection in fractions of 1 * Frequence of garbage collection in fractions of 1
* @var int * @var int
*/ */
public $gcFreq; protected $gcFreq;
/**
* Captcha font
* @var string
*/
protected $font;
/** /**
* Maximum age of images in minutes * Maximum age of images in minutes
* @var int * @var int
*/ */
public $expiration; protected $expiration;
/** /**
* The session * The session
@ -109,6 +115,7 @@ class CaptchaType extends AbstractType
$this->webPath = $config['web_path']; $this->webPath = $config['web_path'];
$this->gcFreq = $config['gc_freq']; $this->gcFreq = $config['gc_freq'];
$this->expiration = $config['expiration']; $this->expiration = $config['expiration'];
$this->font = $config['font'];
} }
public function buildForm(FormBuilder $builder, array $options) public function buildForm(FormBuilder $builder, array $options)
@ -121,7 +128,7 @@ class CaptchaType extends AbstractType
public function buildView(FormView $view, FormInterface $form) public function buildView(FormView $view, FormInterface $form)
{ {
$generator = new CaptchaGenerator($this->generateCaptchaValue(), $this->imageFolder, $this->webPath, $this->gcFreq, $this->expiration); $generator = new CaptchaGenerator($this->generateCaptchaValue(), $this->imageFolder, $this->webPath, $this->gcFreq, $this->expiration, $this->font);
if ($this->asFile) { if ($this->asFile) {
$view->set('captcha_code', $generator->getFile($this->width, $this->height)); $view->set('captcha_code', $generator->getFile($this->width, $this->height));