|  
 PHP Steam Badge Generator - Printable Version
 
 -Shoutbox (https://shoutbox.menthix.net)
 +-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
 +--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
 +---- Forum: Tech Talk (/forumdisplay.php?fid=17)
 +----- Thread: PHP Steam Badge Generator (/showthread.php?tid=94032)
 PHP Steam Badge Generator by Jimbo on 03-06-2010 at 03:56 PM
 
Recently, I pieced this together using code from various sources, so to use on my new forum, but for the life of me, I cannot do what I want to do. It works perfectly, if a valid ID is specified, but when an invalid ID, or private profile is used, a blank black rectangle is shown. I have tried to show some text instead, but have failed miserably, so am wondering if anyone could help me out.
 
 x- ugly black box
 
 
 The code I am using here is:
 index.php:x
 scrape.php:x
 
 or, if you prefer, it can be seen below in the spoiler (so not to cluster the page)
 
 Spoiler:   
index.php
 php code:<?php
 include "scrape.php";
 header ("Content-type: image/jpeg");
 ini_set("max_execution_time", "60");
 $userid = $_GET['id'];
 
 $steamCommunityScrape = new SteamInformation($userid);
 $userDetails = $steamCommunityScrape->getData();
 if((60 + (strlen($userDetails['steamID']) * 5.7)) < 170){
 $imageWidth = 170;
 } else {
 $imageWidth = (70 + (strlen($userDetails['steamID']) * 5.7));
 }
 $imageBase = imagecreatetruecolor($imageWidth, 60);
 
 putenv('GDFONTPATH=' . realpath('.'));
 
 if($userDetails['steamID'] && $userDetails['stateMessage']){
 $imageIcons = array(
 "Rating Star" => imagecreatefrompng("img/star.png"),
 "vacBanned" => array(NULL => imagecreatefrompng("img/shield_add.png"), 1 => imagecreatefrompng("img/shield_delete.png")));
 
 $stateOutlines = array(
 "in-game" => imagecreatefrompng("img/ingame.png"),
 "online" => imagecreatefrompng("img/online.png"),
 "offline" => imagecreatefrompng("img/offline.png"));
 
 $fontColors = array(
 "baseColor" => imagecolorallocate($imageBase, 200, 200, 200),
 "in-game" => imagecolorallocate($imageBase, 177, 251, 80),
 "online" => imagecolorallocate($imageBase, 142, 203, 255),
 "offline" => imagecolorallocate($imageBase, 84, 84, 84));
 
 $avatarPosition = array("x" => 15, "y" => 15);
 
 @ImageCopyResampled($imageBase, imagecreatefromjpeg($userDetails['avatarIcon']), $avatarPosition['x'], $avatarPosition['y'], 0, 0, 32, 32, 32, 32);
 @ImageCopyResampled($imageBase, $stateOutlines[$userDetails['onlineState']], $avatarPosition['x'] - 4, $avatarPosition['y'] - 4, 0, 0, 40, 40, 40, 40);
 
 imagettftext($imageBase, 7, 0, 58, 22, $fontColors[$userDetails['onlineState']], "verdanabold.ttf", htmlspecialchars_decode($userDetails['steamID']));
 imagettftext($imageBase, 7, 0, 57, 34, $fontColors[$userDetails['onlineState']], "verdana.ttf", $userDetails['stateMessage']);
 
 @ImageCopyResampled($imageBase, $imageIcons['vacBanned'][$userDetails['vacBanned']], 58, 38, 0, 0, 12, 12, 16, 16);
 
 @ImageCopyResampled($imageBase, $imageIcons['Rating Star'], 72, 38, 0, 0, 12, 12, 16, 16);
 imagettftext($imageBase, 7, 0, 86, 48, $fontColors['baseColor'], "verdanabold.ttf", "x" . round($userDetails['steamRating']));
 
 if ($userDetails['countryCode']){
 @ImageCopyResampled($imageBase, imagecreatefrompng("img/flags/" . strtolower($userDetails['countryCode']) . ".png"), 106, 40, 0, 0, 14, 10, 16, 11);
 }
 
 
 
 
 }
 @imagejpeg ($imageBase, NULL, 100);
 
 @imagedestroy($imageBase);
 @imagedestroy($userAvatar);
 
 
 ?>
 
 
Scrape.php
 php code:<?php
 class SteamInformation {
 private $data = array();
 public function __construct($steamID) {
 $this->info = file_get_contents($this->pickSteamURL($steamID) . "?xml=1&allfriends=1");
 $this->games = file_get_contents($this->pickSteamURL($steamID) . "/games?xml=1");
 $this->populateData();
 }
 
 private function pickSteamURL($userID) {
 if (is_numeric($userID)){
 return "http://steamcommunity.com/profiles/" . $userID;
 } else {
 return "http://steamcommunity.com/id/" . $userID;
 }
 }
 
 private function populateData() {
 $profileInformation = new XmlToArray($this->info);
 $array = $profileInformation->createArray();
 $array = $array['profile'];
 unset($array["groups"]);
 unset($array["favoriteGame"]);
 $this->data = $array;
 
 if ($this->data['location']){
 $this->data['countryCode'] = $this->getCountryCode(trim(end(explode(",", $this->data['location']))));
 $this->data['countryCode'] = $this->data['countryCode'][0];
 }
 
 $profileGames = new XmlToArray($this->games);
 $profileGames = $profileGames->createArray();
 
 if ($profileGames["gamesList"]['games'] != NULL) {
 $this->data["games"] = $profileGames["gamesList"]["games"][0]["game"];
 }
 
 if ($this->data["stateMessage"]){
 $this->data["stateMessage"] = $this->userStatusTrimming($this->data["stateMessage"]);
 }
 
 if (count($this->data["friends"][0]["friend"]) != 0){
 $x = 0;
 foreach($this->data["friends"][0]["friend"] as $friend){
 $this->data["friends"][0]["friend"][$x]["stateMessage"] = $this->userStatusTrimming($this->data["friends"][0]["friend"][$x]["stateMessage"]);
 $x++;
 }
 }
 
 $this->data['friends'] = $this->data['friends'][0]['friend'];
 $this->data['mostPlayedGames'] = $this->data['mostPlayedGames'][0]['mostPlayedGame'];
 if ($this->data['weblinks']){
 $this->data['weblinks'] = $this->data['weblinks'][0]['weblink'];
 }
 }
 
 private function userStatusTrimming($input){
 $input = trim(strip_tags(trim(str_replace("Join", "", str_replace("In-Game", "", $input)))));
 if (substr($input, -1) == "-"){
 $input = trim(substr($input, 0, strlen($input)-1));
 }
 return $input;
 }
 
 function getCountryCode($countryName){
 $countries = array(
 'AF' => 'Afghanistan',
 'AL' => 'Albania',
 'DZ' => 'Algeria',
 'AS' => 'American Samoa',
 'AD' => 'Andorra',
 'AO' => 'Angola',
 'AI' => 'Anguilla',
 'AQ' => 'Antarctica',
 'AG' => 'Antigua and Barbuda',
 'AR' => 'Argentina',
 'AM' => 'Armenia',
 'AW' => 'Aruba',
 'AU' => 'Australia',
 'AT' => 'Austria',
 'AZ' => 'Azerbaijan',
 'BS' => 'Bahamas',
 'BH' => 'Bahrain',
 'BD' => 'Bangladesh',
 'BB' => 'Barbados',
 'BY' => 'Belarus',
 'BE' => 'Belgium',
 'BZ' => 'Belize',
 'BJ' => 'Benin',
 'BM' => 'Bermuda',
 'BT' => 'Bhutan',
 'BO' => 'Bolivia',
 'BA' => 'Bosnia and Herzegovina',
 'BW' => 'Botswana',
 'BV' => 'Bouvet Island',
 'BR' => 'Brazil',
 'IO' => 'British Indian Ocean Territory',
 'BN' => 'Brunei Darussalam',
 'BG' => 'Bulgaria',
 'BF' => 'Burkina Faso',
 'BI' => 'Burundi',
 'KH' => 'Cambodia',
 'CM' => 'Cameroon',
 'CA' => 'Canada',
 'CV' => 'Cape Verde',
 'KY' => 'Cayman Islands',
 'CF' => 'Central African Republic',
 'TD' => 'Chad',
 'CL' => 'Chile',
 'CN' => 'China',
 'CX' => 'Christmas Island',
 'CC' => 'Cocos (Keeling) Islands',
 'CO' => 'Colombia',
 'KM' => 'Comoros',
 'CG' => 'Congo',
 'CD' => 'Congo the Democratic Republic of the',
 'CK' => 'Cook Islands',
 'CR' => 'Costa Rica',
 'CI' => 'Cote D\'Ivoire',
 'HR' => 'Croatia',
 'CU' => 'Cuba',
 'CY' => 'Cyprus',
 'CZ' => 'Czech Republic',
 'DK' => 'Denmark',
 'DJ' => 'Djibouti',
 'DM' => 'Dominica',
 'DO' => 'Dominican Republic',
 'EC' => 'Ecuador',
 'EG' => 'Egypt',
 'SV' => 'El Salvador',
 'GQ' => 'Equatorial Guinea',
 'ER' => 'Eritrea',
 'EE' => 'Estonia',
 'ET' => 'Ethiopia',
 'FK' => 'Falkland Islands (Malvinas)',
 'FO' => 'Faroe Islands',
 'FJ' => 'Fiji',
 'FI' => 'Finland',
 'FR' => 'France',
 'GF' => 'French Guiana',
 'PF' => 'French Polynesia',
 'TF' => 'French Southern Territories',
 'GA' => 'Gabon',
 'GM' => 'Gambia',
 'GE' => 'Georgia',
 'DE' => 'Germany',
 'GH' => 'Ghana',
 'GI' => 'Gibraltar',
 'GR' => 'Greece',
 'GL' => 'Greenland',
 'GD' => 'Grenada',
 'GP' => 'Guadeloupe',
 'GU' => 'Guam',
 'GT' => 'Guatemala',
 'GN' => 'Guinea',
 'GW' => 'Guinea-Bissau',
 'GY' => 'Guyana',
 'HT' => 'Haiti',
 'HM' => 'Heard Island and Mcdonald Islands',
 'VA' => 'Holy See (Vatican City State)',
 'HN' => 'Honduras',
 'HK' => 'Hong Kong',
 'HU' => 'Hungary',
 'IS' => 'Iceland',
 'IN' => 'India',
 'ID' => 'Indonesia',
 'IR' => 'Iran Islamic Republic of',
 'IQ' => 'Iraq',
 'IE' => 'Ireland',
 'IL' => 'Israel',
 'IT' => 'Italy',
 'JM' => 'Jamaica',
 'JP' => 'Japan',
 'JO' => 'Jordan',
 'KZ' => 'Kazakhstan',
 'KE' => 'Kenya',
 'KI' => 'Kiribati',
 'KP' => 'Korea Democratic People\'s Republic of',
 'KR' => 'Korea Republic of',
 'KW' => 'Kuwait',
 'KG' => 'Kyrgyzstan',
 'LA' => 'Lao People\'s Democratic Republic',
 'LV' => 'Latvia',
 'LB' => 'Lebanon',
 'LS' => 'Lesotho',
 'LR' => 'Liberia',
 'LY' => 'Libyan Arab Jamahiriya',
 'LI' => 'Liechtenstein',
 'LT' => 'Lithuania',
 'LU' => 'Luxembourg',
 'MO' => 'Macao',
 'MK' => 'Macedonia the Former Yugoslav Republic of',
 'MG' => 'Madagascar',
 'MW' => 'Malawi',
 'MY' => 'Malaysia',
 'MV' => 'Maldives',
 'ML' => 'Mali',
 'MT' => 'Malta',
 'MH' => 'Marshall Islands',
 'MQ' => 'Martinique',
 'MR' => 'Mauritania',
 'MU' => 'Mauritius',
 'YT' => 'Mayotte',
 'MX' => 'Mexico',
 'FM' => 'Micronesia Federated States of',
 'MD' => 'Moldova Republic of',
 'MC' => 'Monaco',
 'MN' => 'Mongolia',
 'ME' => 'Montenegro',
 'MS' => 'Montserrat',
 'MA' => 'Morocco',
 'MZ' => 'Mozambique',
 'MM' => 'Myanmar',
 'NA' => 'Namibia',
 'NR' => 'Nauru',
 'NP' => 'Nepal',
 'NL' => 'Netherlands',
 'AN' => 'Netherlands Antilles',
 'NC' => 'New Caledonia',
 'NZ' => 'New Zealand',
 'NI' => 'Nicaragua',
 'NE' => 'Niger',
 'NG' => 'Nigeria',
 'NU' => 'Niue',
 'NF' => 'Norfolk Island',
 'MP' => 'Northern Mariana Islands',
 'NO' => 'Norway',
 'OM' => 'Oman',
 'PK' => 'Pakistan',
 'PW' => 'Palau',
 'PS' => 'Palestinian Territory Occupied',
 'PA' => 'Panama',
 'PG' => 'Papua New Guinea',
 'PY' => 'Paraguay',
 'PE' => 'Peru',
 'PH' => 'Philippines',
 'PN' => 'Pitcairn',
 'PL' => 'Poland',
 'PT' => 'Portugal',
 'PR' => 'Puerto Rico',
 'QA' => 'Qatar',
 'RE' => 'Reunion',
 'RO' => 'Romania',
 'RU' => 'Russian Federation',
 'RW' => 'Rwanda',
 'SH' => 'Saint Helena',
 'KN' => 'Saint Kitts and Nevis',
 'LC' => 'Saint Lucia',
 'PM' => 'Saint Pierre and Miquelon',
 'VC' => 'Saint Vincent and the Grenadines',
 'WS' => 'Samoa',
 'SM' => 'San Marino',
 'ST' => 'Sao Tome and Principe',
 'SA' => 'Saudi Arabia',
 'SN' => 'Senegal',
 'RS' => 'Serbia',
 'SC' => 'Seychelles',
 'SL' => 'Sierra Leone',
 'SG' => 'Singapore',
 'SK' => 'Slovakia',
 'SI' => 'Slovenia',
 'SB' => 'Solomon Islands',
 'SO' => 'Somalia',
 'ZA' => 'South Africa',
 'GS' => 'South Georgia and the South Sandwich Islands',
 'ES' => 'Spain',
 'LK' => 'Sri Lanka',
 'SD' => 'Sudan',
 'SR' => 'Suriname',
 'SJ' => 'Svalbard and Jan Mayen',
 'SZ' => 'Swaziland',
 'SE' => 'Sweden',
 'CH' => 'Switzerland',
 'SY' => 'Syrian Arab Republic',
 'TW' => 'Taiwan Province of China',
 'TJ' => 'Tajikistan',
 'TZ' => 'Tanzania United Republic of',
 'TH' => 'Thailand',
 'TL' => 'Timor-Leste',
 'TG' => 'Togo',
 'TK' => 'Tokelau',
 'TO' => 'Tonga',
 'TT' => 'Trinidad and Tobago',
 'TN' => 'Tunisia',
 'TR' => 'Turkey',
 'TM' => 'Turkmenistan',
 'TC' => 'Turks and Caicos Islands',
 'TV' => 'Tuvalu',
 'UG' => 'Uganda',
 'UA' => 'Ukraine',
 'AE' => 'United Arab Emirates',
 'GB' => 'United Kingdom',
 'GBB' => 'United Kingdom (Great Britain)',
 'US' => 'United States',
 'UM' => 'United States Minor Outlying Islands',
 'UY' => 'Uruguay',
 'UZ' => 'Uzbekistan',
 'VU' => 'Vanuatu',
 'VE' => 'Venezuela',
 'VN' => 'Viet Nam',
 'VG' => 'Virgin Islands British',
 'VI' => 'Virgin Islands U.s.',
 'WF' => 'Wallis and Futuna',
 'EH' => 'Western Sahara',
 'YE' => 'Yemen',
 'ZM' => 'Zambia',
 'ZW' => 'Zimbabwe',
 );
 return array_keys($countries, $countryName);
 }
 
 function getData(){
 return $this->data;
 }
 }
 
 class XmlToArray{
 var $xml=NULL;
 function XmlToArray($xml){
 $this->xml = $xml;
 }
 
 function _struct_to_array($values, &$i){
 $child = array();
 if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
 while ($i++ < count($values)) {
 switch ($values[$i]['type']) {
 case 'cdata':
 array_push($child, $values[$i]['value']);
 break;
 case 'complete':
 $name = $values[$i]['tag'];
 if(!empty($name)){
 $child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
 if(isset($values[$i]['attributes'])) {
 $child[$name] = $values[$i]['attributes'];
 }
 }
 break;
 case 'open':
 $name = $values[$i]['tag'];
 $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
 $child[$name][$size] = $this->_struct_to_array($values, $i);
 break;
 case 'close':
 return $child;
 break;
 }
 }
 return $child;
 }
 function createArray(){
 $xml    = $this->xml;
 $values = array();
 $index  = array();
 $array  = array();
 $parser = xml_parser_create();
 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
 xml_parse_into_struct($parser, $xml, $values, $index);
 xml_parser_free($parser);
 $i = 0;
 $name = $values[$i]['tag'];
 $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
 $array[$name] = $this->_struct_to_array($values, $i);
 return $array;
 }
 }
 ?>
 
 Anyone got any suggestions?
 RE: PHP Steam Badge Generator by Chancer on 03-07-2010 at 03:05 AM
 
Suggestion? Make it a little bit bigger.What do the shield and star mean?
 RE: PHP Steam Badge Generator by Jimbo on 03-07-2010 at 05:45 AM
 
 quote:I meant more of a suggestion to find a way to detect invalid ID, or private profile, but nevertheless, thanks anyway.Originally posted by Chancer
 Suggestion? Make it a little bit bigger.
 What do the shield and star mean?
 
 The reason its that small though, is because on my new forum, I display that under under each user's avatar.
 
 The Shield is VAC status, and the star shows Steam Rank.
 RE: PHP Steam Badge Generator by Spunky on 03-07-2010 at 02:33 PM
 
In scrape.php edit the pickSteamURL function to check for a valid id and return an error code or redirect to an error page
 
 js code:private function pickSteamURL($userID) {
 //put check in for $userID here
 if (is_numeric($userID)){
 return "http://steamcommunity.com/profiles/" . $userID;
 } else {
 return "http://steamcommunity.com/id/" . $userID;
 }
 }
 
 RE: PHP Steam Badge Generator by Jimbo on 03-07-2010 at 03:38 PM
 
Thanks Spunky, I'll see what I can come up with   RE: PHP Steam Badge Generator by Chancer on 03-07-2010 at 10:19 PM
 
 quote:But what if any user sets his ID to a number code?Originally posted by Spunky
 In scrape.php edit the pickSteamURL function to check for a valid id and return an error code or redirect to an error page
 
 
 js code:private function pickSteamURL($userID) {
 //put check in for $userID here
 if (is_numeric($userID)){
 return "http://steamcommunity.com/profiles/" . $userID;
 } else {
 return "http://steamcommunity.com/id/" . $userID;
 }
 }
 
 
 RE: PHP Steam Badge Generator by Jimbo on 03-07-2010 at 10:34 PM
 
 quote:Yes, that is a limitation at the moment, and I have absolutely no clue how I would resolve that.Originally posted by Chancer
 
 quote:But what if any user sets his ID to a number code?Originally posted by Spunky
 In scrape.php edit the pickSteamURL function to check for a valid id and return an error code or redirect to an error page
 
 
 js code:private function pickSteamURL($userID) {
 //put check in for $userID here
 if (is_numeric($userID)){
 return "http://steamcommunity.com/profiles/" . $userID;
 } else {
 return "http://steamcommunity.com/id/" . $userID;
 }
 }
 
 
 
 
 RE: PHP Steam Badge Generator by Spunky on 03-07-2010 at 10:55 PM
 
In that case you would probably have to do an XHR like we talked about to check if it exists. It just adds extra over-head
 
 |