diff --git a/google_vision.module b/google_vision.module index b742912..04b7cbf 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,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 = 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..8ece2f3 --- /dev/null +++ b/google_vision.services.yml @@ -0,0 +1,3 @@ +services: + google_vision.api: + class: Drupal\google_vision\ImageRequest diff --git a/src/Connector.php b/src/Connector.php index e4507d6..0463801 100755 --- a/src/Connector.php +++ b/src/Connector.php @@ -9,54 +9,39 @@ namespace Drupal\google_vision; use Drupal\Component\Serialization\Json; -class Connector { +use Drupal\google_vision\ImageRequest; +use Drupal\Core\Controller\ControllerBase; + +use Symfony\Component\DependencyInjection\ContainerInterface; + +use Symfony\Component\HttpFoundation\Response; + +class Connector extends ControllerBase { + + protected $imageRequest; /** + * Constructor + */ + public function __construct(ImageRequest $imageRequest) { + + $this->imageRequest = $imageRequest; + } + /* * 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), - ) - ); - - $result = Json::decode(curl_exec($ch)); - if (empty($result['error'])) { - return $result; - } - return FALSE; + public static 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'); + + return new static($imageRequest); } } diff --git a/src/ImageRequest.php b/src/ImageRequest.php new file mode 100644 index 0000000..e143df4 --- /dev/null +++ b/src/ImageRequest.php @@ -0,0 +1,62 @@ +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; + } + +}