Skip to content

Instantly share code, notes, and snippets.

@MilesCranmer
Created December 18, 2022 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MilesCranmer/c5c7bbbdc61fa59b984bf22a8aaa7c89 to your computer and use it in GitHub Desktop.
Save MilesCranmer/c5c7bbbdc61fa59b984bf22a8aaa7c89 to your computer and use it in GitHub Desktop.
Generate captcha text to hide text from bots
<?php
require_once 'vendor/autoload.php';
use Gregwar\Captcha\CaptchaBuilder;
// Create a captcha with the text "MilesCranmer@mastodon.social"
$builder = new CaptchaBuilder('MilesCranmer@mastodon.social');
$builder->setMaxBehindLines(10);
$builder->setMaxFrontLines(10);
// Change the "font" to wherever your desired font file is
$builder->build($width=2000, $height=800, $font='/Users/mcranmer/Library/Fonts/Roboto-Regular.ttf');
$builder->save('out.jpg', $quality=100);
@MilesCranmer
Copy link
Author

Instructions:

  1. Install php and composer (e.g., with brew: brew install php composer).
  2. Create a folder and open a terminal in that folder. Run composer require gregwar/captcha to install gregwar's captcha library.
  3. Tweak the text (above, 'MilesCranmer@mastodon.social') to your desired text.
  4. Tweak the font with the location of your ttf file (on macOS, these are in your Library/Fonts folder).
  5. Run php create_captcha_text.php in the terminal.

The image is out.jpg.

@MilesCranmer
Copy link
Author

In case it's still legible to OCR, you can increase the number of lines by changing the 10s. You can also change the font.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment