What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP Steam Badge Generator

PHP Steam Badge Generator
Author: Message:
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. PHP Steam Badge Generator
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?

This post was edited on 05-14-2013 at 02:13 PM by Jimbo.
03-06-2010 03:56 PM
Profile E-Mail PM Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
RE: PHP Steam Badge Generator
Suggestion? Make it a little bit bigger.
What do the shield and star mean?
03-07-2010 03:05 AM
Profile E-Mail PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. RE: PHP Steam Badge Generator
quote:
Originally posted by Chancer
Suggestion? Make it a little bit bigger.
What do the shield and star mean?
I meant more of a suggestion to find a way to detect invalid ID, or private profile, but nevertheless, thanks anyway.
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.

This post was edited on 03-07-2010 at 05:46 AM by Jimbo.
03-07-2010 05:45 AM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: PHP Steam Badge Generator
In scrape.php edit the pickSteamURL function to check for a valid id and return an error code or redirect to an error page

Javascript 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;
        }
    }


This post was edited on 03-07-2010 at 02:34 PM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
03-07-2010 02:33 PM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. RE: PHP Steam Badge Generator
Thanks Spunky, I'll see what I can come up with :)
03-07-2010 03:38 PM
Profile E-Mail PM Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
RE: PHP Steam Badge Generator
quote:
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

Javascript 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;
        }
    }


But what if any user sets his ID to a number code?
03-07-2010 10:19 PM
Profile E-Mail PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. RE: PHP Steam Badge Generator
quote:
Originally posted by Chancer
quote:
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

Javascript 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;
        }
    }


But what if any user sets his ID to a number code?
Yes, that is a limitation at the moment, and I have absolutely no clue how I would resolve that.
03-07-2010 10:34 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: PHP Steam Badge Generator
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
<Eljay> "Problems encountered: shit blew up" :zippy:
03-07-2010 10:55 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On