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.
Installation:
composer require bicycle/tesseract-bridge
Ensure your server has Tesseract OCR installed (tested on FreeBSD, Debian, Ubuntu). Verify with:
tesseract --version
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'));
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']).Image Processing Pipeline:
// Upload → Process → Store OCR result
$imagePath = $request->file('image')->store('temp');
$text = $tesseract->recognizeFromFile($imagePath);
$this->ocrResults->store($text, $imagePath);
Language-Specific OCR:
$bridge->recognizeFromFile($path, ['jpn']); // Force Japanese
Batch Processing:
foreach (glob(storage_path('images/*.png')) as $file) {
$text = $tesseract->recognizeFromFile($file);
// Process $text...
}
dispatch(new ProcessImageWithOCR($imagePath));
$this->app->singleton(TesseractBridge::class, function ($app) {
return new TesseractBridge\CLI(new Configuration(['binary_path' => config('tesseract.path')]));
});
preg_replace('/[^a-zA-Z0-9\s]/', '', $text);
Platform Dependency:
--enable-shared and PHP-FFI enabled.Language Packs:
apt-get install tesseract-ocr-[lang]).getAvailableLanguages().Memory Limits:
memory_limit. Increase if needed:ini_set('memory_limit', '512M');
File Permissions:
chmod +x /usr/bin/tesseract).testGetVersion() fails, confirm Tesseract 3/4 is installed and binary_path is correct.use Intervention\Image\Facades\Image;
$image = Image::make($path)->resize(1000, null)->save();
FFI::load():
$ffi = FFI::load('tesseract');
if (!$ffi) throw new \RuntimeException('FFI failed to load Tesseract');
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
}
}
event(new OCRProcessed($text, $imagePath));
try {
return $ffiBridge->recognizeFromFile($path);
} catch (\Exception $e) {
return $cliBridge->recognizeFromFile($path);
}
How can I help you explore Laravel packages today?