php - Upload image to Google Drive for OCR -


i'm trying upload image google drive optical character recognition (ocr). here codes:

require_once('vendor/autoload.php');  // initialize google client $client_email = 'xxxxxx@yyyyy.iam.gserviceaccount.com'; $private_key = file_get_contents('key.p12'); $scopes = array(     'https://www.googleapis.com/auth/drive.file' ); $credentials = new google_auth_assertioncredentials(     $client_email,     $scopes,     $private_key ); $client = new google_client(); $client->setassertioncredentials($credentials); if ($client->getauth()->isaccesstokenexpired()) {   $client->getauth()->refreshtokenwithassertion(); }  // initialize google drive service $service = new google_service_drive($client);  // upload file $file = new google_service_drive_drivefile(); $file->setname('test image ocr'); $file->setdescription('test image ocr'); $file->setmimetype('image/jpeg'); try {   $data = file_get_contents($filename);   $createdfile = $service->files->create($file, array(       'data' => $data,       'mimetype' => 'image/jpeg',   ));   var_dump($createdfile);   // ===========   // so, what's next?   // =========== } catch(exception $e) {   echo 'error occurred: ' . $e->getmessage(); } 

the above codes run without error, , $createdfile valid resource in google_service_drive_drivefile object form.

questions:

  1. i guess upload successful, create() function not return error. however, can't see files got uploaded in google drive. shouldn't uploaded google drive's root folder?

  2. how can perform ocr? can read here there parameter named ocrlanguage. should put , how obtain result?

thanks in advance.

update var_dump() results follow:

object(google_service_drive_drivefile)#18 (55) {   ["collection_key":protected]=>   string(6) "spaces"   ["internal_gapi_mappings":protected]=>   array(0) {   }   ["appproperties"]=>   null   ["capabilitiestype":protected]=>   string(42) "google_service_drive_drivefilecapabilities"   ["capabilitiesdatatype":protected]=>   string(0) ""   ["contenthintstype":protected]=>   string(42) "google_service_drive_drivefilecontenthints"   ["contenthintsdatatype":protected]=>   string(0) ""   ["createdtime"]=>   null   ["description"]=>   null   ["explicitlytrashed"]=>   null   ["fileextension"]=>   null   ["foldercolorrgb"]=>   null   ["fullfileextension"]=>   null   ["headrevisionid"]=>   null   ["iconlink"]=>   null   ["id"]=>   string(28) "0b_xxxxx1yjq7denaqwp4ckzork0"   ["imagemediametadatatype":protected]=>   string(48) "google_service_drive_drivefileimagemediametadata"   ["imagemediametadatadatatype":protected]=>   string(0) ""   ["kind"]=>   string(10) "drive#file"   ["lastmodifyingusertype":protected]=>   string(25) "google_service_drive_user"   ["lastmodifyinguserdatatype":protected]=>   string(0) ""   ["md5checksum"]=>   null   ["mimetype"]=>   string(10) "image/jpeg"   ["modifiedbymetime"]=>   null   ["modifiedtime"]=>   null   ["name"]=>   string(18) "test image ocr"   ["originalfilename"]=>   null   ["ownedbyme"]=>   null   ["ownerstype":protected]=>   string(25) "google_service_drive_user"   ["ownersdatatype":protected]=>   string(5) "array"   ["parents"]=>   null   ["permissionstype":protected]=>   string(31) "google_service_drive_permission"   ["permissionsdatatype":protected]=>   string(5) "array"   ["properties"]=>   null   ["quotabytesused"]=>   null   ["shared"]=>   null   ["sharedwithmetime"]=>   null   ["sharingusertype":protected]=>   string(25) "google_service_drive_user"   ["sharinguserdatatype":protected]=>   string(0) ""   ["size"]=>   null   ["spaces"]=>   null   ["starred"]=>   null   ["thumbnaillink"]=>   null   ["trashed"]=>   null   ["version"]=>   null   ["videomediametadatatype":protected]=>   string(48) "google_service_drive_drivefilevideomediametadata"   ["videomediametadatadatatype":protected]=>   string(0) ""   ["viewedbyme"]=>   null   ["viewedbymetime"]=>   null   ["viewerscancopycontent"]=>   null   ["webcontentlink"]=>   null   ["webviewlink"]=>   null   ["writerscanshare"]=>   null   ["modeldata":protected]=>   array(0) {   }   ["processed":protected]=>   array(0) {   } } 

the file can obtained via $service->files->get($file_id);, it's invisible in google drive. file resource object returned not contain useful either.

a service account isn't more dummy user. has own drive account.

if want upload personal account. take service account email address , share directory in personal drive account. other user wanted share directory or file with.

then need figure out directory id way have found have service account files.list list of has access to. once find directory id or parent id can change code above

'data' => $data, 'mimetype' => 'image/jpeg', 'parents'   => 'the directory id' 

i think looks of code using v3 api haven't had time play it. 'parents' => 'the directory id' <-- educated guess. if doesn't work let me know , google around bit figure out how pass parent v3.

option nr 2:

another option service account share folder have access drive account, , able see folder in web version of drive. search around permissions again think using v3 haven't looked @ yet. difference data stored , storage gets counted against.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -