/**
  *
  * @param string $playername Minecraft player name
  * @param resource $avatar the rendered avatar (for example player head)
  *
  * @param string $background Image Path or Standard Value
  * @return resource the generated banner
  */
 public static function player($playername, $avatar = NULL, $background = NULL)
 {
     $canvas = MinecraftBanner::getBackgroundCanvas(self::PLAYER_WIDTH, self::PLAYER_HEIGHT, $background);
     $head_height = self::AVATAR_SIZE;
     $head_width = self::AVATAR_SIZE;
     $avater_x = self::PLAYER_PADDING;
     $avater_y = self::PLAYER_HEIGHT / 2 - self::AVATAR_SIZE / 2;
     if ($avatar == NULL) {
         $avatar = imagecreatefrompng(__DIR__ . "/img/head.png");
         imagesavealpha($avatar, true);
         imagecopy($canvas, $avatar, $avater_x, $avater_y, 0, 0, $head_width, $head_height);
     } else {
         $head_width = imagesx($avatar);
         $head_height = imagesy($avatar);
         if ($head_width > self::AVATAR_SIZE) {
             $head_width = self::AVATAR_SIZE;
         }
         if ($head_height > self::AVATAR_SIZE) {
             $head_height = self::AVATAR_SIZE;
         }
         $center_x = $avater_x + self::AVATAR_SIZE / 2 - $head_width / 2;
         $center_y = $avater_y + self::AVATAR_SIZE / 2 - $head_height / 2;
         imagecopy($canvas, $avatar, $center_x, $center_y, 0, 0, $head_width, $head_height);
     }
     $box = imagettfbbox(self::TEXT_SIZE, 0, MinecraftBanner::FONT_FILE, $playername);
     $text_width = abs($box[4] - $box[0]);
     $text_color = imagecolorallocate($canvas, 255, 255, 255);
     $remaining = self::PLAYER_WIDTH - self::AVATAR_SIZE - $avater_x - self::PLAYER_PADDING;
     $text_posX = $avater_x + self::AVATAR_SIZE + $remaining / 2 - $text_width / 2;
     $text_posY = $avater_y + self::AVATAR_SIZE / 2 + self::TEXT_SIZE / 2;
     imagettftext($canvas, self::TEXT_SIZE, 0, $text_posX, $text_posY, $text_color, MinecraftBanner::FONT_FILE, $playername);
     return $canvas;
 }
 /**
  *
  * @param string $address the server address
  * @param string $motd message of the day which should be displayed
  * @param int $players not implemented
  * @param int $max_players not implemented
  * @param resource $favicon not implemented
  * @param string $background Image Path or Standard Value
  * @param int $ping not implemented
  * @return resource the rendered banner
  */
 public static function server($address, $motd = "§cOffline Server", $players = -1, $max_players = -1, $favicon = NULL, $background = NULL, $ping = self::PING_WELL)
 {
     $canvas = MinecraftBanner::getBackgroundCanvas(self::WIDTH, self::HEIGHT, $background);
     if ($favicon == NULL) {
         $favicon = imagecreatefrompng(__DIR__ . '/img/favicon.png');
     }
     //center the iamge in y-direction and add padding to the left side
     $favicon_posY = (self::HEIGHT - self::FAVICON_SIZE) / 2;
     imagecopy($canvas, $favicon, self::PADDING, $favicon_posY, 0, 0, self::FAVICON_SIZE, self::FAVICON_SIZE);
     $startX = self::PADDING + self::FAVICON_SIZE + self::PADDING;
     $white = imagecolorallocate($canvas, 255, 255, 255);
     $titleY = $favicon_posY + self::PADDING * 2 + self::TITLE_SIZE;
     imagettftext($canvas, self::TITLE_SIZE, 0, $startX, $titleY, $white, MinecraftBanner::FONT_FILE, $address);
     $components = explode(MinecraftBanner::COLOR_CHAR, $motd);
     $nextX = $startX;
     $nextY = 50;
     $last_color = [255, 255, 255];
     foreach ($components as $component) {
         if (empty($component)) {
             continue;
         }
         $color_code = $component[0];
         $colors = MinecraftBanner::COLORS;
         //default to white
         $text = $component;
         if (!empty($color_code)) {
             //try to find the color rgb to the colro code
             if (isset($colors[$color_code])) {
                 $color_rgb = $colors[$color_code];
                 $last_color = $color_rgb;
             }
             $text = substr($component, 1);
         }
         $color = imagecolorallocate($canvas, $last_color[0], $last_color[1], $last_color[2]);
         $lines = explode("\n", $text);
         imagettftext($canvas, self::MOTD_SIZE, 0, $nextX, $nextY, $color, MinecraftBanner::FONT_FILE, $lines[0]);
         $box = imagettfbbox(self::MOTD_SIZE, 0, MinecraftBanner::FONT_FILE, $text);
         $text_width = abs($box[4] - $box[0]);
         if (count($lines) > 1) {
             $nextX = $startX;
             $nextY += self::PADDING * 2 + self::MOTD_SIZE;
             imagettftext($canvas, self::MOTD_SIZE, 0, $nextX, $nextY, $color, MinecraftBanner::FONT_FILE, $lines[1]);
         } else {
             $nextX += $text_width + self::PADDING;
         }
     }
     if ($ping < 0) {
         $image = imagecreatefrompng(__DIR__ . '/img/ping/-1.png');
     } else {
         if ($ping > 0 && $ping <= self::PING_WELL) {
             $image = imagecreatefrompng(__DIR__ . '/img/ping/5.png');
         } else {
             if ($ping <= self::PING_GOOD) {
                 $image = imagecreatefrompng(__DIR__ . '/img/ping/4.png');
             } else {
                 if ($ping <= self::PING_WORSE) {
                     $image = imagecreatefrompng(__DIR__ . '/img/ping/3.png');
                 } else {
                     if ($ping <= self::PING_WORST) {
                         $image = imagecreatefrompng(__DIR__ . '/img/ping/2.png');
                     } else {
                         if ($ping >= self::PING_WORST) {
                             $image = imagecreatefrompng(__DIR__ . '/img/ping/1.png');
                         }
                     }
                 }
             }
         }
     }
     $ping_posX = self::WIDTH - self::PING_WIDTH - self::PADDING;
     imagecopy($canvas, $image, $ping_posX, $favicon_posY, 0, 0, self::PING_WIDTH, self::PING_HEIGHT);
     $text = $players . ' / ' . $max_players;
     $box = imagettfbbox(self::PLAYERS_SIZE, 0, MinecraftBanner::FONT_FILE, $text);
     $text_width = abs($box[4] - $box[0]);
     //center it based on the ping image
     $posY = $favicon_posY + self::PING_HEIGHT / 2 + self::PLAYERS_SIZE / 2;
     $posX = $ping_posX - $text_width - self::PADDING / 2;
     imagettftext($canvas, self::PLAYERS_SIZE, 0, $posX, $posY, $white, MinecraftBanner::FONT_FILE, $text);
     return $canvas;
 }