Convert Minecraft 1.8+ Skin to 1.6/1.7/Minetest Skin in PHP

RobbieF's Minetest SkinAs we build up #ThePixelShadow on Category5 TV, and introduce a creative Minetest server specifically for playing Minetest (the free Minecraft alternative), it became apparent that our users/viewers would like to be able to have their own custom skins.

We’re making it easy with a nice little interface to upload your own skins, but part of the process requires making a skin which is compatible with sdzen’s/PilzAdam’s player_textures mod … basically, these skins are Minecraft 1.6/1.7 skins… 64×32. Great Minecraft skin creator sites such as now generate Minecraft 1.8 skin files, which are 64×64.

The difference is essentially that the skins now support overlays (eg., removable headphones or glasses) and your left and right arms and legs can have different textures. Not the case with 1.6/1.7/Minetest… so we must convert the skin file to make it compatible.

Since we’re building a web interface to do this all automatically for you and place your player skin on our server automatically, I’m building the program in PHP. Since there are a lot of tutorials out there that simply instruct you to change your canvas size to 64×32 (which is wrong – you will lose your overlays!) I thought I would share my method with you in case it comes in handy.

And hey, it’s a fun exercise in PHP/GD anyways  🙂

 // Convert Minecraft 1.8+ skin to 1.7-/Minetest skin.
 // From Robbie Ferguson //
 // Requires PHP, GD
 // By default outputs png to browser window AND saves a file for future use. Edit below to change behaviour.
 // v1.1

 $input = './uploads/player_RobbieF.png'; // your 1.8 skin file
 $output = 'newskin.png'; // your new 1.7/Minetest skin file

 // Create image instances
 $src = imagecreatefrompng($input);
 $dest = imagecreatetruecolor(64, 32);

 // Make it transparent
 imagesavealpha($dest, true);
 $trans_colour = imagecolorallocatealpha($dest, 0, 0, 0, 127);
 imagefill($dest, 0, 0, $trans_colour);

 // Learn the dimensions of the input image
 $size = getimagesize($input);

 if ($size[0] == 64 && $size[1] == 64) { // it has Minecraft 1.8 skin dimensions - convert!
 // Copy - Syntax is Dest X,Y, Source X,Y, Width,Height

 // Head
 imagecopy($dest, $src, 0,0, 0,0, 32,16);
 // Head overlay
 imagecopy($dest, $src, 0,0, 32,0, 32,16);

 // Right leg, Body, Right Arm
 // The Leg and Arm become both left and right in 1.7-
 // We'll simply discard the left arm and leg since it's not used.
 // If you have an overlay on your left arm/leg but not right arm/leg, you might want to edit your skin since that will be discarded.
 imagecopy($dest, $src, 0,16, 0,16, 64,16);

 // Leg, Body and Arm overlay
 imagecopy($dest, $src, 0,16, 0,32, 64,16);

 } else { // already compatible. Just copy it.
 imagecopy($dest, $src, 0,0, 0,0, 64,32);

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

 // Save to a file
 imagepng($dest,$output,0); // 0-9. 0=faster, 9=smaller.

 // Free up memory

If you find a good use for it in your project, please comment below. If you really love what I do, please consider supporting my Patreon profile, or throw a little something in the tip jar.

Hope to see you on #ThePixelShadow Minetest server soon, custom skin and all!


Convert numbers to words the easy way with PHP

Sometimes we want words rather than numbers, but it used to be a very onerous task to do this. Since PHP 5.3.0 however, the NumberFormatter Class was introduced, allowing us to do this conversion quickly, with a single line of code.

A good example of the need for a this would be a business web site that says “We’ve been in business for 18 years.” To keep the site current, they’re doing echo ‘We\’ve been in business for ‘ . (date(‘Y’) – 1997) . ‘ years.’; It would look much better to say “We’ve been in business for eighteen years. This bit of code will do that for you.

Search Goblin Number to Words in PHP Screenshot

With the new Number to Words in PHP system at Search Goblin (my little helper script site), you can enter any number and the script will be demonstrated for you, converting your number to plain text. The code is provided there so you can start using this technique on your own site.

Check it out:

Convert video to several JPG images on Linux without ffmpeg.

These days I just use this command and hit CTRL-C when the video frames (V:) stop moving:

mplayer -vo jpeg:outdir=screenshots -sstep 10 filename.mp4

But, this post remains for the sake of historical record – lol!

I admit… I do love PHP in the command line. Does that make me a bad person? 😉

Here’s a tiny little script that I wrote to create many JPG screenshots of a video file. I use this each week to create a bunch of stills from our broadcast so I can use them as thumbnails and so-on. I didn’t want it to depend on ffmpeg since I don’t have that on any of my modern systems.

It requires just three packages: mplayer mediainfo php-5

Save it as whatever.php and run it like this: php whatever.php file.wmv

It will create a folder called file-Screenshots/ and will save one picture per 10 seconds for any video source. Just change “file.wmv” to the name of your video. Include the path if it’s not in the current folder.

  // Depends: mplayer mediainfo
  // Does not need ffmpeg (deprecated)

  if ($_GET) {
    $file = $_GET['file'];
  } else {
    $file = $argv[1];
  if (strlen($file) < 3) exit('Need a proper filename for input.' . PHP_EOL);  
  $dir = array_shift(explode('.',$file)) . '-Screenshots';

  $duration = duration($file);
  echo 'Duration in Seconds: ' . $duration . PHP_EOL;
  echo 'Saving to folder:    ' . $dir . PHP_EOL;
  echo 'Creating ' . ($duration/10) . ' JPG images from source...';
  exec('mplayer -vo jpeg:outdir=' . $dir . ' -sstep 10 -endpos ' . ($duration-2) . ' ' . $file . ' > /dev/null 2>&1');
  echo ' Done.' . PHP_EOL; 

  function duration($file) {
    if (file_exists($file)) {
      exec('mediainfo -Inform="Audio;%ID%:%Format%:%Language/String%\n" ' . $file . ' | grep -m1 Duration | cut -d\':\' -f2',$result);
      $tmp = explode('h',$result[0]);
      $seconds = ((intval($tmp[0]*60)+intval($tmp[1]))*60);
      return intval(trim($seconds));
    } else {
      exit('File ' . $file . ' not found.' . PHP_EOL);

Hope it helps you out.


Automated cache-buster on images in PHP

I have a particular site I manage where one particular image (a grid of sponsors) gets updated quite regularly.

Rather than edit my source code each time I upload a new image, I thought I’d let PHP do the work for me.

<img class="img-responsive" src="images/sponsors/silver.jpg?<?= date('U',filemtime('images/sponsors/silver.jpg')) ?>" />

Now, every time I upload a new image, replacing silver.jpg, it will automatically update the image in the users’ cache.

Just a silly little time saver.

Note: I wouldn’t do this on every image on a site since it means an extra hit to the filesystem. That could mean a performance drop if a site is checking the filemtime of 100 images. In my case, it’s just a single image, so it’s okay.

Unify Theme ERROR! on CAPTCHA form.

For the life of me, I couldn’t figure out why the Sky Forms CAPTCHA was showing ERROR! on my Unify Theme Bootstrap 3 deployment.

Turns out this was just a rookie mistake… I wasn’t looking closely enough at the “how it works” and I was missing some code from the demo-contacts.php sample file that was crucial to the operation of the CAPTCHA. This code generates the CAPTCHA itself and stores it in SESSION data. Since it was missing, the CAPTCHA system’s image.php was turning out an ERROR!

// Make the page validate
ini_set('session.use_trans_sid', '0');

// Create a random string, leaving out 'o' to avoid confusion with '0'
$char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 4));

// Concatenate the random string onto the random numbers
// The font 'Anorexia' doesn't have a character for '8', so the numbers will only go up to 7
// '0' is left out to avoid confusion with 'O'
$str = rand(1, 7) . rand(1, 7) . $char;

// Begin the session

// Set the session contents
$_SESSION['captcha_id'] = $str;

That $_SESSION[‘captcha_id’] is what the image.php file is looking for. If it doesn’t find it, ERROR!

Refresh, and we’re good to go!


Running phpcs against many domains to test PHP5 Compatibility.

Running a shared hosting service (or otherwise having a ton of web sites hosted on the same server) can pose challenges when it comes to upgrading.  What’s going to happen if you upgrade something to do with the web server, and it breaks a bunch of sites?

That’s what I ran into this week.

For security reasons, we needed to knock PHP4 off our Apache server and force all users onto PHP5.

But a quick test showed us that this broke a number of older sites (especially sites running on old code for things like OS Commerce or Joomla).

I can’t possibly scan through billions of lines of client code to see if their site will work or break, nor can I click every link and test everything after upgrading them to PHP5.

So automation takes over, and we look at PHP_CodeSniffer with the PHPCompatibility standard installed.

Making it work was a bit of a pain in the first place, and you’ll need some know-how to get it to go.  There are inconsistencies in the documentation and even some incorrect instruction on getting it running.  However, a good place to start is…..

Running the command on a specific folder (eg. phpcs –extensions=php –standard=PHP53Compat /home/myuser/domains/ works great.  But as soon as you decide to try to run it through many, many domains, it craps out.  Literally just hangs.  But usually not until it’s been running for a few hours, so what a waste of time.

So I wrote a quick script to help with this issue.  It (in its existing form – feel free to mash it up to suit your needs) first generates a list of all public_html and private_html folders recursive to your /home folder.  It then runs phpcs against everything it finds, but does it one site at a time (so no hanging).

I suggest you run phpcs against one domain first to ensure that you have phpcs and the PHPCompatibility standard installed and configured correctly.  Once you’ve successfully tested it, then use this script to automate the scanning process.

You can run the script from anywhere, but it must have a tmp and results folder within the current folder.

mkdir /scanphp
cd /scanphp
mkdir tmp
mkdir results

And then place the PHP file in /scanphp and run it like this:
php myfile.php (or whatever you ended up calling it)

Remember, this script is to be run through a terminal session, not in a browser.

  exec('find /home -type d -iname \'public_html\' > tmp/public_html');
  exec('find /home -type d -iname \'private_html\' > tmp/private_html');
  $public_html = file('tmp/public_html');
  $private_html = file('tmp/private_html');

  foreach ($public_html as $folder) {
    $tmp = explode('/', $folder);
    $domain = $tmp[(count($tmp)-2)];
    if ($domain == '.htpasswd' || $domain == 'public_html') $domain = $tmp[(count($tmp)-3)];
    $user = $tmp[2];
    echo 'Running scan: ' . $folder . $user . '->' . $domain . '... ';
    exec('echo "Scan Results for ' . $folder . '" >> results/' . $user . '_' . $domain . '.log');
    exec('phpcs --extensions=php --standard=PHP53Compat ' . $folder . ' >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    echo 'Done.' . PHP_EOL . PHP_EOL;

  foreach ($private_html as $folder) {
    $tmp = explode('/', $folder);
    $domain = $tmp[(count($tmp)-2)];
    if ($domain == '.htpasswd' || $domain == 'private_html') $domain = $tmp[(count($tmp)-3)];
    $user = $tmp[2];
    echo 'Running scan: ' . $folder . $user . '->' . $domain . '... ';
    exec('echo "Scan Results for ' . $folder . '" >> results/' . $user . '_' . $domain . '.log');
    exec('phpcs --extensions=php --standard=PHP53Compat ' . $folder . ' >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    echo 'Done.' . PHP_EOL . PHP_EOL;


See what we’re doing there?  Easy breezy, and solves the problem when having to run phpcs against a massive number of domains.

Let me know if it helped!

– Robbie

Walk-in Wifi Responder

Had a thought this morning that wifi could be used to do some pretty rad stuff… like detecting when I get home by noticing my iPod touch.

Since most of us carry wifi-enabled devices with us at all times, and most of us have them set to auto-connect once in range of our routers, I thought, why not use that data?  It could be as simple as logging coming and going, or as sophisticated as automatically turning on my favorite music when I walk in the door.  Or even adjusting the thermostat when I arrive home to save energy when nobody is around.

As a very brief proof of concept I whipped out a simple algorithm in PHP which can be run from any Linux computer on your network.

Usage:  php wifi-check.php –device=devicename

 0) {
      $tmp=@explode('=', $ping);
      $result=@explode('/', trim($tmp[1]));
    if (count($result) > 0) {
      // Now we know the device is connected; do something
      echo 'Device active.' . PHP_EOL;
    } else {
      // Device is not connected.
      echo 'Device inactive.' . PHP_EOL;
  } else {
    echo 'Usage: php wifi-check.php --device=devicename' . PHP_EOL;
} else {
  echo 'This script is designed to be run from the Linux terminal, not a browser.';

My thinking is to put something like this in a looping script and let it run every so many seconds or something, calling particular functions if the device is detected as active vs inactive.

I’d welcome your thoughts in the comment section below.  What practical things could this be used for?