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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
<?php header("Content-type: image/png"); // get the connection information require('config.php'); // for logging the requesting site and page, not currently used /* if (substr($_SERVER['HTTP_REFERER'], 0, 33) == "http://www.thegrandtournament.com") { } else { $myFile = "log.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "\n".$_SERVER['HTTP_REFERER']; fwrite($fh, $stringData); fclose($fh); } */ function number_suffix($number) { // Validate and translate our input if (is_numeric($number)){ // Get the last two digits (only once) $n = $number % 100; } else { // If the last two characters are numbers if (preg_match( '/[0-9]?[0-9]$/', $number, $matches )) { // Return the last one or two digits $n = array_pop($matches); } else { // Return the string, we can add a suffix to it return $number; } } // Skip the switch for as many numbers as possible. if (($n > 3) && ($n < 21)) { return $number . 'th'; } // Determine the suffix for numbers ending in 1, 2 or 3, otherwise add a 'th' switch ($n % 10) { case '1': return $number . 'st'; case '2': return $number . 'nd'; case '3': return $number . 'rd'; default: return $number . 'th'; } } if (!mysql_connect($dbhost,$dbuser,$dbpasswd)) { die(mysql_error()); } if (!mysql_select_db($dbname)) { die(mysql_error()); } //get string from URL, member id $id = mysql_real_escape_string(intval($_GET['id'])); $format = mysql_real_escape_string($_GET['format']); $sql = "SELECT * FROM grand_users WHERE user_id = ".$id."; "; if( !($result = mysql_query($sql)) ) { die(mysql_error()); } else { $row = mysql_fetch_assoc($result); } $sql = " SELECT * FROM grand_users ORDER BY games_points DESC, games_won DESC, games_lost ASC, username ; "; if( !($result = mysql_query($sql)) ) { die(mysql_error()); } if (!$username = $row['username']) $username = "No such user!"; if (!$points = $row['games_points']) $points = 0; if (!$record = ($row['games_won'] . "-" . $row['games_lost'])) $record = "0-0"; switch ($row['user_avatar_type']) { case "1": $avatar = "images/avatars/".$row['user_avatar']; break; case "2": $handle = fopen($row['user_avatar'], "rb"); $contents = stream_get_contents($handle); fclose($handle); $user_avatar = substr($row['user_avatar'],strrpos($row['user_avatar'],"/")); $fh = fopen('images/avatars/remote/'.$user_avatar, 'w+'); fwrite($fh, $contents); fclose($fh); $avatar = "images/avatars/remote/".$user_avatar; break; } $current = 1; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if ($row["user_id"] === $id) { $rank = $current; } $current++; } $total = mysql_num_rows($result); mysql_close(); //$rank = array_search($username,$all) + 1; switch ($format) { default: if ($rank) $rank = $rank . " of " . $total; $image = imagecreatefrompng('images/redblue.png'); $white = imagecolorallocate($image,255,255,255); $black = imagecolorallocate($image,0,0,0); $font = 'images/visitor-tt2-brk.ttf'; //imagettftext($image,12,0,200,12,$white,$font,'GT League Points:'); imagettftext($image,12,0,320,12,$white,$font,$record); imagettftext($image,12,0,10,12,$white,$font,$username); imagettftext($image,12,0,200,12,$white,$font,$rank); break; case large: $tagline = number_suffix($rank)." in the league, with a ".$record." record."; $image = imagecreatefrompng('images/blue-468x60.png'); if (!file_exists($avatar)) { $avatar = 'templates/MSSimplicity/images/welcome.gif'; } //determine the file extension $avtype = substr($avatar,-4); switch ($avtype) { default: $insert = imagecreatefromgif('templates/MSSimplicity/images/welcome.gif'); break; case ".gif": $insert = imagecreatefromgif($avatar); break; case ".png": $insert = imagecreatefrompng($avatar); break; case ".jpg": $insert = imagecreatefromjpeg($avatar); break; //a little different because JPGs can have both JPEG and JPG extensions case "jpeg": $insert = imagecreatefromjpeg($avatar); break; } $promo = rand(1,1); switch ($promo) { case 1: $promo = "Spar me and get ranked."; break; } $font = 'images/visitor-tt2-brk.ttf'; $altfont = 'images/kartika.ttf'; // Get overlay image width and hight for later use $insert_x = imagesx($insert); $insert_y = imagesy($insert); $box = @imageTTFBbox(12,0,$font,$promo); $textwidth = abs($box[4] - $box[6]); $xcord = 460 - $textwidth; // 2 = some space from right side. $percentage = 58 / $insert_y; $newheight = $insert_y * $percentage; $newwidth = $insert_x * $percentage; $tagstart = $newwidth + 10; imagecopyresized($image,$insert,1,1,0,0,$newwidth,58,$insert_x,$insert_y); $white = imagecolorallocate($image,255,255,255); $black = imagecolorallocate($image,0,0,0); //imagettftext($image,12,0,200,12,$white,$font,'GT League Points:'); // font size, $bottom = imageSY($image) - 8; imagettftext($image,16,0,$tagstart,16,$white,$font,$username); imagettftext($image,14,0,$tagstart,$bottom,$white,$font,$tagline); imagettftext($image,12,0,$xcord,12,$white,$font,$promo); break; } imagepng($image); ?>
Refactorings
No refactoring yet !
Martindale
November 5, 2007, November 05, 2007 04:40, permalink
Updated per current code.
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
<?php header("Content-type: image/png"); require('config.php'); if (substr($_SERVER['HTTP_REFERER'], 0, 33) == "http://www.thegrandtournament.com") { } else { $myFile = "log.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "\n".$_SERVER['HTTP_REFERER']; fwrite($fh, $stringData); fclose($fh); } function number_suffix($number) { // Validate and translate our input if (is_numeric($number)){ // Get the last two digits (only once) $n = $number % 100; } else { // If the last two characters are numbers if (preg_match( '/[0-9]?[0-9]$/', $number, $matches )) { // Return the last one or two digits $n = array_pop($matches); } else { // Return the string, we can add a suffix to it return $number; } } // Skip the switch for as many numbers as possible. if (($n > 3) && ($n < 21)) { return $number . 'th'; } // Determine the suffix for numbers ending in 1, 2 or 3, otherwise add a 'th' switch ($n % 10) { case '1': return $number . 'st'; case '2': return $number . 'nd'; case '3': return $number . 'rd'; default: return $number . 'th'; } } if (!mysql_connect($dbhost,$dbuser,$dbpasswd)) { die(mysql_error()); } if (!mysql_select_db($dbname)) { die(mysql_error()); } //get string from URL, member id $id = mysql_real_escape_string(intval($_GET['id'])); $format = mysql_real_escape_string($_GET['format']); $sql = "SELECT * FROM grand_users WHERE user_id = ".$id."; "; if( !($result = mysql_query($sql)) ) { die(mysql_error()); } else { $row = mysql_fetch_assoc($result); } $sql = " SELECT * FROM grand_users ORDER BY games_points DESC, games_won DESC, games_lost ASC, username ; "; if( !($result = mysql_query($sql)) ) { die(mysql_error()); } if (!$username = $row['username']) $username = "No such user!"; if (!$points = $row['games_points']) $points = 0; if (!$record = ($row['games_won'] . "-" . $row['games_lost'])) $record = "0-0"; switch ($row['user_avatar_type']) { case "1": $avatar = "images/avatars/".$row['user_avatar']; break; case "2": $handle = fopen($row['user_avatar'], "rb"); $contents = stream_get_contents($handle); fclose($handle); $user_avatar = substr($row['user_avatar'],strrpos($row['user_avatar'],"/")); $fh = fopen('images/avatars/remote/'.$user_avatar, 'w+'); fwrite($fh, $contents); fclose($fh); $avatar = "images/avatars/remote/".$user_avatar; break; } $current = 1; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if ($row["user_id"] === $id) { $rank = $current; } $current++; } $total = mysql_num_rows($result); mysql_close(); //$rank = array_search($username,$all) + 1; switch ($format) { default: if ($rank) $rank = $rank . " of " . $total; $image = imagecreatefrompng('images/redblue.png'); $white = imagecolorallocate($image,255,255,255); $black = imagecolorallocate($image,0,0,0); $font = 'images/visitor-tt2-brk.ttf'; //imagettftext($image,12,0,200,12,$white,$font,'GT League Points:'); imagettftext($image,12,0,320,12,$white,$font,$record); imagettftext($image,12,0,10,12,$white,$font,$username); imagettftext($image,12,0,200,12,$white,$font,$rank); break; case large: $tagline = number_suffix($rank)." in the league, with a ".$record." record."; $image = imagecreatefrompng('images/blue-468x60.png'); if (!file_exists($avatar)) { $avatar = 'templates/MSSimplicity/images/welcome.gif'; } //determine the file extension $avtype = substr($avatar,-4); switch ($avtype) { default: $insert = imagecreatefromgif('templates/MSSimplicity/images/welcome.gif'); break; case ".gif": $insert = imagecreatefromgif($avatar); break; case ".png": $insert = imagecreatefrompng($avatar); break; case ".jpg": $insert = imagecreatefromjpeg($avatar); break; //a little different because JPGs can have both JPEG and JPG extensions case "jpeg": $insert = imagecreatefromjpeg($avatar); break; } $promo = rand(1,1); switch ($promo) { case 1: $promo = "Spar me and get ranked."; break; } $font = 'images/visitor-tt2-brk.ttf'; $altfont = 'images/kartika.ttf'; // Get overlay image width and hight for later use $insert_x = imagesx($insert); $insert_y = imagesy($insert); $box = @imageTTFBbox(12,0,$font,$promo); $textwidth = abs($box[4] - $box[6]); $xcord = 460 - $textwidth; // 2 = some space from right side. $percentage = 58 / $insert_y; $newheight = $insert_y * $percentage; $newwidth = $insert_x * $percentage; $tagstart = $newwidth + 10; imagecopyresized($image,$insert,1,1,0,0,$newwidth,58,$insert_x,$insert_y); $white = imagecolorallocate($image,255,255,255); $black = imagecolorallocate($image,0,0,0); //imagettftext($image,12,0,200,12,$white,$font,'GT League Points:'); // font size, $bottom = imageSY($image) - 8; imagettftext($image,16,0,$tagstart,16,$white,$font,$username); imagettftext($image,14,0,$tagstart,$bottom,$white,$font,$tagline); imagettftext($image,12,0,$xcord,12,$white,$font,$promo); break; } imagepng($image); ?>
Sandoze
November 6, 2007, November 06, 2007 17:48, permalink
Sorry I don't have time for a rewrite. I assume things work well. The biggest change I would make is to break that one gigantic function in to several smaller private functions. But that's just me. If I have a function that is doing to many things I break it into smaller chunks.. It makes it easier to debug, change, test.. etc.. not to mention readable.
This code is the one-shot result of generating these images, and results can be be found and tested using these URLs:
http://www.thegrandtournament.com/rank.php?id=4
http://www.thegrandtournament.com/rank.php?id=4&format=large