From 726f21e2ad0db58e6308080b81778e75e04419e3 Mon Sep 17 00:00:00 2001 From: Gregwar Date: Fri, 2 Dec 2011 19:07:28 +0100 Subject: [PATCH] [Configuration] Adding font (fixes #9) --- DependencyInjection/Configuration.php | 1 + Generator/CaptchaGenerator.php | 14 ++++++++++++-- README.md | 1 + Type/CaptchaType.php | 13 ++++++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 1b72967..96c325b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -22,6 +22,7 @@ class Configuration implements ConfigurationInterface ->scalarNode('length')->defaultValue(5)->end() ->scalarNode('width')->defaultValue(120)->end() ->scalarNode('height')->defaultValue(40)->end() + ->scalarNode('font')->defaultValue(__DIR__.'/../Generator/Font/captcha.ttf')->end() ->scalarNode('keep_value')->defaultValue(true)->end() ->scalarNode('charset')->defaultValue('abcdefhjkmnprstuvwxyz23456789')->end() ->scalarNode('as_file')->defaultValue(false)->end() diff --git a/Generator/CaptchaGenerator.php b/Generator/CaptchaGenerator.php index bbb91c4..617bce2 100644 --- a/Generator/CaptchaGenerator.php +++ b/Generator/CaptchaGenerator.php @@ -27,6 +27,12 @@ class CaptchaGenerator { */ public $gcFreq; + /** + * Captcha Font + * @var string + */ + public $font; + /** * Maximum age of images in minutes * @var int @@ -39,15 +45,19 @@ class CaptchaGenerator { */ public $value; - public function __construct($value, $imageFolder, $webPath, $gcFreq, $expiration) + public function __construct($value, $imageFolder, $webPath, $gcFreq, $expiration, $font) { $this->value = $value; $this->imageFolder = $imageFolder; $this->webPath = $webPath; $this->gcFreq = intval($gcFreq); $this->expiration = intval($expiration); + $this->font = $font; } + /** + * Get the captcha embeded code + */ public function getCode($width = 120, $height = 40) { return 'data:image/jpeg;base64,'.base64_encode($this->generate($width, $height)); @@ -112,7 +122,7 @@ class CaptchaGenerator { // Write CAPTCHA text $size = $width/strlen($this->value); - $font = __DIR__.'/Font/captcha.ttf'; + $font = $this->font; $box = imagettfbbox($size, 0, $font, $this->value); $txt_width = $box[2] - $box[0]; $txt_height = $box[1] - $box[7]; diff --git a/README.md b/README.md index 00cc2bd..7cabb35 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ You can define the following type option : * **height**: the height of the captcha image (default=40) * **length**: the length of the captcha (number of chars, default 5) * **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) * **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") diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index 6ff0a84..23c8cff 100755 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -74,13 +74,19 @@ class CaptchaType extends AbstractType * Frequence of garbage collection in fractions of 1 * @var int */ - public $gcFreq; + protected $gcFreq; + + /** + * Captcha font + * @var string + */ + protected $font; /** * Maximum age of images in minutes * @var int */ - public $expiration; + protected $expiration; /** * The session @@ -109,6 +115,7 @@ class CaptchaType extends AbstractType $this->webPath = $config['web_path']; $this->gcFreq = $config['gc_freq']; $this->expiration = $config['expiration']; + $this->font = $config['font']; } public function buildForm(FormBuilder $builder, array $options) @@ -121,7 +128,7 @@ class CaptchaType extends AbstractType 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) { $view->set('captcha_code', $generator->getFile($this->width, $this->height));