Skip to content

Instantly share code, notes, and snippets.

@Frago9876543210
Created October 21, 2017 23:05
Show Gist options
  • Save Frago9876543210/5fe876d2790db1d35f620bc8e2c40ce8 to your computer and use it in GitHub Desktop.
Save Frago9876543210/5fe876d2790db1d35f620bc8e2c40ce8 to your computer and use it in GitHub Desktop.
<?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