Created
October 21, 2017 23:05
-
-
Save Frago9876543210/5fe876d2790db1d35f620bc8e2c40ce8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment