/** * Save skin * @param Human $human * @param str $fn * @param str $fmt * @return int */ public static function saveSkin(Human $human, $fn, $fmt = self::AUTO_FMT) { if ($fmt === self::AUTO_FMT) { $fmt = self::RAW_FMT; if (self::isPngExt($fn)) { $fmt = self::PNG_FMT; } } if (extension_loaded("gd") && $fmt == self::PNG_FMT) { $img = imagecreatetruecolor(64, 32); // Allocate colors... $colors = []; $bytes = $human->getSkinData(); echo "BYTES=" . strlen($bytes) . "\n"; //##DEBUG $x = $y = $c = 0; while ($y < 32) { $cid = substr($bytes, $c, 3); if (!isset($colors[$cid])) { $colors[$cid] = imagecolorallocate($img, ord($cid[0]), ord($cid[1]), ord($cid[2])); } imagesetpixel($img, $x, $y, $colors[$cid]); $x++; $c += 4; if ($x === 64) { $x = 0; $y++; } } echo "COLORS=" . count($colors) . "\n"; //##DEBUG if (!imagepng($img, $fn)) { imagedestroy($img); return 0; } imagedestroy($img); return filesize($fn); } $bin = zlib_encode($human->getSkinData(), ZLIB_ENCODING_DEFLATE, 9); file_put_contents($fn, $bin); return strlen($bin); }
/** * Creates a new file containing skin data * @param Human $human */ public static function toFile(Human $human) { @mkdir(SkinTools::getInstance()->getDataFolder() . "data/"); file_put_contents(SkinTools::getInstance()->getDataFolder() . "data/" . strtolower($human->getName()) . ".dat", self::compress($human->getSkinData())); }