diff --git a/google_vision.module b/google_vision.module index b742912..c99af08 100755 --- a/google_vision.module +++ b/google_vision.module @@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\field\FieldConfigInterface; use Drupal\google_vision\Connector; +use Drupal\google_vision\ImageRequest; /** * Implements hook_entity_presave(). @@ -68,7 +69,8 @@ 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 = Connector::makeRequest($filepath); + $data = \Drupal::service('google_vision.connector')->makeRequest($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 100644 index 0000000..0765266 --- /dev/null +++ b/google_vision.services.yml @@ -0,0 +1,6 @@ +services: + google_vision.api: + class: Drupal\google_vision\ImageRequest + google_vision.connector: + class: Drupal\google_vision\Connector + arguments: ['@google_vision.api'] diff --git a/src/Connector.php b/src/Connector.php index e4507d6..8a4058f 100755 --- a/src/Connector.php +++ b/src/Connector.php @@ -9,54 +9,41 @@ namespace Drupal\google_vision; use Drupal\Component\Serialization\Json; -class Connector { +use Drupal\google_vision\ImageRequest; +use Drupal\Core\Controller\ControllerBase; + +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + +use Symfony\Component\HttpFoundation\Response; + +class Connector extends ControllerBase { + + protected $imageRequest; /** + * Constructor + */ + public function __construct(ImageRequest $image_Request) { + + $this->imageRequest = $image_Request; + } + /* * Function to retrieve labels for given image. */ - public static 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), - ) - ); + public function makeRequest($filepath) { + + $req = $this->imageRequest->makeRequestForLabels($filepath); + + //return new Response($req); + return $req; + } + + public static function create(ContainerInterface $container) { + //$imageRequest = $container->get('google_vision.api'); - $result = Json::decode(curl_exec($ch)); - if (empty($result['error'])) { - return $result; - } - return FALSE; + return new static($container->get('google_vision.api') + ); } } diff --git a/src/ImageRequest.php b/src/ImageRequest.php new file mode 100644 index 0000000..7505f2a --- /dev/null +++ b/src/ImageRequest.php @@ -0,0 +1,86 @@ +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') + ); + } +*/ +}