How to use tesseract ocr with php from any location - php

I have installed tesseract in C:\Program Files (x86)\Tesseract-OCR. In command prompt if I am executing with tesseract 123.png sample, then it works. But I can not use tesseract with php. I have tried many libraries and codes for the same.
Someone could help me on this please?

If you needs to use Tesseract in php code, the way to do this is using the http://php.net/manual/en/function.exec.php or http://php.net/manual/en/function.shell-exec.php functions, both allow you to execute bash codes like in "command prompt", but you needs to have more attention to use that, this open several security issues because the client input data sometimes is malicious. Hope it helps!
A working examples:
exec("tesseract C:/your/path/file.png C:/output/file");
Another thing, in part C:/output/file, you don't need to put .txt in output path, tesseract always do the output as .txt file extension.
If the Tesseract is not present on "Environment Variables" the solution is to pass the full executable file path:
shell_exec('"C:/Program Files (x86)/Tesseract-OCR/tesseract.exe" C:/path/to/image C:/output/path/')

Related

Batch script to download and save XLS file to directory

I am trying to write some batch script to download and save an XLS file from a URL. I am able to down load the file by using
#ECHO OFF
start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE url link
exit
I would now like to save these files to a folder or directory.
Any help anyone could provide here would be greatly appreciated.
There are at least two ways to do it.
Like Buddy suggested, use a command-line downloader, for example wget.
Under Linux, you can run PHP directly (even without a webserver). See PHP: Command Line PHP on Microsoft Windows in PHP manual.
Don't run a browser, or - even worse - IE, just to run a PHP script.

imagemagick convert does not work through php

imagemagick convert does not work through php shell_exec but does through a shell.
In a shell convert file.pdf file.png works fine. But when I have this within a php file as shell_exec('convert file.pdf file.png'); Then I get no output! I have the permissions to do this, so I think it isn't that that's the problem; I have checked the directory I am in by way of getcwd() and this is also ok.
I know shell_exec works because I have used it earlier in the code and that works fine.
Any ideas?
I got the solution thanks to Crontab from another thread. I quote from there:
[I]f you're trying to convert a PDF to a PNG using ImageMagick ...
even using the full path to ImageMagick's convert won't work if the
script's PATH doesn't contain the path location to Ghostscript also.
Without messing with any user paths, you could add:
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
Or something similar depending on your setup. The gs executable has
to be in your script user's path somewhere or ImageMagick will fail to
convert PDF or EPS files.
Try the full path to convert, i.e. shell_exec('/usr/bin/convert file.pdf file.png);. You can use which convert to find the location on your system.
There are several reason why this could happen, but I suggest reading this page and the user comments:
http://php.net/manual/en/function.shell-exec.php

pdftk unable to save on different folder

I'm using pdftk for flattening PDF files on server. For this purpose I use PHP.
This is my code in PHP. Btw, I'm using this on WAMP.
passthru("pdftk editablepdf/jason.pdf output flattenpdf/flattened.pdf flatten");
The pdftk.exe file and the accompanying .dll file both exist in the www directory.
editablepdf and flattenpdf are two separate directories in the www directory.
After executing the script and checking, i find the pdfs haven't been saved.
When i tried running pdftk via command prompt, it worked fine. But the same is not happening here. Is it something that's gotta do with passthru??
Thanks and Regards
Sameer
was able to solve after looking around.
Here's the working line -
passthru("pdftk editablepdf\\jason.pdf output flattenpdf\\flattened.pdf flatten");
Since \ acts as escape sequence character, i'm using "\ \" to denote the folder levels.

Extracting self-extracting exe archive via php

I have a problem. I have the service which is giving me .exe file which they claim is in fact zip archive. A self-extracting archive.
Problem is that I am downloading that with my app (php) to server and need to extract it there witout downloading to local computer.
I have tried download .exe file to local computer - it is self extracting on windows to /temp dir and than self launching FLASH player.
$zip = zip_open($myfile); gives in print_r($zip): 1
zip->open gives no results either.
change .exe to .zip doesn't let winzip or other kind of un-packer on windows to open it - .exe cannot be opened by winzip too.
Now I have no idea how to deal with it. If anybody can advise please.
Try to execute the program as an executable with the system command
Executing files from an external source you don't trust 100% is never a good idea.
The info-zip version of zip allows you to remove the SFX stub from a self-extracting zip file (with the -J flag) converting it back into a normal zip file.
Source code is freely available.
Making a self-extracting zip file is a matter of prepending a zip file with the SFX binary code, then appending the size of the binary stub to the resulting file - but I'm not sure how the data is represented - but a bit of reverse-engineering the available code should make this clear.
Well... if your PHP server is Windows you shouldn't have a problem doing it as a system command. Otherwise, it's a little more tricky. I hear that the unzip system command will unzip self-extracting zip files, but I don't have access to a Linux box at the moment to try it out.
If you're on shared hosting, chances are you can't do it.
Well if you think after executing the exe file, it will extract its content, then you can use exec function to run the .exe files like the one below:
exec("d:\\example\\php\_exe\\1436.exe");
and also you can use system function to run external programs as well.
And also if you wonder what's the difference:
PHP - exec() vs system() vs passthru()

Get filename from wget in php

I'm setting up a script so that I can input a URL to a web page and the script will wget the file. Most of the files, however, will be in the *.rar format. Is there anyway I can pass the filename to the unrar command to unarchive the files downloaded via wget?
Many, many thanks in advance!
EDIT I thought about using PHP's explode() function to break up the URL by the slashes (/) but that seems a bit hack-y.
Rather than forking out to external programs to download and extract the file, you should consider using PHP's own cURL and RAR extensions. You can use the tmpfile() function to create a temporary file, use it as the value of the CURLOPT_FILE option to make cURL save the downloaded file there, and then open that file with the RAR functions to extract the contents.
Use basename()to get the filename.
#Wyzard gives the best answer. If there's a library that solves your problem, use it instead of forking an external process. It's safer and it's the clean solution. PHP's cURL and RAR are good, use them.
However, if you must use wget and unrar, then #rik gives a good answer. wget's -O filename option saves the file as filename, so you don't have to work it out. I would rather pipe wget's output directly to unrar though, using wget -q -O - http://www.example.com | unrar.
#Byron's answer is helpful, but you really should not need to use it here. It is, however, better than using explode() as your edit mentions.
wget -O filename URL && unrar filename

Categories