Community

Not able to convert image into text using php

$applicationId = 'urvishaocr'; $password = ''; $fileName = 'input.png';

$local_directory=dirname(FILE).'/images/';

$url = 'http://cloud.ocrsdk.com/processImage?language=english&exportFormat=rtf';

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "$applicationId:$password"); curl_setopt($ch, CURLOPT_POST, 1); $post_array = array( "my_file"=>"@".$fileName, );

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); $response = curl_exec($ch);

curl_close($ch); echo "

";
  print_r($response);
  die;

$xml = simplexml_load_string($response); $arr = $xml->task[0]->attributes();

$taskid = $arr["id"];

$url = 'http://cloud.ocrsdk.com/getTaskStatus'; $qry_str = "?taskid=$taskid";

do { sleep(5); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url.$qry_str); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "$applicationId:$password"); $response = curl_exec($ch); curl_close($ch);

$xml = simplexml_load_string($response);
$arr = $xml->task[0]->attributes();

} while($arr["status"] != "Completed");

$url = $arr["resultUrl"]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch);

// Let user donwload rtf result header('Content-type: application/rtf'); header('Content-Disposition: attachment; filename="file.rtf"'); echo $response; ?>

i have put image in that directory only not in iamges folder. but i am not getting $response.Can you please help me...

0

Comments

12 comments

  • Avatar
    Dmitry Mesheryakov

    Please reset your application password ASAP and never publish it on public sites.

    0
  • Avatar
    Dmitry Mesheryakov

    The folder to take the file from is controlled by this line:

    $local_directory=dirname(__FILE__).'/images/';
    

    If you have your file in some folder other than /images subfolder of the folder where the sample is located you have to modify that line accordingly.

    1
  • Avatar
    urvisha

    Hi thanks for your reply. I have put php file in ocr folder and put image in ocr folder itself .But when i run that page in $response i didnt get anything.

    0
  • Avatar
    Dmitry Mesheryakov

    @urvisha: Have you edited the code to account for such files relative location?

    0
  • Avatar
    urvisha

    I have changed in this code snippet only. $post_array = array( "my_file"=>"@".$fileName, ); Because my image file is located in the folder where php file is there.I ahve checked in php configuration curl is enable then also i am not getting any response in curl. Can you please help me out?

    0
  • Avatar
    Dmitry Mesheryakov

    @urvisha: This will not work - PHP runtime won't search for the file in the same directory as where the code file is unless you prepend it with dirname(__FILE__).

    0
  • Avatar
    urvisha

    @Dmitry Me : I have prepend $local_directory=dirname(FILE).'\images\'; this but i didt get any response in $response.Is there any prerequisite to setup for running this code? I am using appServer.Please let me know. Thanks.

    0
  • Avatar
    Dmitry Mesheryakov

    @urvisha: The sample should run "out of the box" once the correct credentials and path are provided. To ensure this is not a filepath problem could you please pull the latest changes from Github that we recently made to this sample?

    0
  • Avatar
    urvisha

    @Dmitry Me : Thanks for replying.I have used latest code from gitHub while testing it gives me file.rtf but there is no content in the file.Its blank.Can you please help me?

    0
  • Avatar
    Dmitry Mesheryakov

    @urvisha Turns out curl fails to download a file from URLs starting with https://, so for the purpose of trial use the way to go is to just disable SSL certificate verification as it's done in the latest change.

    0
  • Avatar
    urvisha

    @Dmitry Me: Thank you so much...Its working fine.. Thanks again..

    0
  • Avatar
    enjayzroc

    Hi Dmitry,

    I am not getting any response when I do curl_exec here:

    $response = curl_exec($curlHandle);

    The file path is fine, I am not getting any file error. I am just getting no response so the PHP Demo dies.

    Any idea what I'm doing wrong?

    $filePath = '/home/user1/public/OCR/sample.pdf'; if(!file_exists($filePath)) { die('File '.$filePath.' not found.'); } if(!is_readable($filePath) ) { die('Access to file '.$filePath.' denied.'); }

    $url = 'http://cloud.ocrsdk.com/processImage?language=english&exportFormat=pdfSearchable';

    / if (function_exists('curl_close')) { print "foo defined\n"; } else print "Function Doesn't Exist\n"; die; /

    $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $url); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password"); curl_setopt($curlHandle, CURLOPT_POST, 1); curl_setopt($curlHandle, CURLOPT_USERAGENT, "PHP Cloud OCR SDK Sample"); $post_array = array( "my_file"=>"@".$filePath, ); curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array); $response = curl_exec($curlHandle); print_r2($response); if($response == FALSE) { $errorText = curl_error($curlHandle); curl_close($curlHandle);

    die($errorText);
    

    } $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); curl_close($curlHandle);

    // Parse xml response $xml = simplexml_load_string($response);

    if($httpCode != 200) { if(property_exists($xml, "message")) { die($xml->message); } die("unexpected response ".$response); }

    $arr = $xml->task[0]->attributes(); $taskStatus = $arr["status"]; if($taskStatus != "Queued") { die("Unexpected task status ".$taskStatus); }

    // Task id $taskid = $arr["id"];

    // 4. Get task information in a loop until task processing finishes // 5. If response contains "Completed" staus - extract url with result // 6. Download recognition result (text) and display it

    $url = 'http://cloud.ocrsdk.com/getTaskStatus'; $qry_str = "?taskid=$taskid";

    // Check task status in a loop until it is finished // TODO: support states indicating error while(true) { sleep(5); $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $url.$qry_str); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password"); curl_setopt($curlHandle, CURLOPT_USERAGENT, "PHP Cloud OCR SDK Sample"); $response = curl_exec($curlHandle); $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); curl_close($curlHandle);

    // parse xml
    $xml = simplexml_load_string($response);
    if($httpCode != 200) {
      if(property_exists($xml, "message")) {
        die($xml->message);
      }
      die("Unexpected response ".$response);
    }
    
    0

Please sign in to leave a comment.