Skip to content

Instantly share code, notes, and snippets.

@yangg

yangg/TextureSpliter.php

Last active Aug 16, 2017
Embed
What would you like to do?
将 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