Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Skin{
- public static function getTextureFromFile(string $filename) : string{
- $im = imagecreatefrompng($filename);
- list($width, $height) = getimagesize($filename);
- $bytes = "";
- for($y = 0; $y < $height; $y++){
- for($x = 0; $x < $width; $x++){
- $code = imagecolorat($im, $x, $y);
- $bytes .= chr(($code >> 16) & 0xff) . chr(($code >> 8) & 0xff) . chr(($code >> 16) & 0xff) . ($code >> 24) & 0xff;
- }
- }
- imagedestroy($im);
- return $bytes;
- }
- public static function saveTextureToFile(string $filename, string $bytes) : void{
- $len = strlen($bytes);
- $im = imagecreatetruecolor(64, $len === 8192 ? 32 : 64);
- $x = $y = $part = 0;
- while($y < 64){
- $cid = substr($bytes, $part, 3);
- if(isset($cid[0])){
- imagesetpixel($im, $x, $y, imagecolorallocate($im, ord($cid[0]), ord($cid[1]), ord($cid[2])));
- }
- $x++;
- $part += 4;
- if($x === 64){
- $x = 0;
- $y++;
- }
- }
- imagepng($im, $filename);
- imagedestroy($im);
- }
- }
Add Comment
Please, Sign In to add comment