-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathspritedecomposer.php
126 lines (114 loc) · 3.15 KB
/
spritedecomposer.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
120
121
122
123
124
125
126
<?php
/**
* WARNING - This version is forked from its original owner :
* @copyright Copyright © 2014 - All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* @author James Liu
* @author mail [email protected]
* @website http://www.jmsliu.com/
*/
if(empty($_POST["width"]) || empty($_POST["height"]))
{
echo "Please indicate the frame width and height";
}
else
{
$msg = validUploadFile($_FILES["file"]);
if ($msg)
{
//move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
$frameWidth = $_POST["width"];
$frameHeight = $_POST["height"];
$startX = 0;
$startY = 0;
$result = explodeImage($_FILES["file"]["tmp_name"], $_FILES["file"]["type"], $frameWidth, $frameHeight, "./result/");
foreach($result as $v) {
echo "<img src=\"".$v."\"/><br />";
}
}
else
{
echo "Invalid";
}
}
function explodeImage($srcFilePath, $srcFileType, $frameWidth, $frameHeight, $desFolder) {
$result = array();
list($width, $height) = getimagesize($srcFilePath);
$isPNG = ($srcFileType == "image/png"); // The image format can either be PNG or JPEG
$srcimage = ($isPNG) ? imagecreatefrompng($srcFilePath) : imagecreatefromjpeg($srcFilePath);
$frameNumber = 0;
for($row = 0; $row < $height / $frameHeight; $row++) {
for($col = 0; $col < $width / $frameWidth; $col++) {
$desimage = imagecreatetruecolor($frameWidth, $frameHeight);
imagealphablending($desimage, false);
imagesavealpha($desimage, true);
copyPixelsToImage($srcimage, $desimage, $col * $frameWidth, $row * $frameHeight, $frameWidth, $frameHeight);
$strFormat = ($isPNG) ? ".png" : ".jpeg";
$file = "./result/".$frameNumber.$strFormat;
if ($isPNG)
imagepng($desimage, $file);
else
imagejpeg($desimage, $file);
imagedestroy($desimage);
$result[] = $file;
$frameNumber++;
}
}
return $result;
}
function copyPixelsToImage($soruce, $destination, $startX, $startY, $width, $height) {
for($i = 0; $i < $width; $i++) {
for($j = 0; $j < $height; $j++) {
$sourcePosX = $startX + $i;
$sourcePoxY = $startY + $j;
$rgba = ImageColorAt($soruce, $sourcePosX, $sourcePoxY);
$a = ($rgba >> 24) & 0x7F;
$r = ($rgba >> 16) & 0xFF;
$g = ($rgba >> 8) & 0xFF;
$b = $rgba & 0xFF;
imagecolorallocatealpha($destination, $r, $g, $b, $a);
imagesetpixel($destination, $i, $j, $rgba);
}
}
}
function validUploadFile($file)
{
if ($file["error"] > 0)
{
return "Uploading File Error!";
}
if (empty($file["type"]))
{
return "Unknown File Type!";
}
else if ($file["type"] != "image/jpeg"
&& $file["type"] != "image/jpg"
&& $file["type"] != "image/png"
&& $file["type"] != "image/x-png")
{
return "File type ".$file["type"]." is invalid!";
}
if($file["size"] > 2000000)
{
return "File is too big!";
}
return true;
}
function validImages()
{
/**
Enable the following extensions
extension=php_mbstring.dll
extension=php_exif.dll
*/
$ext = exif_imagetype( $_FILES['file']['tmp_name']);
if($ext === false)
{
return false;
}
else
{
return true;
}
}
?>