-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchessdiagram.php
119 lines (90 loc) · 3.02 KB
/
chessdiagram.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
<?php
require('chessset.php');
$whites = $_GET['w'];
$blacks = $_GET['b'];
$board = array();
parsePieces($whites, 'w', $board);
parsePieces($blacks, 'b', $board);
$set = new ChessSet();
header("Content-type: image/png");
header('Content-Disposition: attachment; filename="chess-diagra-matic.png"');
imagepng(placePieces($board, $set));
function parsePieces($pieces, $color, &$board)
{
foreach (explode(",", $pieces) as $piece)
{
$piece = trim ($piece);
if (strlen($piece) == 2)
{
$piece = 'P' . $piece;
}
if (strlen($piece) != 3)
continue;
$code = strtoupper(substr($piece, 0, 1));
$col = strtolower(substr($piece, 1, 1));
$row = intval(substr($piece, 2, 1));
$board[$row][$col] = $color . $code;
}
}
function placePieces($board, $set)
{
$cols = array ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$rows = array (8, 7, 6, 5, 4, 3, 2, 1);
$boardImg = $set->getBoard();
$size = $set->getSquareSize();
$background = 'w';
$offy = $set->getMarginTop();
foreach ($rows as $row)
{
$offx = $set->getMarginLeft();
foreach ($cols as $col)
{
if (isset($board[$row][$col]))
{
$color = substr($board[$row][$col], 0, 1);
$code = substr($board[$row][$col], 1, 1);
if ($set->isTransparent())
{
$pieceImg = $set->getPiece($code, $color, $background);
$pieceImgTrueColor = imagecreatetruecolor($size, $size);
imagecopy(
$pieceImgTrueColor,
$pieceImg,
0,
0,
0,
0,
$size,
$size);
imagecolortransparent($pieceImgTrueColor, imagecolorat($pieceImgTrueColor, 0, 0));
imagecopymerge(
$boardImg,
$pieceImgTrueColor,
$offx,
$offy,
0,
0,
$size,
$size,
100);
}
else
imagecopy(
$boardImg,
$set->getPiece($code, $color, $background),
$offx,
$offy,
0,
0,
$size,
$size);
}
$background = $background == 'w' ? 'b' : 'w';
$offx += $size;
}
$background = $background == 'w' ? 'b' : 'w';
$offy += $size;
}
return $boardImg;
}
?>