使用Minecraft类进行全身预览

时间:2012-02-28 13:55:21

标签: php minecraft

我已经在PHP中开发了一个Minecraft类,最近添加的功能之一是皮肤预览,但我在制作全身预览的代码时遇到了一些麻烦(头部预览工作正常),我用于头部预览的代码如下......

header('Content-Type: image/png');
$canvas = imagecreatetruecolor($size, $size);
$image = imagecreatefrompng($this->custom_skin($username));
imagecopyresampled($canvas, $image, 0, 0, 8, 8, $size, $size, 8, 8);
return imagepng($canvas);

https://github.com/nblackburn/minecraft/blob/master/class.minecraft.php

任何贡献对于让这个全身预览工作都是一个巨大的帮助。

1 个答案:

答案 0 :(得分:2)

查看此皮肤模板http://www.minecraftwiki.net/images/5/54/Skintemplate.png

基于该区域应该是:

head        x8 y8 w8 h8 
body front  x20 y20 w8 h12
arm front   x44 y20 w4 h12
leg front   x4 y20 w4 h12

您可能需要翻转手臂和腿部,以便左右镜像。

header('Content-Type: image/png');

$scale = $size / 16;
$canvas = imagecreatetruecolor(16*$scale, 32*$scale);
$image = imagecreatefrompng($this->custom_skin($username));

imagealphablending($canvas, false);
imagesavealpha($canvas,true);
$transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
imagefilledrectangle($canvas, 0, 0, 16*$scale, 32*$scale, $transparent);

imagecopyresized($canvas, $image, 4*$scale,  0*$scale,  8,   8,   8*$scale,  8*$scale,  8,  8);  //head
imagecopyresized($canvas, $image, 4*$scale,  8*$scale,  20,  20,  8*$scale,  12*$scale, 8,  12); //body
imagecopyresized($canvas, $image, 0*$scale,  8*$scale,  44,  20,  4*$scale,  12*$scale, 4,  12); //arm left
imagecopyresampled($canvas, $image, 12*$scale, 8*$scale,  47,  20,  4*$scale,  12*$scale, -4,  12); //arm right (flipped)
imagecopyresized($canvas, $image, 4*$scale,  20*$scale, 4,   20,  4*$scale,  12*$scale, 4,  12); //leg left
imagecopyresampled($canvas, $image, 8*$scale,  20*$scale, 7,   20,  4*$scale,  12*$scale, -4,  12); //leg right (flipped)

return imagepng($canvas);

以上只是记忆,但它应该指向正确的方向。在升级时不应使用imagecopyresampled,因为在这种情况下你要避免插值。