Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Tesseract Bridge Laravel Package

bicycle/tesseract-bridge

PHP wrapper for Tesseract OCR with CLI and FFI backends. Configure the Tesseract binary, list available languages, and recognize text from images via a simple API. Tested on FreeBSD/Debian/Ubuntu with Tesseract 3/4.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require bicycle/tesseract-bridge
    

    Ensure your server has Tesseract OCR installed (tested on FreeBSD, Debian, Ubuntu). Verify with:

    tesseract --version
    
  2. First Use Case: Quickly extract text from an image (e.g., eurotext.png):

    use Bicycle\Tesseract\Bridge as TesseractBridge;
    
    $config = TesseractBridge\Configuration(['binary_path' => '/usr/bin/tesseract']);
    $bridge = new TesseractBridge\CLI($config); // or FFI for direct binding
    $text = $bridge->recognizeFromFile(storage_path('images/eurotext.png'));
    
  3. Where to Look First:

    • Configuration class: Customize paths, language packs, or CLI/FFI behavior.
    • CLI vs FFI: Choose based on performance needs (FFI is faster but platform-dependent).
    • recognizeFromFile(): Core method for OCR. Supports optional language hints (e.g., ['eng', 'fra']).

Implementation Patterns

Workflows

  1. Image Processing Pipeline:

    // Upload → Process → Store OCR result
    $imagePath = $request->file('image')->store('temp');
    $text = $tesseract->recognizeFromFile($imagePath);
    $this->ocrResults->store($text, $imagePath);
    
  2. Language-Specific OCR:

    $bridge->recognizeFromFile($path, ['jpn']); // Force Japanese
    
  3. Batch Processing:

    foreach (glob(storage_path('images/*.png')) as $file) {
        $text = $tesseract->recognizeFromFile($file);
        // Process $text...
    }
    

Integration Tips

  • Queue Jobs: Offload heavy OCR tasks to Laravel queues:
    dispatch(new ProcessImageWithOCR($imagePath));
    
  • Service Provider: Bind the bridge to Laravel’s container:
    $this->app->singleton(TesseractBridge::class, function ($app) {
        return new TesseractBridge\CLI(new Configuration(['binary_path' => config('tesseract.path')]));
    });
    
  • Validation: Sanitize OCR output (e.g., strip non-text artifacts):
    preg_replace('/[^a-zA-Z0-9\s]/', '', $text);
    

Gotchas and Tips

Pitfalls

  1. Platform Dependency:

    • FFI requires Tesseract compiled with --enable-shared and PHP-FFI enabled.
    • CLI is more portable but slower (spawns subprocesses).
  2. Language Packs:

    • Missing languages? Install them via system package manager (e.g., apt-get install tesseract-ocr-[lang]).
    • Verify available languages with getAvailableLanguages().
  3. Memory Limits:

    • Large images may hit PHP’s memory_limit. Increase if needed:
    ini_set('memory_limit', '512M');
    
  4. File Permissions:

    • Ensure Tesseract binary is executable (chmod +x /usr/bin/tesseract).

Debugging

  • Version Mismatch: If testGetVersion() fails, confirm Tesseract 3/4 is installed and binary_path is correct.
  • Empty Output: Check image quality (Tesseract struggles with low-res/blurry images). Preprocess with:
    use Intervention\Image\Facades\Image;
    $image = Image::make($path)->resize(1000, null)->save();
    
  • FFI Errors: Debug with FFI::load():
    $ffi = FFI::load('tesseract');
    if (!$ffi) throw new \RuntimeException('FFI failed to load Tesseract');
    

Extension Points

  1. Custom Configuration: Extend Configuration to add options like OCR engine parameters:
    class CustomConfig extends TesseractBridge\Configuration {
        public function __construct(array $options = []) {
            parent::__construct(array_merge($options, ['psm' => 6])); // Page segmentation mode
        }
    }
    
  2. Post-Processing: Chain OCR results with Laravel events:
    event(new OCRProcessed($text, $imagePath));
    
  3. Fallback Logic: Combine CLI/FFI for resilience:
    try {
        return $ffiBridge->recognizeFromFile($path);
    } catch (\Exception $e) {
        return $cliBridge->recognizeFromFile($path);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor