-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
59 lines (51 loc) · 1.7 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
// Get the configuration variables.
require __DIR__ . '/config.php';
if (empty($key)) {
throw new \Exception('API key value ($key) must be set in config.php');
}
// Set up universal bits.
require __DIR__ . '/vendor/autoload.php';
header("Content-Type:application/json;charset=utf-8");
// Get input, or complain.
if (empty($_REQUEST['image'])) {
error(["message"=>"Please set the 'image' parameter"]);
}
// Make sure it's a Commons URL.
$uploadUrl = 'https://upload.wikimedia.org/';
if (substr($_REQUEST['image'], 0, strlen($uploadUrl)) !== $uploadUrl) {
error(["message"=>"Image URL must start with '$uploadUrl'"]);
}
// Otherwise, send the request onwards to Google.
$gcv = new Wikisource\GoogleCloudVisionPHP\GoogleCloudVision;
$gcv->setKey($key);
if (!empty($endpoint)) {
$gcv->setEndpoint($endpoint);
}
$gcv->setImage($_REQUEST['image']);
$gcv->addFeatureOCR();
if (!empty($_REQUEST['lang']) && $_REQUEST['lang'] !== 'en') {
$gcv->setImageContext(['languageHints' => [$_REQUEST['lang']]]);
}
$response = $gcv->request();
// Check for errors and pass any through.
if (isset($response['responses'][0]['error'])) {
error($response['responses'][0]['error']);
}
// Return only the text to the user (it's not an error if there's no text).
if (isset($response['responses'][0]['textAnnotations'][0]['description'])) {
$text = $response['responses'][0]['textAnnotations'][0]['description'];
} else {
$text = '';
}
echo json_encode(['text' => $text]);
/**
* Return a JSON error message to the user (as the top-level 'error' element).
*
* @param strong $msg The message to return.
*/
function error($msg) {
http_response_code(400);
echo json_encode([ "error" => $msg]);
exit(1);
}