Guest User

Untitled

a guest
Oct 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. <?php
  2. class Skin{
  3. public static function getTextureFromFile(string $filename) : string{
  4. $im = imagecreatefrompng($filename);
  5. list($width, $height) = getimagesize($filename);
  6. $bytes = "";
  7. for($y = 0; $y < $height; $y++){
  8. for($x = 0; $x < $width; $x++){
  9. $code = imagecolorat($im, $x, $y);
  10. $bytes .= chr(($code >> 16) & 0xff) . chr(($code >> 8) & 0xff) . chr(($code >> 16) & 0xff) . ($code >> 24) & 0xff;
  11. }
  12. }
  13. imagedestroy($im);
  14. return $bytes;
  15. }
  16.  
  17. public static function saveTextureToFile(string $filename, string $bytes) : void{
  18. $len = strlen($bytes);
  19. $im = imagecreatetruecolor(64, $len === 8192 ? 32 : 64);
  20. $x = $y = $part = 0;
  21. while($y < 64){
  22. $cid = substr($bytes, $part, 3);
  23. if(isset($cid[0])){
  24. imagesetpixel($im, $x, $y, imagecolorallocate($im, ord($cid[0]), ord($cid[1]), ord($cid[2])));
  25. }
  26. $x++;
  27. $part += 4;
  28. if($x === 64){
  29. $x = 0;
  30. $y++;
  31. }
  32. }
  33. imagepng($im, $filename);
  34. imagedestroy($im);
  35. }
  36. }
Add Comment
Please, Sign In to add comment