diff --git a/google_vision.module b/google_vision.module index b742912..8ea4102 100755 --- a/google_vision.module +++ b/google_vision.module @@ -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 = Connector::makeRequestForLabels($filepath); + $data = \Drupal::service('google_vision.requests')->makeRequestForLabels($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 new file mode 100755 index 0000000..3fc2cb2 --- /dev/null +++ b/google_vision.services.yml @@ -0,0 +1,3 @@ +services: + google_vision.requests: + class: Drupal\google_vision\Connector diff --git a/src/Connector.php b/src/Connector.php index e4507d6..79ee470 100755 --- a/src/Connector.php +++ b/src/Connector.php @@ -14,15 +14,13 @@ class Connector { /** * Function to retrieve labels for given image. */ - public static function makeRequestForLabels($filepath) { + 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. @@ -59,4 +57,280 @@ class Connector { return FALSE; } + /** + * Function to retrieve landmark from a given image. + */ + public function makeRequestForLandmark($filepath) { + $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + $encoded_image = base64_encode(file_get_contents($filepath)); + + // Prepare JSON. + $data = '{ + "requests":[ + { + "image":{ + "content":"' . $encoded_image . '" + }, + "features":[ + { + "type":"LANDMARK_DETECTION", + "maxResults":1 + } + ] + } + ] + }'; + + $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; + } + + /** + * Function to retrieve logo of given image. + */ + public function makeRequestForLogo($filepath) { + $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + $encoded_image = base64_encode(file_get_contents($filepath)); + + // Prepare JSON. + $data = '{ + "requests":[ + { + "image":{ + "content":"' . $encoded_image . '" + }, + "features":[ + { + "type":"LOGO_DETECTION", + "maxResults":1 + } + ] + } + ] + }'; + + $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; + } + + /** + * Function for safe search detection. + */ + public function makeRequestForSafeSearch($filepath) { + $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + $encoded_image = base64_encode(file_get_contents($filepath)); + + // Prepare JSON. + $data = '{ + "requests":[ + { + "image":{ + "content":"' . $encoded_image . '" + }, + "features":[ + { + "type":"SAFE_SEARCH_DETECTION", + "maxResults":1 + } + ] + } + ] + }'; + + $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; + } + + /** + * Function to retrieve texts within a given image. + */ + public function makeRequestForOCR($filepath) { + $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + $encoded_image = base64_encode(file_get_contents($filepath)); + + // Prepare JSON. + $data = '{ + "requests":[ + { + "image":{ + "content":"' . $encoded_image . '" + }, + "features":[ + { + "type":"TEXT_DETECTION", + "maxResults":10 + } + ] + } + ] + }'; + + $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; + } + + /** + * Function for face detection. + */ + public function makeRequestForFace($filepath) { + $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + $encoded_image = base64_encode(file_get_contents($filepath)); + + // Prepare JSON. + $data = '{ + "requests":[ + { + "image":{ + "content":"' . $encoded_image . '" + }, + "features":[ + { + "type":"FACE_DETECTION", + "maxResults":15 + } + ] + } + ] + }'; + + $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; + } + + /** + * Function to retrieve image attributes for given image. + */ + public function makeRequestForImageProperties($filepath) { + $config = \Drupal::getContainer()->get('config.factory')->getEditable('google_vision.settings'); + $api_key = $config->get('api_key'); + if (!$api_key) { + return FALSE; + } + + $encoded_image = base64_encode(file_get_contents($filepath)); + + // Prepare JSON. + $data = '{ + "requests":[ + { + "image":{ + "content":"' . $encoded_image . '" + }, + "features":[ + { + "type":"IMAGE_PROPERTIES", + "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; + } + }