funnydevjsc/image-captcha-solver-laravel

Laravel package for solving image captcha

1.0.1 2025-03-13 08:18 UTC

This package is auto-updated.

Last update: 2025-03-13 15:05:09 UTC


README

The free Laravel package to help you convert image captcha to text by OCR with Tesseract

Use Cases

  • Solve image text captcha by OCR from images with Tesseract

Features

  • Easy and free to solve image text captcha with a simple line code

Know issues

  • Please consider your server's environment before using this package. It doesn't have to be a physical server or support GPUs but at least you have to have the ability to install applications and run commands.

Requirements

  • PHP: 8.1 or higher
  • Laravel 9.0 or higher
  • Server with permissions to install applications and execute commands (like APT or YUM/DNF)

Quick Start

If you prefer to install this package into your own Laravel application, please follow the installation steps below

Installation

Step 1. Install a Laravel project if you don't have one already

https://laravel.com/docs/installation

Step 2. Install dependencies (If you use an operating system other than Ubuntu, please edit it accordingly):

apt install -y tesseract-ocr libtesseract-dev tesseract-ocr-all libmemcached-tools

Step 3. Require the current package using composer:

composer require funnydevjsc/image-captcha-solver-laravel

Testing

use Imagick;
use FunnyDev\ImageCaptchaSolver\ImageCaptchaSolverSdk;

class TestCaptchaSolver
{
    /**
     * Handle the event.
     * @throws \Exception
     */
    public function handle(): void
    {
        $instance = new ImageCaptchaSolverSdk();
        $image = 'base64-image-captcha-content-here';
        
        /* Set image data and sanity to make it more clear */
        $instance->setImage(base64image: $image);
        $instance->sanityImage();
        
        /* Convert image to black and white, remove unnecessary pixels. Be careful when customizing the `fuzz` value to ensure that you don't lose the stroke of the necessary character. */
        $instance->changeColorImage(fuzz: 33.0);
        
        /* Resize the image to make it easier to recognize and remove pixels inserted into the characters that need to be recognized that were not completely processed in the previous step. */
        $instance->resizeImage(width: 165, height: 50, filter: Imagick::FILTER_LANCZOS, blur: 1, best_fit: false);
        
        /*
        *   Regarding how to train the recognition library, it can be simplified as follows instead of interfering with tesseract:
        *   1. You can run $instance->ocr(...args) with limit 0 to return all the results that this library finds.
        *   2. With the character length of the found results, depending on the frequency, we will separate the characters that need to be replaced and put them into the corresponding table. For example, if the captcha is ABCDEF but the recognition is ABCDDDF, we have the replacement phrase ['DDD' => 'DE'] to add to $instance->moreCharactersDictionary. Similarly with $instance->lessCharactersDictionary and $instance->equalCharactersDictionary.
        *   3. Replace $instance->moreCharactersDictionary = your dictionary array before executing $instance->ocr(...args) for the next time.
        */
        
        /* Returns a set of recorded results, where the `limit` value is the number of characters in the captcha to be recognized. */
        $results = $instance->ocr(limit: 6, language: 'eng', dictionary: 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm123456789');
    }
}

Feedback

Respect us in the Laravel Việt Nam

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email contact@funnydev.vn or use the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.