-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
215 lines (178 loc) · 7.98 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
require_once 'vendor/autoload.php';
require_once "./random_string.php";
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
use MicrosoftAzure\Storage\Blob\Models\PublicAccessType;
# Setup a specific instance of an Azure::Storage::Client
$connectionString = "DefaultEndpointsProtocol=https;AccountName=satustorage;AccountKey=LWI57S7IKnHpOILjV+Wv0htRSvLx0KV3kKUixadjA4FL1s0bFrWFT7F4upXCFx23oOUP9IrVXLReo9BRiZAb2A==;EndpointSuffix=core.windows.net";
// Create blob client.
$blobClient = BlobRestProxy::createBlobService($connectionString);
# Create the BlobService that represents the Blob service for the storage account
$createContainerOptions = new CreateContainerOptions();
$createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);
// Set container metadata.
$createContainerOptions->addMetaData("key1", "value1");
$createContainerOptions->addMetaData("key2", "value2");
$containerName = "popow" . generateRandomString();
try {
// Create container.
$blobClient->createContainer($containerName, $createContainerOptions);
} catch (ServiceException $e) {
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/library/azure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code . ": " . $error_message . "<br />";
} catch (InvalidArgumentTypeException $e) {
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/library/azure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code . ": " . $error_message . "<br />";
}
if (isset($_POST['submit'])) {
$temp = explode(".", $_FILES["images"]["name"]);
$fileToUpload = $containerName . '.' . end($temp);
echo '<pre>';
if (move_uploaded_file($_FILES['images']['tmp_name'], $fileToUpload)) {
echo "File is valid, and was successfully uploaded.\n";
$myfile = fopen($fileToUpload, "r") or die("Unable to open file!");
fclose($myfile);
# Upload file as a block blob
echo "Uploading BlockBlob: " . PHP_EOL;
echo $fileToUpload;
$content = fopen($fileToUpload, "r");
//Upload blob
$blobClient->createBlockBlob($containerName, $fileToUpload, $content);
header('location: index.php?containerName=' . $containerName);
} else {
header('location: index.php');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>DICODING INDONESIA</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1 class="display-4">Azure Computer Vision Analyze</h1>
<p class="lead">Upload & dan analisis isi gambar menggunakan azure computer vision</p>
</div>
<div class="container container-fluid">
<form action="" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col">
<div class="card p-3 mb-3" style="width: 18rem;">
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="customFile" name="images">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<input type="submit" name="submit" value="Analisis" class="btn btn-primary form-control">
</div>
<?php
if (!empty($_GET['containerName'])) {
$containerName = $_GET['containerName'];
$listBlobsOptions = new ListBlobsOptions();
do {
$result = $blobClient->listBlobs($containerName, $listBlobsOptions);
foreach ($result->getBlobs() as $blob) {
echo '<img src=' . $blob->getUrl() . ' id="images">';
}
$listBlobsOptions->setContinuationToken($result->getContinuationToken());
} while ($result->getContinuationToken());
}
?>
</div>
</div>
<div class="row" id="result">
<div class="col-md-12">
<h2 id="captions"></h2>
</div>
</div>
</form>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$("#result").hide();
</script>
<?php
if (!empty($_GET['containerName'])) {
?>
<script type="text/javascript">
$("#result").show();
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace <Subscription Key> with your valid subscription key.
var subscriptionKey = "4097351ee56b45148fea3caada225e2a";
// You must use the same Azure region in your REST API method as you used to
// get your subscription keys. For example, if you got your subscription keys
// from the West US region, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
var uriBase =
"https://southeastasia.api.cognitive.microsoft.com/vision/v1.0/analyze";
// Request parameters.
var params = {
"visualFeatures": "Categories,Description,Color",
"details": "",
"language": "en",
};
// Display the image.
var sourceImageUrl = document.getElementById("images").src;
// document.querySelector("#sourceImage").src = sourceImageUrl;
// Make the REST API call.
$.ajax({
url: uriBase + "?" + $.param(params),
// Request headers.
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/json");
xhrObj.setRequestHeader(
"Ocp-Apim-Subscription-Key", subscriptionKey);
},
type: "POST",
// Request body.
data: '{"url": ' + '"' + sourceImageUrl + '"}',
})
.done(function (data) {
// Show formatted JSON on webpage.
$("#captions").text(data['description']['captions'][0]['text']);
})
.fail(function (jqXHR, textStatus, errorThrown) {
// Display error message.
var errorString = (errorThrown === "") ? "Error. " :
errorThrown + " (" + jqXHR.status + "): ";
errorString += (jqXHR.responseText === "") ? "" :
jQuery.parseJSON(jqXHR.responseText).message;
alert(errorString);
});
</script>
<?php
}
?>
</html>