Minecraft Player Face Rendering

Written something you are proud of, post it here.
Post Reply
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Minecraft Player Face Rendering

Post by jacek »

This is actually really simple to do and can make a impressive auto-avatar to your website :)

[syntax=php]header('Content-Type: image/png');

$player_name = 'wide_load';
$width = 200;
$height = 200;

$cache_file = "cache/player_skins/faces/{$player_name}_{$width}x{$height}.png";

if (!file_exists($cache_file) || time() - filemtime($cache_file) > 86400){
$skin = imagecreatefromstring(file_get_contents("http://skins.minecraft.net/MinecraftSkins/{$player_name}.png"));

$face = imagecreatetruecolor($width, $height);

imagecopyresized($face, $skin, 0, 0, 8, 8, $width, $height, 8, 8);
imagecopyresized($face, $skin, 0, 0, 40, 8, $width, $height, 8, 8);

imagepng($face, $cache_file);
}

readfile($cache_file);[/syntax]

It seems pretty self-explanatory, feel free to ask questions :)
Last edited by Helx on Thu Mar 28, 2013 9:45 am, edited 1 time in total.
Reason: Updated to play nice with migrated accounts (there's a new URL for skins)
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Minecraft Player Face Rendering

Post by Helx »

How would I use this in a MineQuery player list (like massivecraft.com)?

Sorry, I've just never used this kind of thing with an array :P
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

You would just need to change the player_name so the name of the player you want. ;)
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Minecraft Player Face Rendering

Post by Helx »

jacek wrote:You would just need to change the player_name so the name of the player you want. ;)


Ah, thanks :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

It would make the most sense to put the above code in a separate file and use a $_GET to pass in the player name

[syntax=php]$player_name = $_GET['player_name'];[/syntax]
Then you can use the script in a img tag

[syntax=xhtml]<img src="the_script.php?player_name=wide_load" alt="" />[/syntax]
then it's fully dynamic.
Image
Silent79
Posts: 2
Joined: Thu Jun 21, 2012 6:32 pm

Re: Minecraft Player Face Rendering

Post by Silent79 »

Would it be to much trouble to ask you to explain or possibly make a video on how to put this into the Minequery script you made?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

Silent79 wrote:Would it be to much trouble to ask you to explain or possibly make a video on how to put this into the Minequery script you made?

It sort of explained above :S What don't you understand ?
Image
Silent79
Posts: 2
Joined: Thu Jun 21, 2012 6:32 pm

Re: Minecraft Player Face Rendering

Post by Silent79 »

Sorry .. new to PHP ... I have been playing around with it. I think I have it figured out. Trial and error ... :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

Okay :) You can always post in the support section if you come up with a specific question :D
Image
someonetech
Posts: 1
Joined: Fri Feb 10, 2012 5:21 pm

Re: Minecraft Player Face Rendering

Post by someonetech »

screenshot of logged in user faces
screenshot of logged in user faces
Screen Shot 2012-06-27 at 4.23.31 PM.png (188.94 KiB) Viewed 9987 times
This is totally cool...

I followed the previous tutorial(s) you did on Minequery and had incorporated that into the site I host of a Minecraft server. I was displaying the list of logged in users via [playerList], and used this code (placed inside mcface.php) to display the face instead!

[syntax=php]
foreach ($info[playerList] as $player) {
echo "<img width=\"100\" height=\"100\" src=\"/mcface.php?player_name=" . $player . "\" title=\"{$player}\" />&nbsp;";
}[/syntax]

I also incorporated this into the user profile system to auto create an avatar for any user that logs into our Minecraft server since this code creates the image, I just check if it exists and if so it displays as the users avatar. They still have an options to upload their own avatar, it's pretty neat I think...

Oh and some of the players kept asking to create a face for them to use on other sites, so I setup this form:

http://nightrebellion.com/face

It uses the same code and allows the user to generate the face and click to download...

Thanks for sharing :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

Looks good ! That is pretty much what I use it for too http://somethingcraft.co.uk/player_prof ... =wide_load 8-)
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Minecraft Player Face Rendering

Post by Helx »

jacek wrote:Looks good ! That is pretty much what I use it for too http://somethingcraft.co.uk/player_prof ... =wide_load 8-)


May I ask how you did the MOB/player kills part? I understand that you probably used LogBlock for the blocks (because I have done that, http://s.stratuscraft.net/lb/?player=abcedea)

I know LogBlock has a player death logger, with what killed you/you killed. But I have no idea how to use that...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

abcedea wrote:May I ask how you did the MOB/player kills part? I understand that you probably used LogBlock for the blocks (because I have done that, http://s.stratuscraft.net/lb/?player=abcedea)

I know LogBlock has a player death logger, with what killed you/you killed. But I have no idea how to use that...

I found LogBlock to be a bit too slow for that kinds of stats, my database has over 8 million entries in it and summing all of those took a good few seconds. It's a custom plugin https://github.com/betterphp/PlayerStats
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Minecraft Player Face Rendering

Post by Helx »

jacek wrote:It's a custom plugin https://github.com/betterphp/PlayerStats


Ah, ok. I'll take a look at it. :)

(That explains why that webpage loaded so fast XD)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

abcedea wrote:(That explains why that webpage loaded so fast XD)

:lol: exactly ! Since we have 8 worlds on the server it used to take a good 30 seconds sometimes to load !
Image
jobud9
Posts: 2
Joined: Thu Jan 12, 2012 11:22 pm

Re: Minecraft Player Face Rendering

Post by jobud9 »

jacek wrote:Looks good ! That is pretty much what I use it for too http://somethingcraft.co.uk/player_prof ... =wide_load 8-)

Sorry to bring back an old thread, but how the hell did you break Air blocks!?!?!

http://i.imgur.com/APYmk.png
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Minecraft Player Face Rendering

Post by Helx »

jobud9 wrote:how the hell did you break Air blocks!?!?!


I would imagine superpick or some kind of black magic :/
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

abcedea wrote:
jobud9 wrote:how the hell did you break Air blocks!?!?!


I would imagine superpick or some kind of black magic :/

Yep, or bugs. Or I think placing water buckets. I should filter them out really ;)
Image
Brotaoski
Posts: 1
Joined: Mon Jan 21, 2013 3:52 am

Re: Minecraft Player Face Rendering

Post by Brotaoski »

Oh boy, sorry for reviving an old thread but I could use some help with this script. Im doing my best with my very very very basic understanding of php to incorporate this in to my sites CMS(php-fusion) for users auto avatars. Also excuse me if I misuse the terminology here. Upon registering a user has to input their username in order to register. So I have your code. [syntax=php]<?php
header('Content-Type: image/png');

$user_minecraft = 'wide_load';
$width = 100;
$height = 100;

$cache_file = "player_skins/{$user_minecraft}_{$width}x{$height}.png";

if (!file_exists($cache_file) || time() - filemtime($cache_file) > 86400){
$skin = imagecreatefromstring(file_get_contents("http://s3.amazonaws.com/MinecraftSkins/{$user_minecraft}.png"));

$face = imagecreatetruecolor($width, $height);

imagecopyresized($face, $skin, 0, 0, 8, 8, $width, $height, 8, 8);
imagecopyresized($face, $skin, 0, 0, 40, 8, $width, $height, 8, 8);

imagepng($face, $cache_file);
}

readfile($cache_file);
?>[/syntax]
My issue here is that 'wide_load' needs to call from the db query for a players username
The dq query for this is [syntax=php]".$data['user_minecraft'].">[/syntax]

I need to somehow make these files work with each other so that I can replace all the avatar code to this.
Heres what im using to test it at least [syntax=php]echo "<img src=h.../mcavatar.php?player_name=".$data['user_minecraft'].">\n";[/syntax] The script runs fine and will load the player face of "wide_load"

In addition I also need a if/else for an invalid username say its unable to load an image then else it would load like a noavatar.png image.
I hope you understand what im trying to get it as im having trouble explaining this without knowing all the terms. Thanks in advance!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Minecraft Player Face Rendering

Post by jacek »

If you have ?player_name=something in the URL you can get that value in the code using $_GET so for this it would be $_GET['player_name']

So just

[syntax=php]$user_minecraft = $_GET['player'_name'];[/syntax]
should do it.
Image
Post Reply