PHP TesseractOCR exec command issue - php

I have installed TesseractOCR from terminal of mac. when i run the following command from terminal it is working.
tesseract "hello.png" /Applications/MAMP/tmp/php/987051047
but the same command is not working in
exec("tesseract "hello.png" /Applications/MAMP/tmp/php/987051047")
and the full code is
$tesseract = new TesseractOCR("hello.png");
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
$tesseract->setTempDir( $tmp_dir );
$test = $tesseract->recognize();
I feel I have to load tesseract in php.ini or any other configuration file. but I don't know where. I am using mac, MAMP, php 5.4.10

Ok after installation we need to change the path for bin
$path = getenv('PATH');
putenv("PATH=$path:/usr/local/bin");
add these lines and it will work
Thanks

Related

php Tesseract Error! The command "tesseract" was not found

I'm trying and failing to use tesseract php. I get this error:
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:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Peppe\AppData\Local\Microsoft\WindowsApps;C:\Users\Peppe\AppData\Roaming\Composer\vendor\bin in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0
C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\htdocs\index.php(7): thiagoalessio\TesseractOCR\TesseractOCR->run() #2 {main} thrown in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
I'm using windows 10 with xampp installed in C:\xampp. php version 7.4
I installed tesseract.exe from https://github.com/UB-Mannheim/tesseract/wiki both x32 and x64
I used composer to install the https://github.com/thiagoalessio/tesseract-ocr-for-php and it gave no error.
<?php
require_once 'C:\Users\Peppe\vendor\autoload.php';
use thiagoalessio\TesseractOCR\TesseractOCR;
$ocr = new TesseractOCR("caption.jpg");
$content = $ocr->run();
echo $content;
?>
finally, the caption.jpg is in the htdocs folder, the main folder and same folder as index.php where the aforementioned code appears.
any solutions?
I found the answer:
the $PATH variable is taken from Windows. To set it right search for "system environment variables", click "environment variables", edit PATH and add the tesseract software folder, for example C:\Program Files\Tesseract-OCR
In my case I have to add new variable tesseract with full path C:\Program Files\Tesseract-OCR\tesseract.exe as showing in below screenshot
To install TESSERACT OCR and use it in PHP
You must perform the following steps:
(in my explanation, I assume you are on windows)
url src = https://tesseract-ocr.github.io/tessdoc/Installation.html
1- Download the TESSERACT software from URL:
https://github.com/UB-Mannheim/tesseract/wiki
2- From your composer, you must install:
url: https://packagist.org/packages/thiagoalessio/tesseract_ocr
composer require thiagoalessio/tesseract_ocr
3- Then, it is important to do things in the following order:
Your TESSERACT software (step 1) is installed in a directory
which will certainly be (it's up to you to check in your system)
folder = C:\Program Files\Tesseract-OCR
It is absolutely necessary to add this folder in the windows environment variables at the PATH level (user and system)
4- Then you restart your web environment (WAMP, LARAGON ...)
5- Verification that TESSERACT is operational.
Go to your CMD and write:
tesseract --version
Reply somethink like that :
tesseract v5.3.0.20221222
leptonica-1.78.0
libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0
Found AVX2
Found AVX
Found FMA
Found SSE4.1
Found libarchive 3.5.0 zlib/1.2.11 liblzma/5.2.3 bz2lib/1.0.6 liblz4/1.7.5 libzstd/1.4.5
Found libcurl/7.77.0-DEV Schannel zlib/1.2.11 zstd/1.4.5 libidn2/2.0.4 nghttp2/1.31.0
6- Operation test from a PHP script
uses an image on a white background with text
<?php
$app_class = 'thiagoalessio\TesseractOCR\TesseractOCR';
$file = 'C:/Users/admin/Desktop/im2.jpg';
$class = new ReflectionClass($app_class);
$infoImg = $class->newInstanceArgs();
$infoImg->lang('eng','jpn','spa','fr');
$infoImg->image($file);
$infoImg->withoutTempFiles();
$resultat = $infoImg->run();
var_dump($resultat);
?>

How to generate .phar in netbeans

I have a problem of building the php appliction.
Target 'build' does not exist in this project
in netbeans php project.
Just send what are all the prosedures to build a phar along with composer in netbeans IDE using ubuntu os
Netbeans has plugins, you can install NBPhar, which will help you to create .phar file.
You can create .phar file by following below steps :
Create a new PHP file named create-phar.php in your myapp root with the following code:
<?php
$srcRoot = "~/myapp/src";
$buildRoot = "~/myapp/build";
$phar = new Phar($buildRoot . "/myapp.phar",
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, "myapp.phar");
$phar["index.php"] = file_get_contents($srcRoot . "/index.php");
$phar["common.php"] = file_get_contents($srcRoot . "/common.php");
$phar->setStub($phar->createDefaultStub("index.php"));
copy($srcRoot . "/config.ini", $buildRoot . "/config.ini");
Then open a terminal window, navigate to the myapp directory and run it:
$ php create-phar.php
Refer this article Packaging Your Apps with Phar for detailed explanation.

Run WP-CLI using PHP

I've installed WP-CLI on the Mac and my next step is to execute WP-CLI commands using PHP script.
I've tried to implement it the following way but I do not see anything happening. Can someone please look at my code and tell me that what I'm doing wrong?
define( 'WP_CLI_ROOT', '/usr/local/bin/wp' );
include WP_CLI_ROOT . '';
$output = shell_exec("wp --info");
echo "<pre>".$output."</pre>";
Do I need to configure and setup wp-cli with my PHP files?
Also, when I type wp --info on my terminal the following information comes up. Nothing is appearing beside the Package Dir & global config. do I also need to make adjustments to wp-cli?
MAC-00343:htdocs mike$ wp --info
PHP binary: /usr/bin/php
PHP version: 5.6.30
php.ini used:
WP-CLI root dir: phar://wp-cli.phar
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /Users/mike/Docker/xamp/www/wordpress_wwws/htdocs
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.3.0
Any help or suggestions will be much appreciated.
Thanks
The problem is your wp cli aren't in the path environment variable, you should add your wp cli root to the path environment as following, hopefully this is helpful. For reference all wp cli command, visit the url - https://wpcommands.com
<?php
$saved = getenv("path"); // get the value of PATH environment variable and save current value
$newld = "/usr/local/bin"; // extra paths to add - in this case, it should be your wp cli root
if ($saved)
{
$newld .= ":$saved";
}
// append old paths if any
putenv("path=$newld"); // set new value
// exec wp cli command
$output = shell_exec("wp --info");
echo "<pre>".$output."</pre>";
?>
Here is what wp --info returns on my terminal.
the only main difference I see is the php.isi used: field.
Sorry I cant help more, but I dont know what DOCKER is, and I am using MAMP, not XAMP
phar path is just the current directory I'm working in

why wkhtmltopdf doesn't work with php

I ran the codes:
error_reporting(E_ALL);
ini_set( 'display_errors','1');
$d = 'usr/bin/wkhtmltopdf /www/11_07_13/task1/ml.html hd.pdf';
//also used: $d = 'xvfb-run -a wkhtmltopdf ml.html hd.pdf';
//$d = 'xvfb-run -a usr/bin/wkhtmltopdf ml.html hd.pdf';
if (exec($d)) {
echo "success";
}
else{
echo "error";
}
and everytime it shows error.
At first I installed wkhtmltopdf via terminal, dinn't work. Then I downloaded the file: wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2' from the code.google... site, unzipped and placed it in my www/test folder to use with php; with no luck.
but the command 'wkhtmltopdf test.html test.pdf' from terminal woks just fine.
Then where is the problem???
Can anyone please help me out???
p.s. I use ubuntu 12.04. php 5.4.7
The error message from exec call shows: Array ( [0] => /usr/bin/wkhtmltopdf: /opt/lampp/lib/libgcc_s.so.1: version `GCC_4.2.0' not found (required by /usr/lib/i386-linux-gnu/libstdc++.so.6) )

shell_exec() not executing pdftotext command

I installed the required library and its working in terminal but not in my php file.
My code is :
$mypdf = shell_exec('/usr/local/bin/pdftotext test.pdf test.txt');
echo $mypdf;
If I execute this command /usr/local/bin/pdftotext test.pdf test.txt in terminal it works fine.
I also write my code this type :
shell_exec('/usr/local/bin/pdftotext test.pdf test.txt');
$mypdf = file_get_contents("test.txt");
echo $mypdf;
In Error Log the message is :
/usr/local/bin/pdftotext: /opt/lampp/lib/libgcc_s.so.1: version `GCC_4.2.0' not found (required by /usr/lib/i386-linux-gnu/libstdc++.so.6)
My guess would be that you're expecting $mypdf to contain the PDF document instead of the return value of the executed command. If you're trying to print out 'test.txt', then you'll need to load it up and print it.
Issue was: /usr/local/bin/pdftotext: /opt/lampp/lib/libgcc_s.so.1: version 'GCC_4.2.0' not found (required by /usr/lib/i386-linux-gnu/libstdc++.so.6)
And the solution was to rename /opt/lampp/lib/libgcc_s.so.1 to libgcc_s.so.1.bak and solves my problem. But confusion is that what was the issue in it :P
I think you have some trouble with a dependency. Check your error.log. Find out which file is missing and add it to the path environment variable of your php installation.

Categories