使用 PHP 与 Tesseract 实现图像验证码识别

一、环境准备
安装 Tesseract OCR

Ubuntu / Debian

sudo apt install tesseract-ocr
更多内容访问ttocr.com或联系1436423940

macOS

brew install tesseract
安装 PHP(如果尚未安装)

sudo apt install php
二、项目结构示例

project/
├── captcha1.png
└── recognize.php
三、编写识别脚本
创建文件 recognize.php:

\n"; exit(1); } $image = $argv[1]; if (!file_exists($image)) { echo "文件不存在: $image\n"; exit(1); } $result = recognizeCaptcha($image); echo "识别结果: $result\n"; 四、运行程序 假设有图像 captcha1.png,在终端中执行: php recognize.php captcha1.png 输出示例: 识别结果: 9XTA 五、批量识别功能(可选) 你可以扩展脚本支持识别某个目录下的所有 PNG 验证码图片: $dir = 'captchas'; foreach (scandir($dir) as $file) { if (preg_match('/\.png$/i', $file)) { $path = "$dir/$file"; $res = recognizeCaptcha($path); echo "$file -> $res\n"; } }
posted @ 2025-07-08 22:57  ttocr、com  阅读(8)  评论(0)    收藏  举报