forked from geraintluff/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.php
127 lines (116 loc) · 3.7 KB
/
batch.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
<pre>
<?php
$config = json_decode(file_get_contents("batch-animation.json"));
var_dump($config);
if (!file_exists($config->output)) {
mkdir($config->output, 0777, TRUE);
}
$previousFilename = NULL;
if (isset($_POST['pngData'])) {
$filename = str_replace("/", "_", $_POST['filename']);
$origFilename = $config->input.$filename;
if (file_exists($origFilename)) {
$filename = explode(".", $filename);
$filename[count($filename) - 1] = "png";
$filename = implode(".", $filename);
$previousFilename = $config->output.$filename;
if (file_put_contents($config->output.$filename, base64_decode($_POST['pngData']))) {
echo "PNG saved to: ".$filename."<br>";
unlink($origFilename);
} else {
die("Error writing file: ".$config->output.$filename);
}
}
}
$filename = NULL;
$files = scandir($config->input);
shuffle($files);
foreach ($files as $f) {
if ($f[0] == ".") {
continue;
}
$filename = $f;
}
if ($filename == NULL) {
die ("All files processed");
}
?>
</pre>
<style>
#target {
border: 1px solid black;
border-radius: 5px;
margin-right: 1em;
}
#status {
}
</style>
<?php
if ($previousFilename != NULL) {
echo '<a name="prev">Previous image:</a><br>';
echo "<img src=\"".htmlentities($previousFilename)."\"><hr>";
}
?>
<canvas id="target"></canvas><br>
<div id="status"></div>
<hr>
<form id="img-form" action="?#prev" method="POST">
<input name="filename" type="hidden" id="filename"></input>
<textarea name="pngData" id="base64-data" style="width: 100%; height: 5em;"></textarea><br>
</form>
<script src="sketch.js"></script>
<script>
var filename = <?php echo json_encode($filename);?>;
var config = <?php echo json_encode($config);?>;
function setStatus(message) {
document.title = filename + ": " + message;
var statusBox = document.getElementById("status");
statusBox.innerHTML = message;
}
var canvas = document.getElementById("target");
document.getElementById("filename").value = filename;
setStatus("Starting processing");
var img = new Image();
img.onload = function() {
setStatus("Image loaded successfully");
setTimeout(function () {
setStatus("Scanning for required textures...");
var sketcher = processImage(img);
sketcher.whenReady(function () {
setStatus("Done - uploading...");
var dataUrl = canvas.toDataURL();
var base64 = dataUrl.replace(/^data:image\/\w+;base64,/, "");
document.getElementById("base64-data").value = base64;
document.getElementById("img-form").submit();
});
sketcher.progressUpdate(function (proportion, message) {
setStatus("Creating textures: " + (Math.round(proportion*1000)/10) + "% done - " + message);
});
}, 1000);
};
img.src = "source/" + filename;
function processImage(img) {
var context = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
var sketcher = new Sketcher(canvas.width, canvas.height);
sketcher.levelSteps = config.levelSteps || 5;
sketcher.maxTextures = config.maxTextures || 200;
sketcher.lineAlpha = (config.lineAlpha || 0.2);
if (config.lineAlphaVariation) {
sketcher.lineAlpha += (Math.random() - 0.5)*config.lineAlphaVariation;
}
sketcher.lineThickness = config.lineThickness || 2;
sketcher.lineDensity = config.lineDensity || 0.5;
sketcher.lightness = (config.lightness != undefined) ? config.lightness : 4;
sketcher.edgeBlurAmount = config.edgeBlurAmount || 2;
sketcher.edgeAmount = (config.edgeAmount || 0.5);
if (config.edgeAmountVariation) {
sketcher.edgeAmount + (Math.random() - 0.5)*config.edgeAmountVariation;
}
var greyscale = config.greyscale;
context.drawImage(img, 0, 0, canvas.width, canvas.height);
sketcher.transformCanvas(canvas, greyscale);
return sketcher;
}
</script>