PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
SkinImage.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol\types\skin;
16
17use function strlen;
18
20 public function __construct(
21 private int $height,
22 private int $width,
23 private string $data
24 ){
25 if($height < 0 or $width < 0){
26 throw new \InvalidArgumentException("Height and width cannot be negative");
27 }
28 if(($expected = $height * $width * 4) !== ($actual = strlen($data))){
29 throw new \InvalidArgumentException("Data should be exactly $expected bytes, got $actual bytes");
30 }
31 }
32
33 public static function fromLegacy(string $data) : SkinImage{
34 switch(strlen($data)){
35 case 64 * 32 * 4:
36 return new self(32, 64, $data);
37 case 64 * 64 * 4:
38 return new self(64, 64, $data);
39 case 128 * 128 * 4:
40 return new self(128, 128, $data);
41 }
42
43 throw new \InvalidArgumentException("Unknown size");
44 }
45
46 public function getHeight() : int{
47 return $this->height;
48 }
49
50 public function getWidth() : int{
51 return $this->width;
52 }
53
54 public function getData() : string{
55 return $this->data;
56 }
57}