From 0c05f4badbeb28325f8d2fbf86ba94d8f6627595 Mon Sep 17 00:00:00 2001 From: neonf Date: Mon, 22 Apr 2024 07:27:41 +0700 Subject: [PATCH] Replaced duplicate --- charts.php | 23 ++--------------------- src/lgsl_class.php | 34 ++++++++++++++++++++++++++++++++++ userbar.php | 43 +++++++++---------------------------------- 3 files changed, 45 insertions(+), 55 deletions(-) diff --git a/charts.php b/charts.php index 2fbfe58..8606e8a 100644 --- a/charts.php +++ b/charts.php @@ -1,20 +1,6 @@ get_history(); $max = 1; @@ -47,12 +33,7 @@ function findMax(&$server) { $server = new Server(["ip" => $ip, "c_port" => $port, "id" => $s]); $server->lgsl_cached_query("cs"); if (!$server) { - $white = imagecolorallocate($im, 255, 255, 255); - imagefill($im, 0, 0, $white); - imagestring($im, 1, (int) ($w / 2 - strlen($lgsl_config['text']['mid']) * 2.2), (int) ($h / 2), $lgsl_config['text']['mid'], $black); - imagepng($im); - imagedestroy($im); - exit(); + Image::makeImageError($w, $h, $lgsl_config['text']['mid']); } $max = $server->get_players_count('max') > 0 ? $server->get_players_count('max') : findMax($server); $x0 = 30; @@ -132,7 +113,7 @@ function findMax(&$server) { } } - $game_id = makeImage($server->game_icon('src/'), 16, 16); // create game icon + $game_id = Image::makeImage($server->game_icon('src/'), 16, 16); // create game icon imagecopy($im, $game_id, 7, 2, 0, 0, 16, 16); // place game icon $font = dirname(__FILE__) . '/src/other/cousine.ttf'; diff --git a/src/lgsl_class.php b/src/lgsl_class.php index 89f1317..d192b2e 100644 --- a/src/lgsl_class.php +++ b/src/lgsl_class.php @@ -1062,6 +1062,40 @@ public function sort_player_fields() { return array_merge($fields_show, $fields_list); } } + class Image { + static function makeImage($src, $width, $height) { + if (!file_exists($src)) { + error_log("Error with image: $src\n"); + return null; + } + list($w, $h) = getimagesize($src); + $type = substr($src, -3); + header("Content-type: image/$type"); + switch ($type) { + case 'gif': {$result = imagecreatefromgif($src); break;} + case 'png': {$result = imagecreatefrompng($src); break;} + case 'jpg': {$result = imagecreatefromjpeg($src); break;} + } + if ($width != $w || $height != $h) { + $image = $result; + $result = imagecreatetruecolor($width, $height); + imagecopyresampled($result, $image, 0, 0, 0, 0, $width, $height, $w, $h); + } + return $result; + } + static function makeImageError($width, $height, $text) { + header("Content-type: image/gif"); + $text = "LGSL: {$text}"; + $im = imagecreatetruecolor($width, $height); + $font = 6; + $white = imagecolorallocate($im, 255, 255, 255); + imagefill($im, 0, 0, $white); + imagestring($im, 1, (int)(($width - strlen($text) * $font) / 2), $font, $text, imagecolorallocate($im, 0, 0, 0)); + imagegif($im); + imagedestroy($im); + exit(); + } + } //------------------------------------------------------------------------------------------------------------+ diff --git a/userbar.php b/userbar.php index 9233bc6..92473f0 100644 --- a/userbar.php +++ b/userbar.php @@ -1,24 +1,6 @@ $ip, "c_port" => $port, "id" => $s]); $server->lgsl_cached_query($query); if (!$server) { - header("Content-type: image/gif"); - $im = imagecreatetruecolor(350, 20); - $white = imagecolorallocate($im, 255, 255, 255); - imagefill($im, 0, 0, $white); - imagestring($im, 1, (int)(175 - strlen($lgsl_config['text']['mid']) * 2.2), 6, $lgsl_config['text']['mid'], imagecolorallocate($im, 0, 0, 0)); - imagegif($im); - imagedestroy($im); - exit(); + Image::makeImageError(350, 20, $lgsl_config['text']['mid']); } // SHARED SETTINGS @@ -89,7 +64,7 @@ function drawHistory(&$im, $x, $y, $w, $h, &$server) { // SETTINGS $w = 468; $h = 64; - $im = @makeImage("src/other/banner468x64.png", $w, $h); // create background + $im = @Image::makeImage("src/other/banner468x64.png", $w, $h); // create background $color_nm = imagecolorallocate($im, 255, 255, 255); $color_ip = imagecolorallocate($im, 255, 255, 255); $color_mp = imagecolorallocate($im, 255, 255, 255); @@ -115,7 +90,7 @@ function drawHistory(&$im, $x, $y, $w, $h, &$server) { case Server::PASSWORDED: { $stat = $stat_ps; break; } } imagefilledrectangle($im, 14, 14, 47, 47, $stat); - $game_id = @makeImage($server->game_icon('src/'), 32, 32); // create game icon + $game_id = @Image::makeImage($server->game_icon('src/'), 32, 32); // create game icon imagecopy($im, $game_id, 16, 16, 0, 0, 32, 32); // place game icon imagettftext($im, 10, 0, 62, 19, $color_nm, $font, /* name */ $server->get_name(false)); @@ -132,7 +107,7 @@ function drawHistory(&$im, $x, $y, $w, $h, &$server) { // SETTINGS $w = 160; $h = 248; - $im = @makeImage("src/other/banner160x248.jpg", $w, $h); // create background + $im = @Image::makeImage("src/other/banner160x248.jpg", $w, $h); // create background $color_nm = imagecolorallocate($im, 255, 255, 255); $color_ip = imagecolorallocate($im, 255, 255, 255); $color_mp = imagecolorallocate($im, 255, 255, 255); @@ -163,11 +138,11 @@ function drawHistory(&$im, $x, $y, $w, $h, &$server) { } else { imagefilledpolygon($im, [0,0, 16,0, 0,16], $stat); } - $game_id = @makeImage($server->game_icon('src/'), 16, 16); // create game icon + $game_id = @Image::makeImage($server->game_icon('src/'), 16, 16); // create game icon imagecopy($im, $game_id, $w-16, $h-16, 0, 0, 16, 16); // place game icon if ($lgsl_config['locations']) { - $loc_id = @makeImage("src/other/locations.gif", 224, 198); // create location icon + $loc_id = @Image::makeImage("src/other/locations.gif", 224, 198); // create location icon $result = imagecreatetruecolor(16, 11); $pos = LGSL::locationCoords($server->getLocation()); imagecopyresampled($result, $loc_id, 0, 0, $pos[0], $pos[1], 16, 11, 16, 11); @@ -214,7 +189,7 @@ function drawHistory(&$im, $x, $y, $w, $h, &$server) { // SETTINGS $w = 350; $h = 20; - $im = @makeImage("src/other/banner350x20.gif", $w, $h); // create background + $im = @Image::makeImage("src/other/banner350x20.gif", $w, $h); // create background $color_nm = imagecolorallocate($im, 128, 0, 0); $color_ip = imagecolorallocate($im, 255, 0, 0); $color_mp = imagecolorallocate($im, 0, 0, 0); @@ -230,8 +205,8 @@ function drawHistory(&$im, $x, $y, $w, $h, &$server) { $time = date(str_replace([':S', ':s', '/Y', '/y'], '', $lgsl_config['text']['tzn'])); - $on_id = @makeImage($server->icon_status('src/'), 16, 16); // create status icon - $game_id = @makeImage($server->game_icon('src/'), 16, 16); // create game icon + $on_id = @Image::makeImage($server->icon_status('src/'), 16, 16); // create status icon + $game_id = @Image::makeImage($server->game_icon('src/'), 16, 16); // create game icon imagecopy($im, $on_id, 7, 2, 0, 0, 16, 16); // place status icon imagecopy($im, $game_id, 25, 2, 0, 0, 16, 16); // place game icon