presentkim / png-converter
Converter between PNG images (GD) and Minecraft: Bedrock SkinImage
Fund package maintenance!
Requires
- php: ^8.4
- ext-gd: *
- pocketmine/pocketmine-mp: ^5.0
Requires (Dev)
- axolotl-pm/pocketmine-mp: ^5.51
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
png-converter
Convert PNG files and GD images to Bedrock SkinImage, and back.
Packagist ยท Changelog ยท Report a bug ยท Request a feature
$skinImage = PngConverter::toSkinImageFromFile("skin.png");
๐ Why png-converter?
Skins travel as raw RGBA bytes (SkinImage), while artists work with PNG files.
This library does the pixel conversion, including the alpha channel, which GD and Bedrock store differently.
- Both directions : PNG file / GD image to
SkinImage,SkinImage/SkinDatato a GD image - Correct alpha : GD alpha
0..127(0 is opaque) is mapped to0..255, so an opaque pixel is255, not254 - Palette PNGs : read through the palette instead of treating the index as a color
- Fast : lookup tables and one
pack()/unpack()call per image (a 64x64 skin converts in about 0.15 ms) - Tested : on PHP 8.4 / 8.5
๐ฆ Installation
composer require presentkim/png-converter
| Requirement | Version |
|---|---|
| Server | axolotl-pm/PocketMine-MP (fork of PocketMine-MP 5) |
| PHP | 8.4 or newer, with ext-gd |
The Poggit virion for PocketMine-MP API 4 is kept on the
legacy/pmmp-5.44branch (no longer maintained).
๐ Quick start
use kim\present\converter\png\PngConverter; // PNG file -> SkinImage (throws RuntimeException when the file cannot be read) $skinImage = PngConverter::toSkinImageFromFile($plugin->getDataFolder() . "skin.png"); // PNG data in memory -> SkinImage $skinImage = PngConverter::toSkinImageFromString($pngBytes); // GD image -> SkinImage $skinImage = PngConverter::toSkinImage($gdImage); // Check the size before using the image as a player skin (64x32, 64x64 or 128x128) if(!PngConverter::isValidSkinSize($skinImage)){ throw new \InvalidArgumentException("Unsupported skin size"); } // SkinImage / SkinData -> GD image, e.g. to save it as a PNG file $gdImage = PngConverter::toPng($skinImage); $gdImage = PngConverter::toPngFromSkinData($skinData); imagepng($gdImage, "skin.png");
๐ API reference
| Method | Description |
|---|---|
toSkinImage(GdImage $image) : SkinImage |
Convert a GD image (it is not destroyed) |
toSkinImageFromFile(string $filepath) : SkinImage |
Read a PNG file |
toSkinImageFromString(string $data) : SkinImage |
Read image data in memory (any format GD reads) |
toPng(SkinImage $skinImage) : GdImage |
Convert to a true color GD image with alpha |
toPngFromSkinData(SkinData $skinData) : GdImage |
Convert the skin image of a SkinData |
isValidSkinSize(GdImage|SkinImage $image) : bool |
Whether the size is accepted for a player skin |
GD stores alpha with 7 bits, so a semi-transparent alpha value can change by one step on a round trip.
--------------------------------------------------|---------------------------------------------|
| toSkinImage(GdImage $image) : SkinImage | Convert a GD image (it is not destroyed) |
| toSkinImageFromFile(string $filepath) : SkinImage | Read a PNG file |
| toPng(SkinImage $skinImage) : GdImage | Convert to a true color GD image with alpha |
| toPngFromSkinData(SkinData $skinData) : GdImage| Convert the skin image of a SkinData |
๐งช Development
composer install --ignore-platform-req='ext-*' composer test
๐ License
Distributed under the MIT License. See LICENSE for more information.