<?php
ob_start();
$db = new PDO('mysql:host=localhost;dbname=sword', 'root', '');
$_items = $db -> prepare('SELECT * FROM `items`');
$_items -> execute();
$items = $_items -> fetchAll();
/*
$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 = [
['id' => '1', '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]
];
*/
$_inv = $db -> prepare('SELECT * FROM `table` where `user` = ?');
$_inv -> execute([1]);
$inv = $_inv-> fetchAll();
$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) {
$_check = $db -> prepare('SELECT * FROM `table` where `x` = ? and `y` = ?');
$_check -> execute([$x,$y]);
$check = $_check-> fetch();
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 = $db -> prepare('SELECT * FROM `items` where `id` = ?');
$_item -> execute([$slot['item']]);
$item = $_item -> fetch();
$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 = $db -> prepare('SELECT * FROM `items` where `id` = ?');
$_item -> execute([$slot['item']]);
$item = $_item -> fetch();
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="?'.$slot['id'].'" 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="?'.$slot['id'].'" target="" />
';
}
echo '
</map>
<img usemap="inventory" src="?image" alt="inventory" />
';
CREATE TABLE IF NOT EXISTS `items` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`name` varchar(55) NOT NULL,
`type` varchar(55) NOT NULL,
`image` varchar(55) NOT NULL,
`width` int(10) NOT NULL,
`height` int(10) NOT NULL,
`time` int(55) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
INSERT INTO `items` (`id`, `name`, `type`, `image`, `width`, `height`, `time`) VALUES
(1, 'Меч', 'sword', 'swords/sword.png', 1, 2, 60),
(2, 'Резак', 'sword', 'swords/rezak.png', 1, 3, 60),
(3, 'Двойной Клинок', 'sword', 'swords/double.png', 2, 2, 60);
CREATE TABLE IF NOT EXISTS `table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` int(11) NOT NULL,
`item` int(11) NOT NULL,
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`rotation` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
INSERT INTO `table` (`id`, `user`, `item`, `x`, `y`, `rotation`) VALUES
(1, 1, 1, 0, 0, 0),
(2, 1, 2, 1, 0, 0),
(3, 1, 3, 2, 0, 0),
(4, 1, 1, 0, 2, 0),
(5, 1, 2, 1, 3, 270);