Need PHP Help - Minecraft to Minetest Skin Converter

Post Reply
User avatar
TheReaperKing
Member
Posts: 531
Joined: Sun Nov 22, 2015 21:36
Contact:

Need PHP Help - Minecraft to Minetest Skin Converter

by TheReaperKing » Post

So I'm using RobbieF's code for part of it and I think I ALMOST having it working because it does successfully upload the image, I just need it to convert the uploaded image and put it in the covertedskins folder. Maybe some check too so that it doesn't erase a preexisting image already in coverted skins. Thanks for any help!

Code: Select all

<!DOCTYPE html>
<html>
<body>

<form action="minecrafttominetestskinconverter.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>



<?php
// File Uploader

$target_dir = "skinuploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

 // Convert Minecraft 1.8+ skin to 1.7-/Minetest skin.
 // From Robbie Ferguson // www.baldnerd.com
 // 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

$newskin = basename($_FILES["fileToUpload"]["name"])

$input = './skinuploads/"fileToUpload"'; // your 1.8 skin file
$output = './convertedskins/"fileToUpload"'; // 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');
 imagepng($dest);

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

 // Free up memory
 imagedestroy($dest);
 imagedestroy($src);
 
?>
Become A Real Life Superhero - http://SuperheroHill.com
Project Lead of the Doom 3 Mod Last Man Standing - http://Doom3Coop.com
Project Lead of Platinum Arts Sandbox Free 3D Game Maker - http://SandboxGameMaker.com
Youtube Channel - https://www.youtube.com/user/PlatinumArtsKids

User avatar
MangleFox70
Member
Posts: 62
Joined: Mon Feb 01, 2016 14:50
GitHub: Cat5TV
IRC: MangleFox70
In-game: MangleFox70
Contact:

Re: Need PHP Help - Minecraft to Minetest Skin Converter

by MangleFox70 » Post

Here's how we do it, in case it helps you out... and now, I must review for any security holes - lol!

Don't use it verbatim (as it likely won't work for you as is -- you're missing some of our components), but glean ideas or help from it if you like.

Code: Select all

<?php
  require_once('./includes/header.php');

    $gallery['folder'] = '/home/robbie/minetest.tv/skins/uploads/';

    if(isset($_FILES['file']) && strlen(trim(strip_tags($_POST['username']))) > 0 ) {
      $username = trim(strip_tags($_POST['username']));
      $target_dir = '/tmp/';
      $target_file = $target_dir . basename($_FILES['file']['name']);
      $uploadOk = 1;
      $extension = pathinfo($target_file,PATHINFO_EXTENSION);
      if ($extension == 'png') {
        // Check if image file is a actual image or fake image
          $check = getimagesize($_FILES['file']['tmp_name']);
          if($check !== false) {

              $input = $_FILES['file']['tmp_name'];
              $skin['filename'] = 'player_' . $username . '.png';
              $skin['datestamp'] = date('U'); // fake it  :)
              $skin['username'] = $username;
              $output = $gallery['folder'] . $skin['filename'];
              if (!file_exists($output)) {
                // 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');
                //imagepng($dest);

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

                // Free up memory
                imagedestroy($dest);
                imagedestroy($src);
                
                $uploadOk = 1;
              } else {
                $error = 'That user already has a skin. If you\'d like to overwrite it, please contact us.';
                $uploadOk = 0;
              }
          } else {
              $error = 'Invalid File.';
              $uploadOk = 0;
              unlink($_FILES['file']['tmp_name']);
          }
      } else {
        $error = 'Invalid File.';
      }
    }


