-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSpriteTools.php
More file actions
67 lines (56 loc) · 2.1 KB
/
SpriteTools.php
File metadata and controls
67 lines (56 loc) · 2.1 KB
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
<?php
namespace Trojal\PhpRo;
class SpriteTools
{
static private function rotateX($x, $y, $theta)
{
return $x * cos($theta) - $y * sin($theta);
}
static private function rotateY($x, $y, $theta)
{
return $x * sin($theta) + $y * cos($theta);
}
public static function rotateGdBitmap(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0)
{
$srcw = imagesx($srcImg);
$srch = imagesy($srcImg);
if ($angle == 0) return $srcImg;
// Convert the angle to radians
$theta = deg2rad($angle);
// Calculate the width of the destination image.
$temp = array(self::rotateX(0, 0, 0 - $theta),
self::rotateX($srcw, 0, 0 - $theta),
self::rotateX(0, $srch, 0 - $theta),
self::rotateX($srcw, $srch, 0 - $theta)
);
$minX = floor(min($temp));
$maxX = ceil(max($temp));
$width = $maxX - $minX;
// Calculate the height of the destination image.
$temp = array(self::rotateY(0, 0, 0 - $theta),
self::rotateY($srcw, 0, 0 - $theta),
self::rotateY(0, $srch, 0 - $theta),
self::rotateY($srcw, $srch, 0 - $theta)
);
$minY = floor(min($temp));
$maxY = ceil(max($temp));
$height = $maxY - $minY;
$destimg = imagecreatetruecolor($width, $height);
imagefill($destimg, 0, 0, imagecolorallocate($destimg, 0, 255, 0));
// sets all pixels in the new image
for ($x = $minX; $x < $maxX; $x++) {
for ($y = $minY; $y < $maxY; $y++) {
// fetch corresponding pixel from the source image
$srcX = round(self::rotateX($x, $y, $theta));
$srcY = round(self::rotateY($x, $y, $theta));
if ($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch) {
$color = imagecolorat($srcImg, $srcX, $srcY);
} else {
$color = $bgcolor;
}
imagesetpixel($destimg, $x - $minX, $y - $minY, $color);
}
}
return $destimg;
}
}