diff --git a/api/application/controllers/Test.php b/api/application/controllers/Test.php index 2ceebd1a..064d51f7 100644 --- a/api/application/controllers/Test.php +++ b/api/application/controllers/Test.php @@ -11,7 +11,8 @@ class Test extends REST_Controller { $this->load->helper(array('form', 'url')); //$this->load->model('test_model'); $this->load->library('asynclibrary'); - $this->load->library('aws_rekonition'); + $this->load->library('AWS_REKONITION'); + $this->load->helper('MYUTIL'); } @@ -78,20 +79,69 @@ class Test extends REST_Controller { public function compareFaces_post() { - //$fk_entity_id = $this->uri->segment(2);//die(); $pdid = $this->post('pdid'); - $url1 = $this->post('url1'); - $url2 = $this->post('url2'); - $data= $this->aws_rekonition->comparePhotos($url1,$url2); - //print_r($data); - $fin_data = array(); - foreach($data as $k => $v) + $src = $this->post('src'); + $dest = $this->post('dest'); + $src_url_type = $this->post('src_url_type'); + $dest_url_type = $this->post('dest_url_type'); + $res_data = array('nxt_action' => null); + // echo $src;die(); + if($src_url_type == 'base64')//make local img { - $fin_data[$k] = $v; - // echo '***********************'; + $src_local_file = APPPATH.'docs/'.MYUTIL::getRandomString(10).'-'.date('Y-m-d-H-i-s').'.png'; + $ifp = fopen( $src_local_file, 'wb' ); + //$data = explode( ',', $src ); + fwrite( $ifp, base64_decode( $src ) ); + fclose( $ifp ); + $src = $src_local_file; + } + if($dest_url_type == 'base64')//make local img + { + $dest_local_file = APPPATH.'docs/'.MYUTIL::getRandomString(10).'-'.date('Y-m-d-H-i-s').'.png'; + $ifp = fopen( $dest_local_file, 'wb' ); + //$data = explode( ',', $src ); + fwrite( $ifp, base64_decode( $dest ) ); + fclose( $ifp ); + $dest = $dest_local_file; + } + //check no of faces in src img + + $no_of_faces_in_src_img = $this->aws_rekonition->detectFaces($src); + if(is_object($no_of_faces_in_src_img) && isset($no_of_faces_in_src_img['FaceDetails']) && count($no_of_faces_in_src_img['FaceDetails']) > 1) + { + foreach($no_of_faces_in_src_img as $k => $v) + { + $res_data[$k] = $v; + // echo '***********************'; + } + $res_data['nxt_action'] = 'pick_src_img'; + + //if no faces more than two then return all face details to + //front end and let user pick the src img + $this->response($res_data,REST_Controller::HTTP_OK); } - $this->response($fin_data,REST_Controller::HTTP_OK); + $compare_analysis_data = $this->aws_rekonition->comparePhotos($src,$dest); + + if(is_object($compare_analysis_data)) + { + foreach($compare_analysis_data as $k => $v) + { + $res_data[$k] = $v; + // echo '***********************'; + } + } + + //delete local files if created + if($dest_url_type == 'base64') + { + unlink($dest); + } + if($src_url_type == 'base64') + { + unlink($src); + } + $this->response($res_data,REST_Controller::HTTP_OK); } } \ No newline at end of file diff --git a/api/application/helpers/myutil_helper.php b/api/application/helpers/myutil_helper.php index d0a6d8a5..da2a9191 100644 --- a/api/application/helpers/myutil_helper.php +++ b/api/application/helpers/myutil_helper.php @@ -277,6 +277,19 @@ class MYUTIL //echo $different; return ($different >= $days_to_compare); } + + + public static function getRandomString($strlen = 6) + { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $strlen; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + + return $randomString; + } } diff --git a/api/application/libraries/AWS_REKONITION.php b/api/application/libraries/AWS_REKONITION.php index 34f93ad2..43e1e9f3 100644 --- a/api/application/libraries/AWS_REKONITION.php +++ b/api/application/libraries/AWS_REKONITION.php @@ -41,7 +41,7 @@ class AWS_REKONITION public function comparePhotos($url1,$url2) { // echo "listAllBuckets"; - $result = array('aksj'=>'sana'); + $result = null; $sourceImg = 'will1.jpg'; $targetImg = 'will2.jpg'; //echo $url1;die(); @@ -78,253 +78,37 @@ class AWS_REKONITION return $result; } - - public function createNewBucket($BUCKET_NAME) + + + public function detectFaces($imgurl) { - - try { - $result = $this->S3->createBucket(['ACL' => 'private','Bucket' => $BUCKET_NAME]); - - return $result; - } catch (AwsException $e) { - //output error message if fails - log_message('ERROR', $e->getMessage()); - return $e->getMessage(); - //echo "\n"; - } - } - - public function uploadFileToS3Bucket($data) - { - $result = ""; - try { - - $result = $this->S3->putObject([ - 'Bucket' => $data['bucket_name'], - 'Key' => $data['key'], - 'SourceFile' => $data['sourcefile'], - 'ContentType' => '*/*', - 'StorageClass' => 'STANDARD', - 'ACL'=>'private' - ]); - - } catch (AwsException $e) + $result = null; + try { - $result = false; - log_message('ERROR',$e); + $result = $this->RekognitionClient->detectFaces([ + 'Attributes' => ["DEFAULT"], + 'Image' => [ // REQUIRED + 'Bytes' => file_get_contents($imgurl), + // 'S3Object' => [ + // 'Bucket' => '', + // 'Name' => '', + // 'Version' => '', + // ], + ], + ]); + } + catch(AwsException $e) + { + print_r($e->getMessage()); + //echo "ex"; + // echo "\n"; . + die(); } - - return $result; - } - - /* This Function return signed URI of single S3 Object*/ - public function getSingleObjectInaBucketAsSignedURI($bucket_name,$folder_name,$time = '+5 minutes')//get as singed url - { - $result = array(); - - try { - - // $result = $this->S3->getObject([ - // 'Bucket' => 'thecucumber', - // 'Key' => 'byapi/woodgrass1.jpg' - // //'ResponseContentType' => 'image/png' - // //'ResponseContentDisposition' => 'attachment; woodgrass1.jpg', - // ]); - - $cmd = $this->S3->getCommand('GetObject', [ - 'Bucket' => $bucket_name, - 'Key' => $folder_name - ]); - - $request = $this->S3->createPresignedRequest($cmd, $time); - $presignedUrl = (string) $request->getUri(); - } - catch(AwsException $e) - { - return $e->getMessage(); - } - - return $presignedUrl; - - - } - - public function getS3SingleObj($bucket_name,$folder_name,$time = '+5 minutes') - { - //echo $time;die(); - $result = $this->S3->getObject([ - 'Bucket' => $bucket_name, - 'Key' => $folder_name - //'ResponseContentType' => 'image/png' - //'ResponseContentDisposition' => 'attachment; woodgrass1.jpg', - ]); - - return result; - } - // Get List of objetcs as byte(Raw file). - public function getAllObjectsInaBucket($bucket_name,$folder_name) - { - $objects = array(); - try { - $objects = $this->S3->getIterator('ListObjects', [ - 'Bucket' => $bucket_name, - 'Prefix' => $folder_name - ]); - - // echo "Keys retrieved!" ; - // //print_r($objects); - // foreach ($objects as $object) { - // print_r($object) ; - // } - } catch (S3Exception $e) { - echo $e->getMessage() . PHP_EOL; - } - - return $objects; - - } - - - // Delete an object from the bucket. - public function deleteSingleObjFromBucket($bucket,$key) - { - - $result = $this->S3->deleteObject([ - 'Bucket' => $bucket, - 'Key' => $key - ]); - return $result; - } - - public function setCors($BUCKETNAME) - { - $result = $this->S3->putBucketCors([ - 'Bucket' => $BUCKETNAME, - 'CORSConfiguration' => [ - 'CORSRules' => [ - [ - 'AllowedHeaders' => [ - '*', - ], - 'AllowedMethods' => [ - 'PUT', - 'POST', - 'DELETE', - ], - 'AllowedOrigins' => [ - '*', - ], - 'ExposeHeaders' => [ - 'x-amz-server-side-encryption', - ], - 'MaxAgeSeconds' => 3000, - ], - [ - 'AllowedHeaders' => [ - 'Authorization', - ], - 'AllowedMethods' => [ - 'GET', - ], - 'AllowedOrigins' => [ - '*', - ], - 'MaxAgeSeconds' => 3000, - ], - ], - ] - -]); - -return $result; } - - public function getCORES($BUCKETNAME) - { - $result = $this->S3->getBucketCors([ - 'Bucket' => $BUCKETNAME]); - //////print_r($result); - } - - public function copyS3Object($targetBucket,$sourceBucket,$sourceKeyname,$tagetKeyname) - { - - //echo $sourceBucket.'/'.$sourceKeyname; - // Copy an object. - $res = $this->S3->copyObject([ - 'Bucket' => $targetBucket, - 'Key' => $tagetKeyname.'/'.$tagetKeyname.'.png', - 'CopySource' => $sourceBucket.'/'.$sourceKeyname.'/v4MLA2019-01-12-18:01:21.png' - ]); - - print($res); - } - - public function dupSinglePDImages($bucketName,$sourceKey,$targetKey) - { - //first get all images/s3 obj from source bucket$objects = array(); - $existingS3Objects = array(); - $copied_objects = 0; - try { - - $existingS3Objects =$this->S3->listObjects(['Bucket' => $bucketName,'Prefix' => $sourceKey]); - if(isset($existingS3Objects['Contents']) && count($existingS3Objects['Contents'])) - { - // Perform a batch of CopyObject operations. - $batch = array(); - foreach($existingS3Objects['Contents'] as $obj_key => $obj_vale) - { - $key = ($obj_vale['Key']); - $fulltargetKey = (function() use ($targetKey,$key){ - $res = ''; - $t = explode('/',$key);unset($t[0]); - foreach ($t as $index => $value) { $res .= (count($t) == ($index+1) ? $value.'/' : $value); } - return $targetKey.'/'.$res; - - }); - $fulltargetKey = $fulltargetKey(); - //echo $fulltargetKey.'###'.$bucketName.'/'.$key.'~~~'; - $batch[] = $this->S3->getCommand('CopyObject', [ - 'Bucket' => $bucketName, - 'Key' => $fulltargetKey, - 'CopySource' => "$bucketName/$key", - 'MetadataDirective' => 'REPLACE' - ]); - } - - try { - $results = CommandPool::batch($this->S3, $batch); - foreach($results as $result) { - if ($result instanceof ResultInterface) - { - $copied_objects++; - echo 'DONE'; - } - if ($result instanceof AwsException) { - log_message('ERROR','###Error in Batch result - '.$result->getAwsErrorMessage()); - - } - } - } catch (Exception $e) - { - log_message('ERROR','###Error in executing Copy Batch Operation'); - log_message('ERROR',$e->getMessage()); - echo $e->getMessage() . PHP_EOL; - } - } - }catch (S3Exception $e) - { - log_message('ERROR','###Error in getting all exist s3 keys'); - log_message('ERROR',$e->getMessage()); - //echo $e->getMessage() . PHP_EOL; - } - - echo $copied_objects; - - } + } ?> \ No newline at end of file