forked from trojal/php-ro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionFrame.php
116 lines (96 loc) · 3.98 KB
/
ActionFrame.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
<?php
namespace Trojal\PhpRo;
class ActionFrame
{
public function __construct($header, $sprite)
{
$this->header = & $header;
$this->sprite = & $sprite;
$this->layers = array();
}
public function addLayer(ActionLayer $actionLayer)
{
$this->layers[] = & $actionLayer;
}
public function addData($frameData)
{
$this->frameData = & $frameData;
}
public function getDimensions()
{
if (isset($this->dimensions))
return $this->dimensions;
$this->dimensions = array(
'width' => 0,
'height' => 0
);
foreach ($this->layers as &$layer) {
$layerDimensions = $layer->getDimensions();
$layerHeader = $layer->getHeader();
if ($this->dimensions['width'] < abs($layerDimensions['width']) + 2 * abs($layerHeader['xOffset']))
$this->dimensions['width'] = abs($layerDimensions['width']) + 2 * abs($layerHeader['xOffset']);
if ($this->dimensions['height'] < abs($layerDimensions['height']) + 2 * abs($layerHeader['yOffset']))
$this->dimensions['height'] = abs($layerDimensions['height']) + 2 * abs($layerHeader['yOffset']);
}
return $this->dimensions;
}
public function getImage($imageType = PHPRO_IMG_GIF)
{
$this->getDimensions();
$containerImage = imagecreatetruecolor($this->dimensions['width'], $this->dimensions['height']);
$transparentColor = imagecolorallocatealpha($containerImage, 0xFF, 0xFF, 0xFF, 127);
imagecolortransparent($containerImage, $transparentColor);
imagefill($containerImage, 0, 0, $transparentColor);
foreach ($this->layers as &$layer) {
$layerImage = & $this->sprite->getImage(
$layer->getSpriteFrame(),
PHPRO_IMG_RAW
);
$layerHeader = & $layer->getHeader();
if ($layerHeader['mirror'] != 0) {
$mirrorImage = imagecreatetruecolor(imagesx($layerImage), imagesy($layerImage));
imagecopyresampled($mirrorImage, $layerImage, 0, 0, imagesx($layerImage) - 1, 0, imagesx($layerImage), imagesy($layerImage), -1 * imagesx($layerImage), imagesy($layerImage));
imagecolortransparent($mirrorImage, imagecolortransparent($layerImage));
$layerImage = $mirrorImage;
}
if ($layerHeader['rotation'] != 0) {
$rotatedImage = SpriteTools::rotateGdBitmap($layerImage, 360 - $layerHeader['rotation'], $this->sprite->resourcePalette[0], 1);
imagecolortransparent($rotatedImage, imagecolortransparent($layerImage));
$layerImage = $rotatedImage;
}
$xPosition = intval(($this->dimensions['width'] - imagesx($layerImage) * $layerHeader['xScale']) / 2 + $layerHeader['xOffset']);
$yPosition = intval(($this->dimensions['height'] - imagesy($layerImage) * $layerHeader['yScale']) / 2 + $layerHeader['yOffset']);
imagecopyresized(
$containerImage,
$layerImage,
$xPosition,
$yPosition,
0,
0,
imagesx($layerImage) * $layerHeader['xScale'],
imagesy($layerImage) * $layerHeader['yScale'],
imagesx($layerImage),
imagesy($layerImage)
);
}
ob_start(null, 0);
switch ($imageType) {
case PHPRO_IMG_GIF:
imagegif($containerImage);
break;
case PHPRO_IMG_JPG:
imagejpeg($containerImage);
break;
case PHPRO_IMG_PNG:
imagepng($containerImage);
break;
case PHPRO_IMG_GD2:
imagegd2($containerImage);
break;
case PHPRO_IMG_RAW:
return $containerImage;
break;
}
return ob_get_clean();
}
}