?>
    <div class="container">
        <div class="col-sm-12">
              <?= responsivead(1,0) ?>
            <div class="caption text-center">
                <p>Advertisement</p>
            </div>
        </div>
    </div>

    <div class="container">

            <div class="col-md-8">
                <form class="sky-form" method="post" enctype="multipart/form-data">
                    <header>Skin Converter</header>
                    <p>Convert your Minecraft 1.8 Skin into Minecraft 1.6/1.7/Minetest format</p>
                    <p>Please login to our server first to ensure your name is available and create your account, then convert your skin using the exact same name you use to login (case-sensitive). Your new skin will be available immediately in the <span class="text-highlights text-highlights-green rounded"><a href="./skins.php">Minetest Skin Gallery</a></span> for you to download to use on any Minetest server (or your own local games) and will also be automatically synced to your account on our servers within a couple hours.</p>
                    <p>Don't have a skin? Try downloading or creating one via <span class="text-highlights text-highlights-green rounded"><a href="http://www.minecraftskins.com/" target="_blank">The Skindex</a></span> and then use our system to convert and import it to Minetest.</p>
                    <p><b>Please Note:</b> Your skin is your visual identity to other players on our servers. Plan out your skin and make sure you're happy with it before you use the converter as you cannot change it once it is synced. That said, if you experience a problem or require help, please contact an admin in-game.</p>
                    <?php
                    if ($uploadOk == 1) {
                        $gallery['output'] = '';
                        $gallery['output'] .= '<div class="container team-v1 content-sm">
                            <div class="margin-bottom-40 text-center">
                                <h2 class="title-v2 title-center">YOUR CONVERTED SKIN</h2>
                                <p>All done!</p>
                            </div>

                            <ul class="list-unstyled row">';
                            $gallery['output'] .= '<li class="col-sm-2 col-xs-4 md-margin-bottom-30">
                                    <div class="team-img text-center">
                                        <img class="img-responsive" style="margin: 0 auto;" src="./skins/preview/front/' . $skin['username'] . '.png" alt="">
                                        <ul>
                                            <li class="text-center"><a target="_blank" href="/skins/uploads/' . $skin['filename'] . '"><i class="icon-custom icon-sm rounded-x fa fa-download"></i></a></li>
                                        </ul>
                                    </div>
                                    <h3 class="text-center">' . $skin['username'] . '</h3>
                                    <h4 class="text-center">' . date('F j, Y', $skin['datestamp']) . '</h4>
                                </li>' . PHP_EOL;
                          
                        $gallery['output'] .= '</ul></div>';
                        echo $gallery['output'];
                      }
                      if (isset($error)) {
                        echo '<p class="bg-danger">' . $error . '</p>';
                      }                      
                    ?>
                    <fieldset>
                        <section style="display:none;">
                            <label class="label">Convert and...</label>
                            <div class="row">
                              <div class="col col-6">
                                <h1>Free Options</h1>
                                <label class="checkbox"><input type="checkbox" name="checkbox" checked disabled><i></i>Show Sample In Skin Gallery</label>
                                <label class="checkbox"><input type="checkbox" name="checkbox" checked><i></i>Allow Other Users to Download Skin File</label>
                                <label class="checkbox"><input type="checkbox" name="checkbox"><i></i>Sync To My Minetest.TV Player Account</label>
                              </div>
                              <div class="col col-6">
                                <h1>Paid Options (We Accept PayPal)</h1>
                                <label class="checkbox"><input type="checkbox" name="checkbox"><i></i>Output HD Skin ($1)</label>
                                <label class="checkbox"><input type="checkbox" name="checkbox"><i></i>Give Me a JPG Too ($1)</label>
                              </div>
                              
                            </div>
                        </section>

                        <section>
                            <label class="label">Your Username</label>
                            <label class="input">
                                <input type="text" name="username">
                            </label>
                        </section>

                        <section>
                            <label class="label">File input</label>
                            <label for="file" class="input input-file">
                                <div class="button"><input type="file" id="file" onchange="this.parentNode.nextSibling.value = this.value" name="file">Browse</div><input type="text" readonly>
                            </label>
                        </section>
                    </fieldset>

                    <footer>
                        <button type="submit" class="btn-u">Submit</button>
                        <button type="button" class="btn-u btn-u-default" onclick="window.history.back();">Back</button>
                    </footer>
                    
                </form>

            </div>
            <div class="col-md-4">
              <?= responsivead(3,0) ?>
            </div>

    </div>

    <div class="container">
        <div class="col-sm-12">
              <?= responsivead(2,0) ?>
            <div class="caption text-center">
                <p>Advertisement</p>
            </div>
        </div>
    </div>
<?php
  
  echo responsivead(0,1); // initialize the page's ads
  require_once('./includes/footer.php');
?>
Of course, "./skins/preview/front/' . $skin['username'] . '.png" doesn't actually exist. It's a render created by another script, which I'm also happy to share if you want. Perhaps I could add this stuff to GitHub.

Robbie
#ThePixelShadow on YouTube
A Weekly Minetest Webcast hosted by MangleFox70
Have a mod you'd like featured? Want to participate in our show? Join servers.minetest.tv Port 30000 for creative or 30001 for survival.
Upload your own skin via our web site: Minetest.TV

User avatar
TheReaperKing
Member
Posts: 531
Joined: Sun Nov 22, 2015 21:36
Contact:

Re: Need PHP Help - Minecraft to Minetest Skin Converter

by TheReaperKing » Post

This is awesome, thank you!! Since I know so little about PHP all of this helps me "learn the rules" of the language :)
Become A Real Life Superhero - http://SuperheroHill.com
Project Lead of the Doom 3 Mod Last Man Standing - http://Doom3Coop.com
Project Lead of Platinum Arts Sandbox Free 3D Game Maker - http://SandboxGameMaker.com
Youtube Channel - https://www.youtube.com/user/PlatinumArtsKids

User avatar
MangleFox70
Member
Posts: 62
Joined: Mon Feb 01, 2016 14:50
GitHub: Cat5TV
IRC: MangleFox70
In-game: MangleFox70
Contact:

Re: Need PHP Help - Minecraft to Minetest Skin Converter

by MangleFox70 » Post

Just remember it was never meant to be released verbatim... so you'll need to tweak it (it was built for our servers only). But surely it gives you some insight.

Good luck!
#ThePixelShadow on YouTube
A Weekly Minetest Webcast hosted by MangleFox70
Have a mod you'd like featured? Want to participate in our show? Join servers.minetest.tv Port 30000 for creative or 30001 for survival.
Upload your own skin via our web site: Minetest.TV

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests