1

I have developed a class for Minecraft in PHP, one of the most recent additions to the functions is a skin preview but I am having a little trouble working out the codes for the full body preview (head preview works fine), the code I am using for the head preview is as follows...

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

Any contributions would be a huge help to get this full body preview working.

  • 1
    Define what "a little trouble" looks like and how you'd rather see it "working". You can't expect anyone to notice "oh, the second 8 should be a 42" and then it magically works. – CodeCaster Feb 28 '12 at 13:57
  • Well i am unable to work out the right numbers for the imagecopyresampled to render out the chest, arms, legs etc but the head is working perfectly. –  Feb 28 '12 at 13:58
  • 2
    @nblackburn You're copying a small part of the image. Why don't you just show the whole image? What are you trying to do here? – Svish Feb 28 '12 at 14:00
  • Its not that simple when the image looks like this: http://s3.amazonaws.com/MinecraftSkins/nblackburn.png –  Feb 28 '12 at 14:03
  • @nblackburn you simply have to define (or find) the regions that make up the image. Then simply loop through all known regions and extract that part of the image. – CodeCaster Feb 28 '12 at 14:08
  • @CodeCaster thanks, i am not sure how that is done, would you be able to get me started with an example? –  Feb 28 '12 at 14:19

1 Answers1

2

Have a look at this skin template http://www.minecraftwiki.net/images/5/54/Skintemplate.png

Based on that the regions should be:

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

You may need to flip the arm and leg sections in order to get the left and right to be mirrored.

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);

The above is just from memory but it should point you in the right direction. You shouldn't use imagecopyresampled when upscaling since in this case you want to avoid interpolation.

P. Galbraith
  • 2,477
  • 21
  • 24
  • Thanks, this is pretty close but i had to make some tweaks as the legs were a little off. How do i make this image transparent? –  Feb 28 '12 at 15:31
  • To utilize transparency see this http://stackoverflow.com/questions/279236/how-do-i-resize-pngs-with-transparency-in-php#279310 – P. Galbraith Feb 28 '12 at 15:36
  • See this pull request for working version https://github.com/nblackburn/minecraft/pull/1 – P. Galbraith Feb 29 '12 at 12:32