OCR wrapper for php - php

I am using this for ocr. This is wrapper for Tesseract ocr(I previosly installed Tesseract itself).
At first i dowloaded it via composer and followed examples in repo and also several posts on SO itself. And all can show is in network tab failed to load response data.
My alternate approaches is that i tried downloading repo itself, then tried to call it from my index.php which for test purposes is situated in same folder where class TesseractOCR is. I tried with images in repo and also tried with black letters on white background images with simple text.
This SO post looks promising, but i'm unsure where OP's file with example code is residing...
use thiagoalessio\TesseractOCR\TesseractOCR;
//or//require "TesseractOCR.php";//if it's in the same dir as test.php
$content = new TesseractOCR('text.png');
$text = $content->run();
echo $text;
Did i miss something obvious? Any help is appreciated.
EDIT1: I tried using in win powershell cli. By putting text.png in directory where tesseract is installed, then calling shell with administrator privileges, subsequently typing in it tesseract text.png output which creates output.txt in same directory with recognized text from that image.
So tesseract its working, my implementation with php wrapper is not.
EDIT2:
Forgot to add, page itself shows:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
Not sure why it happens.
Edit3:
My code:
try{
//use thiagoalessio\TesseractOCR\TesseractOCR;
require "./vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php";
echo $temp;//It's value is set in TesseractOCR.php
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
Value set in $temp variable is visible through state file path, so why TesseractOCRclass itself isn't?
Edit4:
Even if i put absolute path to TesseractOCR.php which holds class, in include statement, it doesn't work.
It throws this error:
Fatal error: Uncaught Error: Class 'TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
This is TesseractOCR.//echoed text from file that holds TesseractOCR class.
Inclusion path:
include ("C:/xampp/htdocs/myocr/vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php");
If i use(which is suggested in repo readme, use thiagoalessio\TesseractOCR\TesseractOCR;, then it throws:
Fatal error: Uncaught Error: Class 'thiagoalessio\TesseractOCR\TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
My question is: How it hits test message, but won't hit TesseractOCR class?
EDIT5:
If i require_once "./vendor/autoload.php"; , it throws:
Fatal error: Uncaught thiagoalessio\TesseractOCR\TesseractNotFoundException: Error! The command "tesseract" was not found. Make sure you have Tesseract OCR installed on your system: https://github.com/tesseract-ocr/tesseract The current $PATH is C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Eddie\AppData\Local\Microsoft\WindowsApps;;C:\Users\Eddie\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Eddie\AppData\Roaming\npm;C:\Users\Eddie\AppData\Roaming\Composer\vendor\bin;C:\Program Files\heroku\bin in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0 C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\ht in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
Btw, i added its patch to env variable:

I solved it!. My problem occurs that i didn't know about autoloaders in php.
Link that helped me is this.
My project structure is this:
Created project folder myocr.
After previous, downloaded latest stable version of Tesseract and installed it.
Depending on your system, you may be required to add value to your system env variable. You need to do that here
then
then
then
I'm assuming it self explanatory with images provided.
Next is getting TesseractOCR via composer:
composer require thiagoalessio/tesseract_ocr
Finally, before using code sample in repo, you need to call autoloader.
Index.php:
require_once('./vendor/autoload.php');//<-This!
use thiagoalessio\TesseractOCR\TesseractOCR;
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
This worked for me. Crucial thing to look after is directory structure.
localhost > myocr > index.php with its code, and after using composer you'll get vendor dir. and it's content. Image path in new TesseractOCR('text1.png'); is directory where both index.php and image are located.

Related

Merge PDF files with PHP into single file

If i have some PDF files, each has one page and i would like to merge all files into on single file using PHP
I have read dozens of questions here about same but most of all are out of date or no longer working since for PHP version >= 7.0
One of the solutions was to use PDFMerger and here is my code
require_once ('PDFMerger.php');
use PDFMerger\PDFMerger;
$pdf = new PDFMerger;
$pdf->addPDF('books/1.pdf');
$pdf->addPDF('books/2.pdf');
$pdf->merge('download','books/merged.pdf');
but if simply gives blank page and no file is created and same to this library PDFMerger
UPDATE1! wihtout namespace use PDFMerger\PDFMerger; iam getting this error
Fatal error: Uncaught Error: Class "PDFMerger" not found in mypath\sample.php:4 Stack trace: #0 {main} thrown in mypath\sample.php on line 4
so is there any way that is still working to merger PDF files without have to care how such file are created.
NOTE! I can not use shell_exec since files are on shared hosting
After trying a lot of time , i decide to shift back to a lower PHP version 7.1 rather than 8.0 and everything goes fine though

Unable to read the "../.dev.env" environment file in Dotenv.php:symfony

I want To run a symfony project in my computer but I get all times errors , and now I get this error and i didn't find any solution for my problem
Problem Failed to parse output as xml: Error on line 2: Content is not
allowed in prolog.. Command C:\xampp\php\php.exe
C:\xampp\htdocs\buzzaka\bin\console list --format=xml Output
Fatal error: Uncaught
Symfony\Component\Dotenv\Exception\PathException: Unable to read the
"C:\xampp\htdocs\buzzaka\bin/../.dev.env" environment file. in
C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php:466 Stack
trace:
0 C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php(51): Symfony\Component\Dotenv\Dotenv->doLoad(false, Array)
1 C:\xampp\htdocs\buzzaka\bin\console(38): Symfony\Component\Dotenv\Dotenv->load('C:\xampp\htdocs...')
2 {main} thrown in C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php on line 466
If u move your file from the folder where it was created symfony will keep looking for it on this folder, i've went on autoload_runtime and changed on $runtime variable where the project is, just worked for me, lets see on the last composer dump what just happens haha
The error is quite explicit, do you have a file C:\xampp\htdocs\buzzaka\bin/../.dev.env ?
Well, you need to create one. As announced a little while ago, Symfony will now require a .env file per environment. As you are running your command in the dev environment, you need to create a .dev.env file.

MAMP Localhost not working (Error 500) - LWT

I am trying to run LWT on my mac and have installed MAMP for this. I have followed these instructions, but when I try to go to http://localhost:8888/lwt/ I am getting:
The localhost page isn’t working
localhost is currently unable to handle this request.
500
I have created a different folder in my htdocs folder and created a php file in it (index.php), with some code to see if php is working:
<?php
echo "this is the start";
phpinfo();
echo "<br>this is the end";
?>
That is all fine and works. I have also checked the user-rights for the lwt folder, but it seems fine too (Admins & Me are Read&Write, Others are ReadOnly). Typing tail -f /Applications/MAMP/logs/php_error.log into the terminal gives me the following error log:
PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /Applications/MAMP/htdocs/lwt/utilities.inc.php:3272
Stack trace:
#0 /Applications/MAMP/htdocs/lwt/index.php(46): require_once()
#1 {main}
thrown in /Applications/MAMP/htdocs/lwt/utilities.inc.php on line 3272
I am a beginner though, so I don't know what else the problem could be. Any ideas?
I don't know which version of PHP you use. But the mysql_* function are deprecated and its possible that its not included in your new version anymore. Use mysqli_* to connect to your database or use PDO.
In the new version of MAMP is PHP7 included. Perhaps you use that.

Amazon Instant Accèss PHP SDK phar file

I am attempting to use https://github.com/amzn/amazon-instant-access-sdk-php for amazon instant access via php. I am not super familiar with phar files; but am starting to figure it out.
I included https://github.com/amzn/amazon-instant-access-sdk-php/blob/master/phar-stub.php in a file as shown below. (Modified a bit as I renamed phar file)
Phar::mapPhar('amazon-instant-access-sdk-php.phar');
require_once 'phar://amazon-instant-access-sdk-php.phar/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$classLoader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$classLoader->registerNamespaces(array(
'Amazon' => 'phar://amazon-instant-access-sdk-php.phar/src',
'Psr' => 'phar://amazon-instant-access-sdk-php.phar/vendor/psr/log',
'Monolog' => 'phar://amazon-instant-access-sdk-php.phar/vendor/monolog/monolog/src'
));
$classLoader->register();
return $classLoader;
__HALT_COMPILER();
Then when I do in another file:
require_once('amazon-instant-access-phar-stub.php');
I get error:
Fatal error: Uncaught exception 'PharException' with message 'internal corruption of phar "/Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php" (truncated manifest at stub end)' in /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php:17 Stack trace: #0 /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php(17): Phar::mapPhar('amazon-instant-...') #1 /Applications/MAMP/htdocs/phppos/amazon_link_account.php(3): require_once('/Applications/M...') #2 {main} thrown in /Applications/MAMP/htdocs/phppos/amazon-instant-access-phar-stub.php on line 17
I have tried re-downloading phar file and running different versions of php (5.3, 5.6)
I am not sure what is causing this error.
You could directly download the .phar file from the release.
If you have the .phar file, you don't need the source code from github. The source code on github is used to compile the .phar file.
In your php code, you could do something like this:
<?php
//import the phar file directly like you did
require_once('amazon-instant-access-sdk-php.phar’);
//before you initiate the class, you need to use the right name space:
use Amazon\InstantAccess\Signature as signature;
$credentialStore = new signature\CredentialStore();
?>
This one will work.
If you want to check exactly what's in the .phar file, you could extract it:
php -r '$phar = new Phar("amazon-instant-access-sdk-php.phar"); $phar->extractTo("/tmp/phar/");'
then you could browse the /tmp/phar/ for all the files in the .phar.

Marking PHAR archive as PHP executable

I'm trying to make my PHAR archive marked as PHP (I don't know correct term for this) so that I can use it from cli without php part. So instead of calling php /path/to/my/archive.phar ARGUMENTS_HERE I can just move it to /usr/local/bin and call it archive from anywhere. (Just like PHPUnit or Composer).
If I'm right, to do that, I just need to add #!/usr/bin/env php on top of my PHAR archive file?
How do I do that? I tried to mimic composer compiler in my build script, but it just crashes PHP_CLI (segmentation fault):
$archiveName = 'phpbenchmark.phar';
$phar = new Phar($archiveName);
$phar->buildFromDirectory(__DIR__, '/\/lib\/|\/vendor\//');
$phar->setStub(createStub());
function createStub()
{
$stub = <<<EOD
#!/usr/bin/env php
<?php
Phar::mapPhar('phpbenchmark.phar');
require 'phar://phpbenchmark.phar/lib/app.php';
__HALT_COMPILER();
EOD;
return $stub;
}
As you can see, I only need stub to run /lib/app.php file and that's it.
Prepending to compiled PHAR
Of course, first thing I tried was manually prepending it to my archive, but it looks like that corrupts it:
PHP Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
Stack trace:
#0 /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar(9): Phar::webPhar(NULL, 'index.php')
#1 {main}
thrown in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
PharException: manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar" in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
Call Stack:
0.0010 306272 1. {main}() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:0
0.0011 307840 2. Phar::webPhar() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
Note that exactly the same archive works before I manually modify it. (But needs php /path/to/arch of course)
Aaaand, solved (kinda) after few hours of breaking my head. I still have no idea why, this build script seems to work:
$archiveName = 'phpbenchmark.phar';
$phar = new Phar($archiveName);
$phar->buildFromDirectory(__DIR__, '/\/lib\/|\/vendor\//');
$phar->setStub(createStub());
function createStub()
{
$stub = <<<ENDSTUB
#!/usr/bin/env php
<?php
Phar::mapPhar('phpbenchmark.phar');
require 'phar://phpbenchmark.phar/lib/app.php';
__HALT_COMPILER();
ENDSTUB;
return $stub;
}
Except.....I can't really see any differences to original script posted in my question? Why would original one crash PHP Cli and this one works?
All I needed to do to get that to work is update the line:
$phar->setStub($phar->createDefaultStub("main.php"));
to :
$phar->setStub("#!/usr/bin/env php\n\n" . $phar->createDefaultStub("main.php"));
This takes the default stub that phar creates for you, and adds the requires #! line to it. Then just chmod 700 the resulting phar file, and you are good to go.

Categories