diff --git a/google_vision.module b/google_vision.module index 361b56e..8d1b7cf 100755 --- a/google_vision.module +++ b/google_vision.module @@ -7,7 +7,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\field\FieldConfigInterface; use Drupal\google_vision\Connector; -use Drupal\google_vision\ImageRequest; +use Drupal\google_vision\GoogleVisionAPI; /** * Implements hook_entity_presave(). @@ -68,7 +68,7 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { // Try to retrieve file URI. $file_uri = $file->getFileUri(); if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { - $data = \Drupal::service('google_vision.connector')->makeRequest($filepath); + $data = \Drupal::service('google_vision.connector')->labelDetection($filepath); // If we have retrieved labels. if (!empty($data['responses'][0]['labelAnnotations'])) { $labels = []; diff --git a/google_vision.services.yml b/google_vision.services.yml index 0765266..f165465 100755 --- a/google_vision.services.yml +++ b/google_vision.services.yml @@ -1,6 +1,7 @@ services: google_vision.api: - class: Drupal\google_vision\ImageRequest + class: Drupal\google_vision\GoogleVisionAPI + arguments: ['@config.factory'] google_vision.connector: class: Drupal\google_vision\Connector arguments: ['@google_vision.api'] diff --git a/src/Connector.php b/src/Connector.php index aef70b4..2e19dfc 100755 --- a/src/Connector.php +++ b/src/Connector.php @@ -8,7 +8,7 @@ namespace Drupal\google_vision; use Drupal\Component\Serialization\Json; -use Drupal\google_vision\ImageRequest; +use Drupal\google_vision\GoogleVisionAPI; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -21,23 +21,75 @@ class Connector extends ControllerBase { /** * Constructor */ - public function __construct(ImageRequest $image_Request) { + public function __construct(GoogleVisionAPI $image_Request) { $this->imageRequest = $image_Request; } + /* * Function to retrieve labels for given image. */ - public function makeRequest($filepath) { + public function labelDetection($filepath) { $req = $this->imageRequest->makeRequestForLabels($filepath); + return $req; + } + + /* + * Function to detect landmarks within a given image. + */ + public function landmarkDetection($filepath) { + + $req = $this->imageRequest->makeRequestForLandmark($filepath); + return $req; + } + + /* + * Function to detect logos of famous brands within a given image. + */ + public function logoDetection($filepath) { + + $req = $this->imageRequest->makeRequestForLogo($filepath); + return $req; + } + + /* + * Function to detect explicit content within a given image. + */ + public function safeSearchDetection($filepath) { + + $req = $this->imageRequest->makeRequestForSafeSearch($filepath); + return $req; + } + + /* + * Function to retrieve texts within a given image. + */ + public function opticalCharacterRecognition($filepath) { + + $req = $this->imageRequest->makeRequestForOCR($filepath); + return $req; + } + + /* + * Function to detect faces from a given image. + */ + public function faceDetection($filepath) { + + $req = $this->imageRequest->makeRequestForFaceDetection($filepath); + return $req; + } + + /* + * Function to retrieve image attributes from a given image. + */ + public function imageAttributes($filepath) { - //return new Response($req); + $req = $this->imageRequest->makeRequestForImageAttributes($filepath); return $req; } public static function create(ContainerInterface $container) { - //$imageRequest = $container->get('google_vision.api'); return new static($container->get('google_vision.api') ); diff --git a/src/ImageRequest.php b/src/ImageRequest.php deleted file mode 100755 index c7a43b7..0000000 --- a/src/ImageRequest.php +++ /dev/null @@ -1,83 +0,0 @@ -httpClient = $http_client; - }*/ - - /** - * Function to retrieve labels for given image. - */ - public function makeRequestForLabels($filepath) { - $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { - return FALSE; - } - - // It looks pretty dirty. I hope that in future it will be implemented in Google SDK - // and we will be able to avoid this approach. - $encoded_image = base64_encode(file_get_contents($filepath)); - - // Prepare JSON. - $data = '{ - "requests":[ - { - "image":{ - "content":"' . $encoded_image . '" - }, - "features":[ - { - "type":"LABEL_DETECTION", - "maxResults":5 - } - ] - } - ] - }'; - - /*$ch = curl_init('https://vision.googleapis.com/v1/images:annotate?key=' . $api_key); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, array( - 'Content-Type: application/json', - 'Content-Length: ' . strlen($data), - ) - ); - - $result = Json::decode(curl_exec($ch)); - if (empty($result['error'])) { - return $result; - } - return FALSE;*/ - - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $request = $client->post($url, [ - 'form_params' => $data - ]); - - return json_decode($result); - } - - /*public static function create(ContainerInterface $container) { - return new static( - $container->get('http_client') - ); - } -*/ -}