Bài viết này có hữu ích không?
How to solve and bypass captcha for free
Tech builder focused on infrastructure, automation, backend systems, and scalable SaaS development
This article explains how to build a free captcha solver in PHP using the Tesseract OCR engine and the tesseract_ocr wrapper.
Free text-based captcha solver in PHP using Tesseract OCR
This article explains how to build a minimal text free captcha solver in PHP using the Tesseract OCR engine and the thiagoalessio/tesseract_ocr wrapper.
1. Install dependencies
Tesseract OCR
On Linux:
bash
sudo apt update
sudo apt install tesseract-ocr -y
On Windows: download from Tesseract Wiki and add tesseract.exe to your PATH.
PHP Wrapper
bash
composer require thiagoalessio/tesseract_ocr
2. Minimal working script
php
<?php
require __DIR__ . '/vendor/autoload.php';
use thiagoalessio\TesseractOCR\TesseractOCR;
$ocr = new TesseractOCR('captcha.jpg');
$ocr->lang('eng');
$ocr->setOptions([
'tessedit_char_whitelist' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
]);
$text = $ocr->run();
$cleaned = preg_replace('/[^A-Z0-9]/', '', strtoupper($text));
echo "Recognized text: $cleaned\n";
3. Optional: Image preprocessing
For low-quality images, preprocessing may improve OCR accuracy. You can use PHP GD for basic grayscale and contrast adjustments:
php
<?php
$im = imagecreatefromjpeg('captcha.jpg');
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagefilter($im, IMG_FILTER_CONTRAST, -50);
imagejpeg($im, 'captcha_prepared.jpg');
imagedestroy($im);
Update the OCR input path to use 'captcha_prepared.jpg'.
4. Test captcha generation
You can generate a simple CAPTCHA image for testing using ImageMagick:
bash
convert -size 150x60 xc:white -font Arial -pointsize 30 -fill black -draw "text 20,40 'AB12'" captcha.jpg
5. Issues
| Error | Cause | Solution |
|---|---|---|
tesseract command not found |
Tesseract not installed or not in PATH | Install Tesseract and add it to system PATH |
| Empty OCR result | Low image quality | Apply grayscale, contrast, resize |
| File not found | Wrong or missing file path | Check if the image exists and is accessible |
6. Final Code with Preprocessing
php
<?php
require __DIR__ . '/vendor/autoload.php';
use thiagoalessio\TesseractOCR\TesseractOCR;
// Step 1: Preprocess image
$source = 'captcha.jpg';
$prepared = 'captcha_prepared.jpg';
$im = imagecreatefromjpeg($source);
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagefilter($im, IMG_FILTER_CONTRAST, -50);
imagejpeg($im, $prepared);
imagedestroy($im);
// Step 2: Run OCR
$ocr = new TesseractOCR($prepared);
$ocr->lang('eng');
$ocr->setOptions([
'tessedit_char_whitelist' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
]);
$text = $ocr->run();
$cleaned = preg_replace('/[^A-Z0-9]/', '', strtoupper($text));
echo "Recognized text: $cleaned\n";
7. Limitations
This method only works on simple image-based CAPTCHAs with clear alphanumeric text and minimal distortion. It is not suitable for modern CAPTCHA systems that use:
- Obfuscation and heavy noise
- Dynamic rendering (canvas/webGL)
- Behavioral tracking (mouse movement, clicks)
- Interactive tasks (image classification, drag and drop)
Examples include:
- Google reCAPTCHA v2/v3
- hCaptcha
- Cloudflare Turnstile
- GeeTest
To handle such CAPTCHAs, you need third-party solving services with real-time support, such as 2Captcha captcha solver. These services use machine learning and human solvers to deliver reliable tokens and support complex flows via API.
If you want a working example using 2Captcha in PHP or via browser automation, check - PHP captcha solver.
While this example uses PHP, CAPTCHA solving can be automated in many other languages using the same tools:
Supported: