-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaint.php
52 lines (45 loc) · 1.29 KB
/
paint.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
<?php
require('LZString.php');
$imagePath = 'images/test.bmp';
$paletteKey = 'test.palette';
$pendingTasksKey = 'test.pending';
$data = LZString::decompressFromBase64($_POST['data']);
$json = json_decode($data, true);
$taskId = $json['taskId'];
$x = $json['x'];
$y = $json['y'];
$columns = $json['columns'];
$rows = $json['rows'];
$solution = $json['solution'];
$image = new Imagick($imagePath);
$areaIterator = $image->getPixelRegionIterator($x, $y, $columns, $rows);
$index = 0;
$bool = true;
$colorPalette = apc_fetch($paletteKey, $bool);
foreach ($areaIterator as $rowIterator) {
foreach ($rowIterator as $pixel) {
$nearestPoint = $solution[$index];
$index += 1;
if ($nearestPoint != -1) {
$color = $colorPalette[$nearestPoint];
$pixel->setColor(sprintf("rgb(%s, %s, %s)", $color['r'], $color['g'], $color['b']));
} else {
$pixel->setColor("rgb(0, 0, 0)"); //pixel is already black
}
}
$areaIterator->syncIterator();
}
$saveOk = $image->writeImage();
unlink('images/test.jpg');
#$image->setImageFormat('jpg');
#$image->writeImage('images/test.jpg');
if ($saveOk) {
$pendingTasks = apc_fetch($pendingTasksKey);
foreach ($pendingTasks as $key => $task) {
if($task['taskId'] == $taskId) {
unset($pendingTasks[$key]);
apc_store($pendingTasksKey, $pendingTasks);
break;
}
}
}