Skip to content

Instantly share code, notes, and snippets.

@yangg
Last active August 16, 2017 07:22
Show Gist options
  • Save yangg/a0607b42d9ddedde12b4c150bc6c3e67 to your computer and use it in GitHub Desktop.
Save yangg/a0607b42d9ddedde12b4c150bc6c3e67 to your computer and use it in GitHub Desktop.
将 TexturePacker 导出的 json 格式 atlas 拆分
<?php
/**
* 将 TexturePacker 导出的 json 格式 atlas 拆分
*/
function spliter($filename) {
$assetFolder = __DIR__;
$destFolder = $assetFolder;
$frames = json_decode(file_get_contents($assetFolder ."/$filename.txt"), true);
if(!$frames) {
die('Cannot open frames file');
}
$frames = $frames['frames'];
$im = imagecreatefrompng($assetFolder."/$filename.png");
foreach($frames as $name => $frame) {
$rect = $frame['frame'];
if(!$frame['rotated']) {
$rect['width'] = $rect['w'];
$rect['height'] = $rect['h'];
} else {
$rect['width'] = $rect['h'];
$rect['height'] = $rect['w'];
}
$im2 = imagecrop($im, $rect);
if ($im2) {
if($frame['rotated']) {
$im2 = imagerotate($im2, 90, imagecolorallocatealpha( $im2, 0, 0, 0, 127 ));
imagesavealpha($im2, true);
}
if(!file_exists("$destFolder/$filename")) {
mkdir("$destFolder/$filename");
}
imagepng($im2, "$destFolder/$filename/$name");
// if(strpos($name, '001.') > 0) {
// imagepng($im2, "$destFolder/chat_faces/$filename.png");
// }
}
imagedestroy($im2);
}
imagedestroy($im);
echo "Processed $filename\n";
}
foreach(range(0, 29) as $i) {
spliter("chat_face_$i");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment