1 | <?php |
||
7 | class GoogleCloudVision |
||
8 | { |
||
9 | |||
10 | protected $features = array(); |
||
11 | protected $imageContext = array(); |
||
12 | protected $image = array(); |
||
13 | protected $requestBody = array(); |
||
14 | protected $version = "v1"; |
||
15 | protected $endpoint = "https://vision.googleapis.com/"; |
||
16 | protected $key; |
||
17 | |||
18 | /** @var int The maximum size allowed for the image, in bytes. */ |
||
19 | protected $imageMaxSize = 1024 * 1024 * 4; |
||
20 | |||
21 | /** @var string Image type: Google Cloud Storage URI. Note the typo. */ |
||
22 | const IMAGE_TYPE_GCS = 'GSC'; |
||
23 | |||
24 | /** @var string Image type: file path or URL. */ |
||
25 | const IMAGE_TYPE_FILE = 'FILE'; |
||
26 | |||
27 | /** @var string Image type: raw data. */ |
||
28 | const IMAGE_TYPE_RAW = 'RAW'; |
||
29 | |||
30 | /** |
||
31 | * Change the URL for the API endpoint. Defaults to https://vision.googleapis.com/ but may need to be changed for |
||
32 | * various reasons (e.g. if routing through a proxy server). |
||
33 | * |
||
34 | * @param string $newEndpoint The new URL of the API endpoint. |
||
35 | */ |
||
36 | public function setEndpoint($newEndpoint) |
||
40 | |||
41 | /** |
||
42 | * Set the permitted maximum size of images. |
||
43 | * This defaults to 4 MB as per the Google Clound Vision API limits documentation. |
||
44 | * |
||
45 | * @param ing $newSize |
||
46 | * @throws Exception |
||
47 | */ |
||
48 | public function setImageMaxSize($newSize) |
||
55 | |||
56 | /** |
||
57 | * Set the image that will be sent to the API. |
||
58 | * |
||
59 | * An image can be set from a filename or URL, raw data, or a Google Cloud Storage item. |
||
60 | * |
||
61 | * A Google Cloud Storage image URI must be in the following form: gs://bucket_name/object_name. |
||
62 | * Object versioning is not supported. |
||
63 | * Read more: https://cloud.google.com/vision/reference/rest/v1/images/annotate#imagesource |
||
64 | * |
||
65 | * @param mixed $input The filename, URL, data, etc. |
||
66 | * @param string $type The type that $input should be treated as. |
||
67 | * @return string[] The request body. |
||
68 | * @throws LimitExceededException When the image size is over the maximum permitted. |
||
69 | */ |
||
70 | public function setImage($input, $type = self::IMAGE_TYPE_FILE) |
||
85 | |||
86 | /** |
||
87 | * Fetch base64-encoded data of the specified image. |
||
88 | * |
||
89 | * @param string $path Path to the image file. Anything supported by file_get_contents is suitable. |
||
90 | * @return string The encoded data as a string or FALSE on failure. |
||
91 | * @throws LimitExceededException When the image size is over the maximum permitted. |
||
92 | */ |
||
93 | public function convertImgtoBased64($path) |
||
103 | |||
104 | /** |
||
105 | * Set the request body, based on the image, features, and imageContext. |
||
106 | * |
||
107 | * @return string[] |
||
108 | */ |
||
109 | protected function setRequestBody() |
||
122 | |||
123 | public function addFeature($type, $maxResults = 1) |
||
133 | |||
134 | public function setImageContext($imageContext) |
||
142 | |||
143 | public function addFeatureUnspecified($maxResults = 1) |
||
147 | |||
148 | public function addFeatureFaceDetection($maxResults = 1) |
||
152 | |||
153 | public function addFeatureLandmarkDetection($maxResults = 1) |
||
157 | |||
158 | public function addFeatureLogoDetection($maxResults = 1) |
||
162 | |||
163 | public function addFeatureLabelDetection($maxResults = 1) |
||
167 | |||
168 | public function addFeatureOCR($maxResults = 1) |
||
172 | |||
173 | public function addFeatureSafeSeachDetection($maxResults = 1) |
||
177 | |||
178 | public function addFeatureImageProperty($maxResults = 1) |
||
182 | |||
183 | /** |
||
184 | * Send the request to Google and get the results. |
||
185 | * |
||
186 | * @param string $apiMethod Which API method to use. Currently can only be 'annotate'. |
||
187 | * @return string[] The results of the request. |
||
188 | * @throws Exception |
||
189 | */ |
||
190 | public function request($apiMethod = "annotate") |
||
208 | |||
209 | public function setKey($key) |
||
213 | |||
214 | protected function requestServer($url, $data) |
||
226 | } |
||
227 |