README.md (3339B)
1 Captcha 2 ======= 3 4  5 6 Installation 7 ============ 8 9 With composer : 10 11 ``` json 12 { 13 ... 14 "require": { 15 "gregwar/captcha": "1.*" 16 } 17 } 18 ``` 19 20 Usage 21 ===== 22 23 You can create a captcha with the `CaptchaBuilder` : 24 25 ```php 26 <?php 27 28 use Gregwar\Captcha\CaptchaBuilder; 29 30 $builder = new CaptchaBuilder; 31 $builder->build(); 32 ``` 33 34 You can then save it to a file : 35 36 ```php 37 <?php 38 39 $builder->save('out.jpg'); 40 ``` 41 42 Or output it directly : 43 44 ```php 45 <?php 46 47 header('Content-type: image/jpeg'); 48 $builder->output(); 49 ``` 50 51 Or inline it directly in the HTML page: 52 53 ```php 54 <img src="<?php echo $builder->inline(); ?>" /> 55 ``` 56 57 You'll be able to get the code and compare it with a user input : 58 59 ```php 60 <?php 61 62 // Example: storing the phrase in the session to test for the user 63 // input later 64 $_SESSION['phrase'] = $builder->getPhrase(); 65 ``` 66 67 You can compare the phrase with user input: 68 ```php 69 if($builder->testPhrase($userInput)) { 70 // instructions if user phrase is good 71 } 72 else { 73 // user phrase is wrong 74 } 75 ``` 76 77 API 78 === 79 80 You can use theses functions : 81 82 * **__construct($phrase = null)**, constructs the builder with the given phrase, if the phrase is null, a random one will be generated 83 * **getPhrase()**, allow you to get the phrase contents 84 * **setDistortion($distortion)**, enable or disable the distortion, call it before `build()` 85 * **isOCRReadable()**, returns `true` if the OCR can be read using the `ocrad` software, you'll need to have shell_exec enabled, imagemagick and ocrad installed 86 * **buildAgainstOCR($width = 150, $height = 40, $font = null)**, builds a code until it is not readable by `ocrad` 87 * **build($width = 150, $height = 40, $font = null)**, builds a code with the given $width, $height and $font. By default, a random font will be used from the library 88 * **save($filename, $quality = 80)**, saves the captcha into a jpeg in the $filename, with the given quality 89 * **get($quality = 80)**, returns the jpeg data 90 * **output($quality = 80)**, directly outputs the jpeg code to a browser 91 * **setBackgroundColor($r, $g, $b)**, sets the background color to force it (this will disable many effects and is not recommended) 92 * **setBackgroundImages(array($imagepath1, $imagePath2))**, Sets custom background images to be used as captcha background. It is recommended to disable image effects when passing custom images for background (ignore_all_effects). A random image is selected from the list passed, the full paths to the image files must be passed. 93 * **setInterpolation($interpolate)**, enable or disable the interpolation (enabled by default), disabling it will be quicker but the images will look uglier 94 * **setIgnoreAllEffects($ignoreAllEffects)**, disable all effects on the captcha image. Recommended to use when passing custom background images for the captcha. 95 * **testPhrase($phrase)**, returns true if the given phrase is good 96 * **setMaxBehindLines($lines)**, sets the maximum number of lines behind the code 97 * **setMaxFrontLines($lines)**, sets the maximum number of lines on the front of the code 98 99 Symfony 2 Bundle 100 ================ 101 102 You can have a look at the following repository to enjoy the Symfony 2 bundle packaging this captcha generator : 103 https://github.com/Gregwar/CaptchaBundle 104 105 License 106 ======= 107 108 This library is under MIT license, have a look to the `LICENSE` file