This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
live_league_signature.php
167 lines (128 loc) · 4.8 KB
/
live_league_signature.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
// Configuration values
define( 'WEBAPIKEY', 'YOURAPIKEYHERE_YOURAPIKEYHERE_YOURAPIKEYHERE' );
// Set up our cache backing
$mc = new Memcached();
$mc->addServer( "localhost", 11211 );
function MakeTemplateImage()
{
$canvas = new Imagick();
$canvas->newPseudoImage( 500, 70, "gradient:#242C30-#000000" );
$canvas->borderImage( "black", 1, 1 );
// Draw a box to put the logo in
$logoColorBorder = new ImagickPixel( "#555555" );
$logoBoxDraw = new ImagickDraw();
$logoBoxDraw->setFillColor( "#000000" );
$logoBoxDraw->setStrokeColor( $logoColorBorder );
$logoBoxDraw->setStrokeWidth( 1 );
$logoBoxDraw->rectangle( -1, -1, 71, 72 );
$canvas->drawImage( $logoBoxDraw );
$logo = new Imagick( "dota2_logo.png" );
$canvas->compositeImage( $logo, imagick::COMPOSITE_OVER, 16, 14 );
// Add a DOTA2.COM link
$fontColorDark = new ImagickPixel( "#656565" );
$urlDraw = new ImagickDraw();
$urlDraw->setFontSize( 9.1 );
$urlDraw->setFontWeight( 900 );
$urlDraw->setFillColor( $fontColorDark );
$urlDraw->setTextAlignment( 2 /* CENTER */);
$urlDraw->annotation( 36, 66, "DOTA2.COM" );
$canvas->drawImage( $urlDraw );
return $canvas;
}
$gif = new Imagick();
$gif->setFormat( "gif" );
// Get the list of leagues from the cache or WebAPI.
$leagueContents = $mc->get( "league_contents" );
if ( $leagueContents === false )
{
$leagueContents = file_get_contents( "http://api.steampowered.com/IDOTA2Match_570/GetLeagueListing/V001/?language=en&key=" . WEBAPIKEY );
$mc->set( "league_contents", $leagueContents, 60 * 60 * 1 /* 1 hour */ );
}
$data = json_decode( $leagueContents, true );
// Construct a mapping from the league ID to names.
$leagueIDToName = array();
if ( isset( $data["result"]["leagues"] ) )
{
foreach ( $data["result"]["leagues"] as $league )
{
$leagueIDToName[$league["leagueid"]] = $league["name"];
}
}
// Get the list of live league games from the cache or WebAPI.
$liveGamesContents = $mc->get( "live_games_contents" );
if ( $liveGamesContents === false )
{
$liveGamesContents = file_get_contents( "http://api.steampowered.com/IDOTA2Match_570/GetLiveLeagueGames/V001/?language=en&key=" . WEBAPIKEY );
$mc->set( "live_games_contents", $liveGamesContents, 60 * 3 /* 3 minutes */ );
}
$data = json_decode( $liveGamesContents, true );
if ( isset( $data["result"]["games"] ) && !empty( $data["result"]["games"] ) )
{
foreach ( $data["result"]["games"] as $game )
{
$canvas = MakeTemplateImage();
$fontColorBright = new ImagickPixel( "#EEEEDD" );
$fontColorDark = new ImagickPixel( "#A2A2A2" );
$fontColor = new ImagickPixel( "#BFBFBF" );
$draw = new ImagickDraw();
$draw->setFontSize( 22 );
$draw->setFontWeight( 600 );
$draw->setFillColor( $fontColorBright );
$draw->setTextAlignment( 2 /* CENTER */);
$draw->annotation( 277, 30, isset( $leagueIDToName[$game["league_id"]] ) ? $leagueIDToName[$game["league_id"]] : "Unknown League" );
$draw->setFontSize( 18 );
$draw->setFontWeight( 600 );
$draw->setFillColor( $fontColor );
$draw->setTextAlignment( 1 /* LEFT */);
$draw->annotation( 95, 60, $game["dire_team"]["team_name"] );
$draw->setTextAlignment( 2 /* CENTER */);
$draw->setFillColor( $fontColorDark );
$draw->setFontWeight( 100 );
$draw->annotation( 277, 60, "vs" );
$draw->setFontWeight( 600 );
$draw->setFillColor( $fontColor );
$draw->setTextAlignment( 3 /* RIGHT */);
$draw->annotation( 460, 60, $game["radiant_team"]["team_name"] );
$canvas->drawImage( $draw );
$drawColorBorder = new ImagickPixel( "#852222" );
// Draw a box to put the live text in
$liveBoxDraw = new ImagickDraw();
$liveBoxDraw->setFillColor( "#000000" );
$liveBoxDraw->setStrokeColor( $drawColorBorder );
$liveBoxDraw->setStrokeWidth( 1 );
$liveBoxDraw->rectangle( 484, -1, 502, 72 );
$canvas->drawImage( $liveBoxDraw );
// Draw some text showing it's a live game
$fontColorRed = new ImagickPixel( "#FF1111" );
$liveDraw = new ImagickDraw();
$liveDraw->setFontSize( 9.1 );
$liveDraw->setFontWeight( 900 );
$liveDraw->setFillColor( $fontColorRed );
$liveDraw->setTextAlignment( 1 /* LEFT */);
$liveDraw->rotate( 270 );
$liveDraw->annotation( -67, 497, "LIVE MATCH!" );
$canvas->drawImage( $liveDraw );
$canvas->setImageFormat( "gif" );
$canvas->setImageDelay( 350 );
$gif->addImage( $canvas );
}
}
else
{
$canvas = MakeTemplateImage();
$fontColor = new ImagickPixel( "#777777" );
$draw = new ImagickDraw();
$draw->setFontSize( 22 );
$draw->setFontWeight( 100 );
$draw->setFillColor( $fontColor );
$draw->setTextAlignment( 2 /* CENTER */);
$draw->annotation( 280, 43, "No live league games running" );
$canvas->drawImage( $draw );
$canvas->setImageFormat( "gif" );
$gif->addImage( $canvas );
}
header( "Content-Type: image/gif" );
header( "Expires: " . gmdate( "D, d M Y H:i:s \G\M\T", time() + ( 3 * 60 /* 3 minutes */ ) ) );
echo $gif->getImagesBlob();
?>