This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyze.php
81 lines (68 loc) · 1.76 KB
/
analyze.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
<?php
require_once 'inc/autoload.php';
if (!isset($_POST["file"]) || !is_string($_POST["file"]))
{
sendError("No file uploaded");
}
try {
$analyzeImage = \Image\Image::createFromDataUrl($_POST["file"]);
$start = microtime(true);
$analyzer = new Analyzer($analyzeImage);
$result = $analyzer->getResult();
$duration = microtime(true) - $start;
sendResponse(array(
"result" => array(
"background" => $result->background->getHexString(),
"title" => $result->title->getHexString(),
"songs" => $result->songs->getHexString(),
),
"metrics" => array(
"duration" => sprintf("%f", $duration)."s",
"memory" => human_filesize(memory_get_usage()),
"memory_real" => human_filesize(memory_get_usage(true)),
"memory_peak" => human_filesize(memory_get_peak_usage()),
"memory_peak_real" => human_filesize(memory_get_peak_usage(true)),
)
));
}
catch (Exception $e)
{
sendError($e);
}
/**
* Sends an error and exits
*
* @param $message
*/
function sendError ($message)
{
sendResponse(array(
"error" => $message
));
}
/**
* Sends data and exists
*
* @param $data
*/
function sendResponse ($data)
{
header("Content-Type: application/json");
echo json_encode($data);
exit;
}
/**
* Renders the file size as human readable
*
* (thanks to: http://www.php.net/manual/de/function.filesize.php#106569)
*
* @param $bytes
* @param int $decimals
*
* @return string
*/
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = (int) floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}