Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //
- // The first thing I did was clean up your code structure. You had a lot of unnecessary spaces and newlines
- // Right now, code readability is key!
- //
- function fetchInfo($playerGamertag) {
- $key = "********************************************";
- $basic = file_get_contents("http://www.bungie.net/api/reach/reachapijson.svc/player/details/nostats/" . $key . "/" . rawurlencode($playerGamertag));
- $map = file_get_contents("http://www.bungie.net/api/reach/reachapijson.svc/player/details/bymap/" . $key . "/" . rawurlencode($playerGamertag));
- $json = json_decode($basic, 1);
- $json_map = json_decode($map, 1); // There was no reason that this needed to be an object while the other was an array.
- unset($basic, $map, $key);
- $emblem = $json['Player']['ReachEmblem'];
- $colors = $emblem['change_colors'];
- $emblemUrl = sprintf("http://www.bungie.net/Stats/emblem.ashx?s=70&0=%s&1=%s&2=%s&3=%s&fi=%s&bi=%s&fl=%s&m=3",
- $colors[0],
- $colors[1],
- $colors[2],
- $colors[3],
- $emblem['foreground_index'],
- $emblem['background_index'],
- ($emblem['flags'] + 1));
- foreach($json_map['StatisticsByMap'] as $stats) { // Updated to use the array instead of the object.
- if($stats['MapId'] == "-1") {
- if($stats['VariantClass'] == "1" || $stats['VariantClass'] == "2" || $stats['VariantClass'] == "3") {
- $totalkills += $stats['total_kills']; //
- $totaldeaths += $stats['total_deaths']; //
- $totalbetrayals += $stats['total_betrayals']; // $variable = $variable + $newvariable is the same thing as $variable += $newvariable, so I changed that.
- $totalassists += $stats['total_assists']; //
- $mvp += $stats['total_first_place']; //
- }
- }
- }
- $cache[] = time();
- $cache[] = $json['Player']['gamertag'];
- $cache[] = $json['Player']['service_tag'];
- $cache[] = $json['Player']['games_total'];
- $cache[] = $json['Player']['CampaignProgressSp'];
- $cache[] = $json['CurrentRankIndex'];
- $cache[] = $totalkills;
- $cache[] = $totaldeaths;
- $cache[] = $totalbetrayals;
- $cache[] = $totalassists;
- $cache[] = $mvp;
- $cache[] = $emblemUrl;
- return $cache;
- }
- function generateImage($cache) {
- $sig = imagecreatefrompng("images/sig_glass.png");
- $getEmblem = file_get_contents($cache[11]);
- $sig_emblem = imagecreatefromstring($getEmblem);
- $sig_solo = imagecreatefrompng("images/solo/" . $cache[4] . ".png");
- $sig_rank = imagecreatefrompng("images/ranks/" . $cache[5] . ".png");
- $text_color = imagecolorallocate($sig, 255, 255, 255);
- imagesavealpha($sig, true); // sig_glass.png doesn't have alpha, but I'll leave this in case you update the template sometime.
- imagesavealpha($sig_solo, true);
- imagesavealpha($sig_rank, true);
- imagettftext($sig, 17, 0, 100, 26, $text_color, 'Utsaah.ttf', $cache[1]." - ".$cache[2]); //
- imagettftext($sig, 13, 0, 105, 47, $text_color, 'Utsaah.ttf', "Games Played: ".$cache[3]); //
- imagettftext($sig, 13, 0, 105, 85, $text_color, 'Utsaah.ttf', "Total Assists: ".$cache[9]); //
- imagettftext($sig, 13, 0, 280, 47, $text_color, 'Utsaah.ttf', "Total betrayals: ".$cache[8]); // There's no need to put parentheses around strings containing variables, just do it like I did here
- imagettftext($sig, 13, 0, 105, 67, $text_color, 'Utsaah.ttf', "MVP ".$cache[10]." Time".(($cache[10] == 1) ? "" : "s")); //
- imagettftext($sig, 13, 0, 280, 67, $text_color, 'Utsaah.ttf', "Total Kills: ".$cache[6]); //
- imagettftext($sig, 13, 0, 280, 85, $text_color, 'Utsaah.ttf', "Total Deaths: ".$cache[7]); //
- imagecopyresampled($sig, $sig_emblem, 13, 12, 0, 0, 70, 70, imagesx($sig_emblem), imagesy($sig_emblem)); // I replaced imagecopyresized() with imagecopyresampled(), which retains a lot more quality when shrinking images
- imagecopyresampled($sig, $sig_rank, 517, 39, 0, 0, 30, 30, imagesx($sig_rank), imagesy($sig_rank)); // I also added the imagesx() and imagesy() functions, which get the width and height of a supplied image resource.
- imagecopyresampled($sig, $sig_solo, 429, 38, 0, 0, 30, 30, imagesx($sig_solo), imagesy($sig_solo)); // This makes it so that the whole source image is copied (and sized, if necessary), not just a portion of it.
- header("Content-type: image/png");
- imagepng($sig); // I told you, calling imagepng() without the string filename parameter causes it to output to the browser.
- imagedestroy($sig);
- }
- $playerGamertag = @$_REQUEST['gamertag']; // This sets the gamertag from the POST array
- if(!$playerGamertag) { // This checks that a gamertag was passed
- die("No gamertag supplied"); // If not, kill the script.
- } else if(strlen($playerGamertag) > 15) { // This checks that the gamertag is less than 15 characters long
- die("Invalid gamertag"); // If it is, kill the script.
- }
- // A gamertag was passed that is considered "valid", so we continue.
- $hash = md5($playerGamertag); //
- $cachefile = "cache/" . $hash . ".txt"; // You had these variables copied all over the place, for some reason. You only need them once.
- if(file_exists($cachefile)) {
- $file = file_get_contents($cachefile);
- $cache = explode("#|#", $file);
- unset($file);
- $timeDiff = time() - $cache[0];
- if($timeDiff > 3600) { // Timestamps are in seconds. 300 seconds is 5 minutes, that was just a demonstration from my script, you should use 1 hour, or 3600 seconds
- // The cache file is older than 1 hour
- $cache = fetchInfo($playerGamertag); // You also had this copied all over the place. The more times you call this function, the more you'll hit the API with your key. You were hitting it 4 times per run of this script before
- $str = implode("#|#", $cache);
- file_put_contents($cachefile, $str);
- unset($str);
- //generateImage($playerGamertag);
- } else {
- // The cache existed and was younger than 1 hour
- // Notice how we don't do anything since we already have our $cache variable populated
- }
- } else {
- // The cache didn't exist
- $cache = fetchInfo($playerGamertag);
- $str = implode("#|#", $cache);
- file_put_contents($cachefile, $str);
- unset($str);
- }
- // At this point we have a variable called $cache that contains information either from the text file, or from the API (which was written to the text file)
- generateImage($cache); // Now we generate our image
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement