Ejemplo n.º 1
0
Archivo: sig.php Proyecto: shrz/sig
 /**
  * @param int $sigId
  * @param int $playerId
  * @param int $bnetServerNum
  * @param string $playerAccount
  * @param int $sigBackgroundIndex
  * @param int $playerStatsIndex
  * @param string $lang
  * @param string $region
  * @param int $characterCode
  * @return boolean
  */
 public static function Create($sigId, $playerId, $bnetServerNum, $playerAccount, $sigBackgroundIndex = 1, $playerStatsIndex, $lang, $region, $characterCode)
 {
     // ник
     preg_match('/[^a-z0-9\\-ǂ]*/ui', $playerAccount, $match);
     if ($match[0]) {
         // с русскими символами
         $fontAccount = 'calibri.ttf';
     } else {
         $fontAccount = 'eurostile_ext_med.otf';
     }
     $fontAccount = SIG_BASEDIR . 'php/' . $fontAccount;
     // echo $playerAccount.' '.$fontAccount."\n";
     // языки \ локали
     $locale['ru']['notRanked'] = ' НЕТ РЕЙТИНГА';
     $locale['ru']['place'] = 'МЕСТО';
     $locale['ru']['points'] = 'ОЧКИ';
     $locale['ru']['region'] = 'РЕГИОН';
     $locale['ru']['wins'] = 'ПОБЕД';
     $locale['en']['notRanked'] = ' NOT YET RANKED';
     $locale['en']['place'] = 'PLACE';
     $locale['en']['points'] = 'POINTS';
     $locale['en']['region'] = 'REGION';
     $locale['en']['wins'] = 'WINS';
     if (!($lang == 'ru' || $lang == 'en')) {
         $lang = 'ru';
     }
     $bnetSubDomain = 'eu';
     if ($region === 'US' || $region === 'KR') {
         $bnetSubDomain = strtolower($region);
     } elseif ($region === 0) {
         $region = false;
     }
     $sigPath = SIG_BASEDIR . $sigId . '.png';
     require_once 'network.php';
     $playerAccountUrl = urlencode($playerAccount);
     $relativePlayerAccountUrl = "/sc2/en/profile/{$playerId}/{$bnetServerNum}/{$playerAccountUrl}/";
     $relativePlayerAccountUrlForRegExp = "/sc2/en/profile/{$playerId}/{$bnetServerNum}/{$playerAccount}/";
     $data = Network::GetHTTPData("http://{$bnetSubDomain}.battle.net" . $relativePlayerAccountUrl . 'ladder/', 'id="portrait', 'class="module-right');
     if (!$data) {
         return false;
     }
     switch ($playerStatsIndex) {
         case 0:
             $playerStatsType = '1v1';
             break;
         case 1:
             $playerStatsType = '2v2';
             break;
         case 2:
             $playerStatsType = '3v3';
             break;
         case 3:
             $playerStatsType = '4v4';
             break;
         default:
             $playerStatsType = '1v1';
             break;
     }
     if ($characterCode) {
         $regionX = 133;
     } else {
         $regionX = 170;
     }
     if ($characterCode || $region) {
         $playerAccountY = 19;
     } else {
         $playerAccountY = 24;
     }
     // получаем число ачивок
     preg_match('/h3>([\\d]*)</si', $data, $match);
     $playerAchievements = $match[1];
     // определяем координаты портрета персонажа
     if (preg_match("#portraits/([-\\d]+).jpg.*?'\\) ([-\\d]+)px ([-\\d]+)px no#si", $data, $match)) {
         // из какой заготовки будем брать
         $portraitImg = 'portraits-' . $match[1];
         // координаты, откуда будем брать
         $portraitX = abs($match[2] * PORTRAIT_KOEFICENT);
         $portraitY = abs($match[3] * PORTRAIT_KOEFICENT);
     } else {
         SaveForDebug("portraitImg is empty, http://{$bnetSubDomain}.battle.net" . $relativePlayerAccountUrl . 'ladder/');
         return FALSE;
     }
     if (preg_match('#<a class="league" href="([\\d]+)\\#current-rank">[\\w\\s]+<strong>' . $playerStatsType . '[\\w\\s]*</strong>.*?<a href="' . $relativePlayerAccountUrlForRegExp . '"[\\s]+class="race-([\\w]+)".*?Rank[\\s]+([\\d]+)#si', $data, $match)) {
         $currentRankUrl = $match[1];
         // место
         $isPlayerHasRank = true;
         $playerRank = $locale[$lang]['place'] . ': ' . $match[3];
         // раса
         $playerRace = $match[2];
     } else {
         $playerStats = '  ' . $playerStatsType . $locale[$lang]['notRanked'];
         $leagueImg = 'none';
         $isPlayerHasRank = false;
         $playerRace = 'random';
     }
     if ($isPlayerHasRank) {
         $data = Network::GetHTTPData('http://' . $bnetSubDomain . '.battle.net' . $relativePlayerAccountUrl . 'ladder/' . $currentRankUrl, '<head.*>', 'id="current-rank".*?tr class="row2"');
         if (!$data) {
             return false;
         }
         // лига
         if (preg_match_all('|<title>' . $playerStatsType . ' ([\\w]+) |si', $data, $match)) {
             $leagueImg = strtolower($match[1][0]);
         } else {
             $leagueImg = 'none';
         }
         if (preg_match('|id="current-rank"(.*?)tr class|si', $data, $match)) {
             $data = $match[1];
             if (preg_match_all('|<td class="align-center">([\\d]+)</td>|si', $data, $match)) {
                 $playerPoints = $locale[$lang]['points'] . ': ' . $match[1][0];
                 $wins = $match[1][1];
                 if (isset($match[1][2])) {
                     $lose = $match[1][2];
                     $playerStats = "{$wins} / {$lose}";
                     $playerWinRate = round($wins * 100 / ($wins + $lose)) . ' %';
                 } else {
                     $playerStats = $locale[$lang]['wins'] . ': ' . $wins;
                 }
             }
         }
     }
     $playerImgResource = imagecreatetruecolor(358, 68);
     // берем шаблон
     $templateImg = imagecreatefrompng(SIG_BASEDIR . 'img/sig/med_back_' . $sigBackgroundIndex . '.png');
     // белый цвет по контуру становится прозрачным
     $transparentColor = imagecolorallocate($playerImgResource, 255, 255, 255);
     imagecolortransparent($playerImgResource, $transparentColor);
     // берем портрет
     $portrait = imagecreatefrompng(SIG_BASEDIR . 'img/sig/' . $portraitImg . '.png');
     // лигу
     $league = imagecreatefrompng(SIG_BASEDIR . 'img/sig/leg_' . $leagueImg . '.png');
     // расу
     $raceTemplateImg = imagecreatefrompng(SIG_BASEDIR . "img/sig/race_{$playerRace}.png");
     // выбираем подходящий цвет текста и фон для bnetId
     list($textColor, $bnetIdTpl) = Sig::SelectTextColorAndBnetIdTplByBackgroundIndex($templateImg, $sigBackgroundIndex);
     // выводим все
     // первая строка, первый блок
     if (isset($playerRank)) {
         imagettftext($templateImg, TEXT_FONT_SIZE, 0, 101, 46, $textColor, FONT_STATS_RANK, $playerRank);
     }
     // первая строка, второй блок
     if (isset($playerPoints)) {
         imagettftext($templateImg, TEXT_FONT_SIZE, 0, 162, 46, $textColor, FONT_STATS_RANK, $playerPoints);
     }
     // вторая строка, первый блок
     if (isset($playerStats)) {
         imagettftext($templateImg, TEXT_FONT_SIZE, 0, 101, 57, $textColor, FONT_STATS_RANK, $playerStats);
     }
     // вторая строка, второй блок
     if (isset($playerWinRate)) {
         imagettftext($templateImg, TEXT_FONT_SIZE, 0, 162, 57, $textColor, FONT_STATS_RANK, $playerWinRate);
     }
     $box_nik = imagettfbbox(11, 0, $fontAccount, $playerAccount);
     // 237 - 12 - $box_nik[ 2 ] = 225 - $box_nik[ 2 ]
     imagettftext($templateImg, 11, 0, 223 - $box_nik[2], $playerAccountY, $textColor, $fontAccount, $playerAccount);
     if (isset($playerAchievements)) {
         imagettftext($templateImg, 9, 0, 302, 19, $textColor, FONT_ACHIEVEMENTS, $playerAchievements);
     }
     if (isset($playerStatsType)) {
         imagettftext($templateImg, 9, 0, 320, 51, $textColor, FONT_ACHIEVEMENTS, $playerStatsType);
     }
     if ($characterCode) {
         imagettftext($templateImg, 8, 0, 191, 31, $textColor, FONT_STATS_RANK, ' ID: ' . $characterCode);
     }
     if ($region) {
         imagettftext($templateImg, 8, 0, $regionX, 31, $textColor, FONT_STATS_RANK, $locale[$lang]['region'] . ': ' . $region);
     }
     imagecopyresampled($playerImgResource, $templateImg, 0, 0, 0, 0, 358, 68, imagesx($templateImg), imagesy($templateImg));
     imagecopyresampled($playerImgResource, $portrait, 237, 12, $portraitX, $portraitY, 46, 46, 46, 46);
     imagecopyresampled($playerImgResource, $league, 292, 31, 0, 0, 26, 28, 26, 28);
     imagecopyresampled($playerImgResource, $raceTemplateImg, 219, 43, 0, 0, 18, 18, 18, 18);
     imagepng($playerImgResource, $sigPath);
     // не забываем освобождать за собой память
     imagedestroy($playerImgResource);
     imagedestroy($templateImg);
     imagedestroy($portrait);
     imagedestroy($league);
     imagedestroy($raceTemplateImg);
     return true;
 }