场景
最近项目上需要开发图片文字识别功能,接入了开源的tesseract_ocr,根据官网介绍,使用composer require thiagoalessio/tesseract_ocr
命令安装依赖,但是在执行之后,删除了一些文件,导致项目报错。
解决方案
手动调整自动加载配置。
实现步骤
- 在项目根目录
composer.json
文件中的require
对象中添加
"thiagoalessio/tesseract_ocr": "^2.12"
- 在
composer.lock
文件中的packages
对象中添加
{
"name": "thiagoalessio/tesseract_ocr",
"version": "2.12.0",
"source": {
"type": "git",
"url": "https://github.com/thiagoalessio/tesseract-ocr-for-php.git",
"reference": "0f10bd7b02bdcba59c4fbd98fbd93a56f93b09b7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thiagoalessio/tesseract-ocr-for-php/zipball/0f10bd7b02bdcba59c4fbd98fbd93a56f93b09b7",
"reference": "0f10bd7b02bdcba59c4fbd98fbd93a56f93b09b7",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": "^5.3 || ^7.0 || ^8.0"
},
"require-dev": {
"phpunit/php-code-coverage": "^2.2.4 || ^9.0.0"
},
"type": "library",
"autoload": {
"psr-4": {
"thiagoalessio\\TesseractOCR\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "thiagoalessio",
"email": "thiagoalessio@me.com"
}
],
"description": "A wrapper to work with Tesseract OCR inside PHP.",
"keywords": [
"OCR",
"Tesseract",
"text recognition"
],
"support": {
"irc": "irc://irc.freenode.net/tesseract-ocr-for-php",
"issues": "https://github.com/thiagoalessio/tesseract-ocr-for-php/issues",
"source": "https://github.com/thiagoalessio/tesseract-ocr-for-php"
},
"time": "2021-06-04T21:21:33+00:00"
}
- 将下载的依赖包放到vendor目录下,我这边的路径是
vendor\thiagoalessio\tesseract_ocr
- 在
vendor\composer\autoload_classmap.php
中添加
'thiagoalessio\\TesseractOCR\\Command' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/Command.php',
'thiagoalessio\\TesseractOCR\\FeatureNotAvailableException' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/FeatureNotAvailableException.php',
'thiagoalessio\\TesseractOCR\\FriendlyErrors' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/FriendlyErrors.php',
'thiagoalessio\\TesseractOCR\\ImageNotFoundException' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/ImageNotFoundException.php',
'thiagoalessio\\TesseractOCR\\NoWritePermissionsForOutputFile' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/NoWritePermissionsForOutputFile.php',
'thiagoalessio\\TesseractOCR\\Option' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/Option.php',
'thiagoalessio\\TesseractOCR\\Process' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/Process.php',
'thiagoalessio\\TesseractOCR\\TesseractNotFoundException' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/TesseractNotFoundException.php',
'thiagoalessio\\TesseractOCR\\TesseractOCR' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/TesseractOCR.php',
'thiagoalessio\\TesseractOCR\\TesseractOcrException' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/TesseractOcrException.php',
'thiagoalessio\\TesseractOCR\\UnsuccessfulCommandException' => $vendorDir . '/thiagoalessio/tesseract_ocr/src/UnsuccessfulCommandException.php',
- 在
vendor\composer\autoload_psr4.php
中添加
'thiagoalessio\\TesseractOCR\\' => array($vendorDir . '/thiagoalessio/tesseract_ocr/src'),
加载的命名空间为:thiagoalessio\\TesseractOCR\\
,那么将其配置指向的目录文件,$vendorDir 是vendor 目录地址。
- 在
vendor\composer\autoload_static.php
中的$prefixLengthsPsr4
数组中添加
't' =>
array (
'thiagoalessio\\TesseractOCR\\' => 27,
),
将上一步手动添加的依赖包命名空间首字母,如thiagoalessio\\TesseractOCR\\
首字母为t,放到 t 数组内如果没有,就造出来一个字母变量,包含在里面即可,命名空间尾部为:”// “结尾,写法:命名空间 => 权重整型,权重不能重复
- 在
vendor\composer\autoload_static.php
中的文件映射目录数组变量$prefixLengthsPsr4
数组中配置映射目录,添加
'thiagoalessio\\TesseractOCR\\' =>
array (
0 => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src',
),
写法:命名空间 => 映射目录数组。
- 在
vendor\composer\autoload_static.php
中的$classMap
数组中添加
'thiagoalessio\\TesseractOCR\\Command' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/Command.php',
'thiagoalessio\\TesseractOCR\\FeatureNotAvailableException' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/FeatureNotAvailableException.php',
'thiagoalessio\\TesseractOCR\\FriendlyErrors' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/FriendlyErrors.php',
'thiagoalessio\\TesseractOCR\\ImageNotFoundException' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/ImageNotFoundException.php',
'thiagoalessio\\TesseractOCR\\NoWritePermissionsForOutputFile' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/NoWritePermissionsForOutputFile.php',
'thiagoalessio\\TesseractOCR\\Option' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/Option.php',
'thiagoalessio\\TesseractOCR\\Process' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/Process.php',
'thiagoalessio\\TesseractOCR\\TesseractNotFoundException' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/TesseractNotFoundException.php',
'thiagoalessio\\TesseractOCR\\TesseractOCR' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/TesseractOCR.php',
'thiagoalessio\\TesseractOCR\\TesseractOcrException' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/TesseractOcrException.php',
'thiagoalessio\\TesseractOCR\\UnsuccessfulCommandException' => __DIR__ . '/..' . '/thiagoalessio/tesseract_ocr/src/UnsuccessfulCommandException.php',
- 在
vendor\composer\installed.json
中添加
{
"name": "thiagoalessio/tesseract_ocr",
"version": "2.12.0",
"version_normalized": "2.12.0.0",
"source": {
"type": "git",
"url": "https://github.com/thiagoalessio/tesseract-ocr-for-php.git",
"reference": "0f10bd7b02bdcba59c4fbd98fbd93a56f93b09b7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thiagoalessio/tesseract-ocr-for-php/zipball/0f10bd7b02bdcba59c4fbd98fbd93a56f93b09b7",
"reference": "0f10bd7b02bdcba59c4fbd98fbd93a56f93b09b7",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": "^5.3 || ^7.0 || ^8.0"
},
"require-dev": {
"phpunit/php-code-coverage": "^2.2.4 || ^9.0.0"
},
"time": "2021-06-04T21:21:33+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"thiagoalessio\\TesseractOCR\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "thiagoalessio",
"email": "thiagoalessio@me.com"
}
],
"description": "A wrapper to work with Tesseract OCR inside PHP.",
"keywords": [
"OCR",
"Tesseract",
"text recognition"
],
"support": {
"irc": "irc://irc.freenode.net/tesseract-ocr-for-php",
"issues": "https://github.com/thiagoalessio/tesseract-ocr-for-php/issues",
"source": "https://github.com/thiagoalessio/tesseract-ocr-for-php"
},
"install-path": "../thiagoalessio/tesseract_ocr"
},
修改完以上就可以加载了,在项目中使用 use thiagoalessio\TesseractOCR\TesseractOCR;
即可。