OCR Engine2 within PHP

Hi,

I’m trying to use the OCR Engine2 within PHP but I can’t seem to get it working. I tried looking at some other forms but nothing seems to help.

In the Processing.php file this is the code. All help is appreciated. Thank you.

 <?php
> if(isset($_POST['submit']) && isset($_FILES)) {
>     require __DIR__ . '/vendor/autoload.php';
>     $target_dir = "uploads/";
>     $uploadOk = 1;
>     $FileType = strtolower(pathinfo($_FILES["attachment"]["name"],PATHINFO_EXTENSION));
>     $target_file = $target_dir . generateRandomString() .'.'.$FileType;
>     // Check file size
>     if ($_FILES["attachment"]["size"] > 5000000) {
>         header('HTTP/1.0 403 Forbidden');
>         echo "Sorry, your file is too large.";
>         $uploadOk = 0;
>     }
>     if($FileType != "pdf" && $FileType != "png" && $FileType != "jpg") {
>         header('HTTP/1.0 403 Forbidden');
>         echo "Sorry, please upload a pdf file";
>         $uploadOk = 0;
>     }
>     if ($uploadOk == 1) {
>    
>         if (move_uploaded_file($_FILES["attachment"]["tmp_name"], $target_file)) {
>             uploadToApi($target_file);
>         } else {
>             header('HTTP/1.0 403 Forbidden');
>             echo "Sorry, there was an error uploading your file.";
>         }
>     } 
> } else {
>     header('HTTP/1.0 403 Forbidden');
>     echo "Sorry, please upload a pdf file";
> }
> 
> function uploadToApi($target_file){
>     require __DIR__ . '/vendor/autoload.php';
>     $fileData = fopen($target_file, 'r');
>     $client = new \GuzzleHttp\Client();
>     try {
>     $r = $client->request('POST', 'https://api.ocr.space/parse/image',[
>         'headers' => [
>         	'apiKey' => 'xxxxxxxxxxxxxxxxxxx'
>         ],
>         'multipart' => [
>             [
> 'detectOrientation' => 'true',
> 'scale' => 'true',
> 'OCREngine' => '2',
> 'name' => 'file',
> 'contents' => $fileData
>                 
>             ]
>         ],
>         
>     ], ['file' => $fileData]);
>     $response =  json_decode($r->getBody(),true);
>     if($response['ErrorMessage'] == "") {
> ?>
> <html>
>     <head>
>     <title>Result</title>
>         <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
>         <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
>         <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/js/bootstrap.min.js'></script>
>     </head>
>     <body>
>         <div class="form-group container">
>             <label for="exampleTextarea">Result</label>
>             <textarea class="form-control" id="exampleTextarea" rows="30">
>             <?php
>                 foreach($response['ParsedResults'] as $pareValue) {
>                     echo $pareValue['ParsedText'];
>                 
>                 }
>                 
>                 
>             ?></textarea>
>         </div>
>     </body>
> </html>
> <?php
>     } else {
>         header('HTTP/1.0 400 Forbidden');
>         echo $response['ErrorMessage'];
>     }
>     } catch(Exception $err) {
>         header('HTTP/1.0 403 Forbidden');
>         echo $err->getMessage();
>     }
> }
> 
> function generateRandomString($length = 10) {
>     $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
>     $charactersLength = strlen($characters);
>     $randomString = '';
>     for ($i = 0; $i < $length; $i++) {
>         $randomString .= $characters[rand(0, $charactersLength - 1)];
>     }
>     return $randomString;
> }
> ?>

Does this code work ok with ocrengine1?

yes it does work with the first engine.

This is strange. You already using it correctly:

'OCREngine' => '2',
'OCREngine' => '1',

This should work. As a test, try 'ocrengine' => '2', (all small caps) - does that make a difference`?

If not: What is the error you are getting with engine2?

I have tried using all small caps and also everything capital. Nothing changes. It seems to be ignoring whatever rules I set in the “multipart” section.

As a test I even removed:

‘name’ => ‘file’,
‘contents’ => $fileData

and it reported that i need these two keys. So it is reading whatever I put in there but will ignore everything else other than these two pairs.

If I put an image with some text into the online version of engine one, it’s the same as the results I’m getting when calling from the API. Now When I switch the online version to engine two, I get more accurate results.

However when I try to switch the engine to the second version, it seems to be ignoring it completely. But it does generate an error-log. I think it may be contributing to the issue.

[03-Apr-2020 04:53:58 UTC] PHP Notice: Undefined index: ErrorMessage in /home/User/public_html/test/processing.php on line 56

My line 56 is this:

if($response[‘ErrorMessage’] == “”) {

Does it work ok if you use Postman to test the OCR API?

If use, then use the Postman code export feature to generate the code. I like especially the PHP cURL code. We use cURL all the time internally :slight_smile:

1 Like

It worked just fine in postman. I generated the code via postman and was able to tweak the code to be able for it to work as I want it to. Thanks for your suggestion! For anyone looking for a solution here is what I did. You will need to change a few things for yourself but:

<?php if(isset($_POST['submit']) && isset($_FILES)) { require __DIR__ . '/vendor/autoload.php'; $target_dir = "uploads/"; $uploadOk = 1; $FileType = strtolower(pathinfo($_FILES["attachment"]["name"],PATHINFO_EXTENSION)); $target_file = $target_dir . generateRandomString() .'.'.$FileType; // Check file size if ($_FILES["attachment"]["size"] > 5000000) { header('HTTP/1.0 403 Forbidden'); echo "Sorry, your file is too large."; $uploadOk = 0; } if($FileType != "pdf" && $FileType != "png" && $FileType != "jpg") { header('HTTP/1.0 403 Forbidden'); echo "Sorry, please upload a pdf file"; $uploadOk = 0; } if ($uploadOk == 1) { if (move_uploaded_file($_FILES["attachment"]["tmp_name"], $target_file)) { uploadToApi($target_file); } else { header('HTTP/1.0 403 Forbidden'); echo "Sorry, there was an error uploading your file."; } } } else { header('HTTP/1.0 403 Forbidden'); echo "Sorry, please upload a pdf file"; } function uploadToApi($target_file){ require __DIR__ . '/vendor/autoload.php'; $fileData = fopen($target_file, 'r'); $client = new \GuzzleHttp\Client(); try { $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.ocr.space/parse/image", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => array('url' => 'whatever your web servers url is relative to where the ocr files are stored make sure to include a / here at the end of the file path' .$target_file,'OCREngine' => '2','scale' => 'true'), CURLOPT_HTTPHEADER => array( "apikey: whatever your api key is" ), )); $response = curl_exec($curl); curl_close($curl); echo $response; if($response['ErrorMessage'] == "") { ?> Result
Result <?php foreach($response['ParsedResults'] as $pareValue) { echo $pareValue['ParsedText'];
            }
            
            
        ?></textarea>
    </div>
</body>
<?php } else { header('HTTP/1.0 400 Forbidden'); echo $response['ErrorMessage']; } } catch(Exception $err) { header('HTTP/1.0 403 Forbidden'); echo $err->getMessage(); } }

function generateRandomString($length = 10) {
$characters = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$charactersLength = strlen($characters);
$randomString = ‘’;
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

?>