diff --git a/config/install/google_vision.settings.yml b/config/install/google_vision.settings.yml new file mode 100644 index 0000000..a009027 --- /dev/null +++ b/config/install/google_vision.settings.yml @@ -0,0 +1,7 @@ +max_results_label_detection: 5 +max_results_landmark_detection: 2 +max_results_logo_detection: 2 +max_results_safe_search_detection: 1 +max_results_text_detection: 10 +max_results_face_detection: 25 +max_results_image_properties: 5 diff --git a/config/schema/google_vision.schema.yml b/config/schema/google_vision.schema.yml index 86d5a58..514cc6b 100644 --- a/config/schema/google_vision.schema.yml +++ b/config/schema/google_vision.schema.yml @@ -13,6 +13,27 @@ google_vision.settings: api_key: type: string label: 'Google Vision Api Key' + max_results_label_detection: + type: integer + label: 'Max results for the Label Detection feature' + max_results_landmark_detection: + type: integer + label: 'Max results for the Landmark Detection feature' + max_results_logo_detection: + type: integer + label: 'Max results for the Logo Detection feature' + max_results_safe_search_detection: + type: integer + label: 'Max results for the Safe Search Detection feature' + max_results_text_detection: + type: integer + label: 'Max results for the Text Detection feature' + max_results_face_detection: + type: integer + label: 'Max results for the Face Detection feature' + max_results_image_properties: + type: integer + label: 'Max results for the Image Properties feature' field.field.*.*.*.third_party.google_vision: type: mapping diff --git a/src/Form/GoogleVisionSettingsForm.php b/src/Form/GoogleVisionSettingsForm.php index 29dcddf..d52c498 100644 --- a/src/Form/GoogleVisionSettingsForm.php +++ b/src/Form/GoogleVisionSettingsForm.php @@ -50,10 +50,79 @@ class GoogleVisionSettingsForm extends ConfigFormBase {
  • Enable the Cloud Vision API.
  • Generate API key with type "Browser key" under the Credentials tab.
  • ', [ - '@url' => 'https://cloud.google.com/console' + '@url' => 'https://cloud.google.com/console', ] ), - '#default_value' => $config->get('api_key') + '#default_value' => $config->get('api_key'), + ]; + + $form['max_results'] = [ + '#type' => 'details', + '#title' => $this->t('Max number of results on each API call'), + '#open' => FALSE, + ]; + + $form['max_results']['label_detection'] = [ + '#type' => 'number', + '#title' => $this->t('Max results for the Label Detection feature'), + '#default_value' => $config->get('max_results_label_detection'), + '#min' => 1, + '#step' => 1, + '#required' => TRUE, + ]; + + $form['max_results']['landmark_detection'] = [ + '#type' => 'number', + '#title' => $this->t('Max results for the Landmark Detection feature'), + '#default_value' => $config->get('max_results_landmark_detection'), + '#min' => 1, + '#step' => 1, + '#required' => TRUE, + ]; + + $form['max_results']['logo_detection'] = [ + '#type' => 'number', + '#title' => $this->t('Max results for the Logo Detection feature'), + '#default_value' => $config->get('max_results_logo_detection'), + '#min' => 1, + '#step' => 1, + '#required' => TRUE, + ]; + + $form['max_results']['safe_search_detection'] = [ + '#type' => 'number', + '#title' => $this->t('Max results for the Safe Search Detection feature'), + '#default_value' => $config->get('max_results_safe_search_detection'), + '#min' => 1, + '#step' => 1, + '#required' => TRUE, + ]; + + $form['max_results']['text_detection'] = [ + '#type' => 'number', + '#title' => $this->t('Max results for the Text Detection feature'), + '#default_value' => $config->get('max_results_text_detection'), + '#min' => 1, + '#step' => 1, + '#required' => TRUE, + ]; + + $form['max_results']['face_detection'] = [ + '#type' => 'number', + '#title' => $this->t('Max results for the Face Detection feature'), + '#default_value' => $config->get('max_results_face_detection'), + '#min' => 1, + '#step' => 1, + '#required' => TRUE, + ]; + + $form['max_results']['image_properties'] = [ + '#type' => 'number', + '#title' => $this->t('Max results for the Image Properties Detection feature'), + '#default_value' => $config->get('max_results_image_properties'), + '#min' => 1, + '#step' => 1, + '#required' => TRUE, ]; return parent::buildForm($form, $form_state); @@ -65,6 +134,13 @@ class GoogleVisionSettingsForm extends ConfigFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('google_vision.settings') ->set('api_key', $form_state->getValue('api_key')) + ->set('max_results_label_detection', $form_state->getValue('label_detection')) + ->set('max_results_landmark_detection', $form_state->getValue('landmark_detection')) + ->set('max_results_logo_detection', $form_state->getValue('logo_detection')) + ->set('max_results_safe_search_detection', $form_state->getValue('safe_search_detection')) + ->set('max_results_text_detection', $form_state->getValue('text_detection')) + ->set('max_results_face_detection', $form_state->getValue('face_detection')) + ->set('max_results_image_properties', $form_state->getValue('image_properties')) ->save(); parent::submitForm($form, $form_state); diff --git a/src/GoogleVisionApi.php b/src/GoogleVisionApi.php index 7e2d9e1..a3242e5 100644 --- a/src/GoogleVisionApi.php +++ b/src/GoogleVisionApi.php @@ -40,11 +40,59 @@ class GoogleVisionApi implements GoogleVisionApiInterface { protected $apiKey; /** + * Stores the maxResults number for the LABEL_DETECTION feature. + * + * @var int + */ + protected $maxResultsLabelDetection; + + /** + * Stores the maxResults number for the LANDMARK_DETECTION feature. + * + * @var int + */ + protected $maxResultsLandmarkDetection; + + /** + * Stores the maxResults number for the LOGO_DETECTION feature. + * + * @var int + */ + protected $maxResultsLogoDetection; + + /** + * Stores the maxResults number for the SAFE_SEARCH_DETECTION feature. + * + * @var int + */ + protected $maxResultsSafeSearchDetection; + + /** + * Stores the maxResults number for the TEXT_DETECTION feature. + * + * @var int + */ + protected $maxResultsTextDetection; + + /** + * Stores the maxResults number for the FACE_DETECTION feature. + * + * @var int + */ + protected $maxResultsFaceDetection; + + /** + * Stores the maxResults number for the IMAGE_PROPERTIES feature. + * + * @var int + */ + protected $maxResultsImageProperties; + + /** * Construct a GoogleVisionApi object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. - * * @param \GuzzleHttp\ClientInterface $http_client * A Guzzle client object. * @@ -53,17 +101,26 @@ class GoogleVisionApi implements GoogleVisionApiInterface { public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client) { $this->configFactory = $config_factory; $this->httpClient = $http_client; - $this->apiKey = $this->configFactory->get('google_vision.settings') - ->get('api_key'); + $config = $this->configFactory->get('google_vision.settings'); + $this->apiKey = $config->get('api_key'); + $this->maxResultsLabelDetection = $config->get('max_results_label_detection'); + $this->maxResultsLandmarkDetection = $config->get('max_results_landmark_detection'); + $this->maxResultsLogoDetection = $config->get('max_results_logo_detection'); + $this->maxResultsSafeSearchDetection = $config->get('max_results_safe_search_detection'); + $this->maxResultsTextDetection = $config->get('max_results_text_detection'); + $this->maxResultsFaceDetection = $config->get('max_results_face_detection'); + $this->maxResultsImageProperties = $config->get('max_results_image_properties'); } /** * Encode image to send it to the Google Vision Api. * * @param string $filepath + * The full path to the file. */ protected function encodeImage($filepath) { - // It looks pretty dirty. I hope that in future it will be implemented in Google SDK + // 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)); return $encoded_image; @@ -72,10 +129,11 @@ class GoogleVisionApi implements GoogleVisionApiInterface { /** * Function to make request through httpClient service. * - * @param $data . - * The object to be passed during the API call. + * @param mixed $data + * The object to be passed during the API call. * - * @return An array obtained in response from the API call. + * @return array + * An array obtained in response from the API call. */ protected function postRequest($data) { $url = static::APIEndpoint . $this->apiKey; @@ -113,7 +171,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'LABEL_DETECTION', - 'maxResults' => 5 + 'maxResults' => $this->maxResultsLabelDetection ], ], ], @@ -146,7 +204,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'LANDMARK_DETECTION', - 'maxResults' => 2 + 'maxResults' => $this->maxResultsLandmarkDetection ], ], ], @@ -179,7 +237,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'LOGO_DETECTION', - 'maxResults' => 2 + 'maxResults' => $this->maxResultsLogoDetection ], ], ], @@ -212,7 +270,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'SAFE_SEARCH_DETECTION', - 'maxResults' => 1 + 'maxResults' => $this->maxResultsSafeSearchDetection ], ], ], @@ -245,7 +303,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'TEXT_DETECTION', - 'maxResults' => 10 + 'maxResults' => $this->maxResultsTextDetection ], ], ], @@ -278,7 +336,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'FACE_DETECTION', - 'maxResults' => 25 + 'maxResults' => $this->maxResultsFaceDetection ], ], ], @@ -311,7 +369,7 @@ class GoogleVisionApi implements GoogleVisionApiInterface { 'features' => [ [ 'type' => 'IMAGE_PROPERTIES', - 'maxResults' => 5 + 'maxResults' => $this->maxResultsImageProperties ], ], ], @@ -325,4 +383,5 @@ class GoogleVisionApi implements GoogleVisionApiInterface { return FALSE; } + }