-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsave_image.php
More file actions
41 lines (37 loc) · 1.24 KB
/
save_image.php
File metadata and controls
41 lines (37 loc) · 1.24 KB
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
<?php
class Response {
public $download = false;
public $full = false;
}
function saveFile($data, $full = false) {
$full = is_bool($full) ? $full : false;
$rVal = false;
if (is_string($data) && strlen($data) > 0) {
$dataDecoded = base64_decode($data);
if (base64_encode($dataDecoded) === $data) {
$genDirectory = $full ? "generated/full" : "generated";
$genPath = sprintf("%s/%s", $_SERVER["DOCUMENT_ROOT"], $genDirectory);
if (is_dir($genPath)) {
if (!is_writable($genPath)) { chmod($genPath, 0755); }
} else { mkdir($genPath, 0755); }
$fileName = sprintf("%s-%s.png", date("Ymd"), uniqid());
$filePath = sprintf("%s/%s", $genPath, $fileName);
$imageURL = sprintf("/%s/%s", $genDirectory, $fileName);
if (file_put_contents($filePath, $dataDecoded)) {
$rVal = $imageURL;
}
}
}
return $rVal;
}
$response = new Response();
if (isset($_POST["resizedData"]) && isset($_POST["fullData"])) {
$resizedData = $_REQUEST["resizedData"];
$fullData = $_REQUEST["fullData"];
$response->download = saveFile($resizedData);
$response->full = saveFile($fullData, true);
}
$response = json_encode($response);
header("Content-Type: application/vnd.api+json");
echo $response;
exit;