1 | <?php |
||
10 | class GoogleCloudVision |
||
11 | { |
||
12 | |||
13 | protected $features = array(); |
||
14 | protected $imageContext = array(); |
||
15 | protected $image = array(); |
||
16 | protected $requestBody = array(); |
||
17 | protected $version = "v1"; |
||
18 | protected $endpoint = "https://vision.googleapis.com/"; |
||
19 | protected $key; |
||
20 | |||
21 | /** @var int The maximum size allowed for the image, in bytes. */ |
||
22 | protected $imageMaxSize; |
||
23 | |||
24 | /** @var string Image type: Google Cloud Storage URI. Note the typo. */ |
||
25 | const IMAGE_TYPE_GCS = 'GSC'; |
||
26 | |||
27 | /** @var string Image type: file path or URL. */ |
||
28 | const IMAGE_TYPE_FILE = 'FILE'; |
||
29 | |||
30 | /** @var string Image type: raw data. */ |
||
31 | const IMAGE_TYPE_RAW = 'RAW'; |
||
32 | |||
33 | /** |
||
34 | * Create a new GCV API object. |
||
35 | */ |
||
36 | public function __construct() |
||
40 | |||
41 | /** |
||
42 | * Change the URL for the API endpoint. Defaults to https://vision.googleapis.com/ but may need to be changed for |
||
43 | * various reasons (e.g. if routing through a proxy server). |
||
44 | * |
||
45 | * @param string $newEndpoint The new URL of the API endpoint. |
||
46 | */ |
||
47 | public function setEndpoint($newEndpoint) |
||
51 | |||
52 | /** |
||
53 | * Set the permitted maximum size of images. |
||
54 | * This defaults to 4 MB as per the Google Clound Vision API limits documentation. |
||
55 | * |
||
56 | * @param ing $newSize |
||
57 | * @throws Exception |
||
58 | */ |
||
59 | public function setImageMaxSize($newSize) |
||
66 | |||
67 | /** |
||
68 | * Set the image that will be sent to the API. |
||
69 | * |
||
70 | * An image can be set from a filename or URL, raw data, or a Google Cloud Storage item. |
||
71 | * |
||
72 | * A Google Cloud Storage image URI must be in the following form: gs://bucket_name/object_name. |
||
73 | * Object versioning is not supported. |
||
74 | * Read more: https://cloud.google.com/vision/reference/rest/v1/images/annotate#imagesource |
||
75 | * |
||
76 | * @param mixed $input The filename, URL, data, etc. |
||
77 | * @param string $type The type that $input should be treated as. |
||
78 | * @return string[] The request body. |
||
79 | * @throws LimitExceededException When the image size is over the maximum permitted. |
||
80 | */ |
||
81 | public function setImage($input, $type = self::IMAGE_TYPE_FILE) |
||
96 | |||
97 | /** |
||
98 | * Fetch base64-encoded data of the specified image. |
||
99 | * |
||
100 | * @param string $path Path to the image file. Anything supported by file_get_contents is suitable. |
||
101 | * @return string The encoded data as a string or FALSE on failure. |
||
102 | * @throws LimitExceededException When the image size is over the maximum permitted. |
||
103 | */ |
||
104 | public function convertImgtoBased64($path) |
||
117 | |||
118 | /** |
||
119 | * Set the request body, based on the image, features, and imageContext. |
||
120 | * |
||
121 | * @return string[] |
||
122 | */ |
||
123 | protected function setRequestBody() |
||
136 | |||
137 | public function addFeature($type, $maxResults = 1) |
||
147 | |||
148 | public function setImageContext($imageContext) |
||
156 | |||
157 | public function addFeatureUnspecified($maxResults = 1) |
||
161 | |||
162 | public function addFeatureFaceDetection($maxResults = 1) |
||
166 | |||
167 | public function addFeatureLandmarkDetection($maxResults = 1) |
||
171 | |||
172 | public function addFeatureLogoDetection($maxResults = 1) |
||
176 | |||
177 | public function addFeatureLabelDetection($maxResults = 1) |
||
181 | |||
182 | public function addFeatureOCR($maxResults = 1) |
||
186 | |||
187 | public function addFeatureSafeSeachDetection($maxResults = 1) |
||
191 | |||
192 | public function addFeatureImageProperty($maxResults = 1) |
||
196 | |||
197 | /** |
||
198 | * Send the request to Google and get the results. |
||
199 | * |
||
200 | * @param string $apiMethod Which API method to use. Currently can only be 'annotate'. |
||
201 | * |
||
202 | * @return string[] The results of the request. |
||
203 | * @throws Exception If any of the key, features, or image have not been set yet. |
||
204 | */ |
||
205 | public function request($apiMethod = "annotate") |
||
223 | |||
224 | /** |
||
225 | * Set the API key. |
||
226 | * |
||
227 | * @param string $key The API key. |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | public function setKey($key) |
||
235 | |||
236 | /** |
||
237 | * Execute the request and return the result. |
||
238 | * |
||
239 | * @param string $url The full URL to query. |
||
240 | * @param string[] $data The data to send. |
||
241 | * |
||
242 | * @return string[] The resulting information about the image. |
||
243 | */ |
||
244 | protected function requestServer($url, $data) |
||
256 | } |
||
257 |