php ob_start items sword width height image swords sword png rezak wid

 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
<?php
ob_start();
$items = [
'sword' => ['width' => 1, 'height' => 2, 'image' => 'swords/sword.png'],
'rezak' => ['width' => 1, 'height' => 3, 'image' => 'swords/rezak.png'],
'double' => ['width' => 2, 'height' => 2, 'image' => 'swords/double.png']
];
$inv = [
['name' => 'double', 'x' => 0, 'y' => 0, 'rotation' => 180],
['name' => 'rezak', 'x' => 3, 'y' => 1, 'rotation' => 180],
['name' => 'sword', 'x' => 0, 'y' => 2],
['name' => 'sword', 'x' => 1, 'y' => 2, 'rotation' => 180],
['name' => 'rezak', 'x' => 2, 'y' => 1],
['name' => 'sword', 'x' => 2, 'y' => 0, 'rotation' => 90],
];
$size = [4, 4];
$dp = 32;
if(isset($_GET['image'])){
header('Content-type: image/png');
$basis = imagecreatetruecolor($size[0] * $dp, $size[1] * $dp);
imagecolorallocate($basis, 0, 0, 0);
$slot = imagecreatefrompng('slot.png');
list($slot_width, $slot_height) = getimagesize('slot.png');
for ($x = 0; $x < $size[0]; ++$x) {
for ($y = 0; $y < $size[1]; ++$y) {
imagecopyresampled($basis, $slot, $x * $dp, $y * $dp, 0, 0, $dp, $dp, $slot_width, $slot_height);
}
}
imagedestroy($slot);
$count = count($inv);
for ($i = 0; $i < $count; ++$i) {
$slot = $inv[$i];
$item = $items[$slot['name']];
$image = imagecreatefrompng($item['image']);
list($width, $height) = getimagesize($item['image']);
if(in_array($slot['rotation'], [90,270])){
$image = imagerotate($image, $slot['rotation'], 0);
imagecopyresampled($basis, $image, $slot['x'] * $dp, $slot['y'] * $dp, 0, 0, $item['height'] * $dp, $item['width'] * $dp, $height, $width);
}
else{
if($slot['rotation']) $image = imagerotate($image, $slot['rotation'], 0);
imagecopyresampled($basis, $image, $slot['x'] * $dp, $slot['y'] * $dp, 0, 0, $item['width'] * $dp, $item['height'] * $dp, $width, $height);
}
imagedestroy($image);
}
imagepng($basis);
imagedestroy($basis);
}
echo '
<br />
<map name="inventory">
';
foreach($inv as $slot){
$item = $items[$slot['name']];
echo $slot['rotation'] ? '
<area shape="rect" alt="" title="" coords="'.($slot['x'] * $dp).','.($slot['y'] * $dp).','.($item['height'] * $dp + $slot['x'] * $dp ).','.($item['width'] * $dp + $slot['y'] * $dp).'" href="" target="" />
' : '
<area shape="rect" alt="" title="" coords="'.($slot['x'] * $dp).','.($slot['y'] * $dp).','.($item['width'] * $dp + $slot['x'] * $dp ).','.($item['height'] * $dp + $slot['y'] * $dp).'" href="" target="" />
';
}
echo '
<area shape="rect" alt="" title="" coords="0,64,64,96" href="" target="" />
</map>
<img usemap="inventory" src="?image" alt="inventory" />
';
?>