Search by

presentkim / png-converter

PresentKim

Converter between PNG images (GD) and Minecraft: Bedrock SkinImage

Package info

github.com/presentkim-pm/png-converter

pkg:composer/presentkim/png-converter

Fund package maintenance!

Patreon

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 10

Open Issues: 1

2.1.0 2026-11-27 11:00 UTC

This package is auto-updated.

Last update: 2026-11-27 11:05:29 UTC


README

Packagist CI Stars License


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 / SkinData to a GD image
  • Correct alpha : GD alpha 0..127 (0 is opaque) is mapped to 0..255, so an opaque pixel is 255, not 254
  • 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.44 branch (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.