Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Tworzenie obrazka
Forum PHP.pl > Forum > Przedszkole
Rakom111
Hej, nie mogę utworzyć obrazka. W firefox pokazuje się komunikat o tym, że obrazek nie może zostać wyświetlony, ponieważ zawiera błędy.

  1. <?php
  2. include_once 'query.php';
  3. $hostserwa = $_GET['ip'];
  4. $portserwa = $_GET['port'];
  5. $status = new MinecraftServerStatus();
  6. $response = $status->getStatus($hostserwa.':'.$portserwa);
  7. $ipserwera = "$hostserwa:$portserwa";
  8. if(!$response) {
  9. $gracze = "0/0";
  10. $nazwa = "The Server is offline!";
  11. }
  12. else{
  13. $gracze = ''.$response['players'].'/'.$response['maxplayers'].'';
  14. $nazwa = $response['motd'];
  15. }
  16. $czcionka = "ARIALBD.TTF";
  17. $image = imagecreatefrompng('b1.png');
  18. $bialy = imagecolorallocate($image, 255, 255, 255);
  19. imagettftext($image, 8, 0, 30, 8, $bialy, $czcionka, $nazwa);
  20. imagettftext($image, 9, 0, 160, 18, $bialy, $czcionka, $gracze);
  21. imagettftext($image, 9, 0, 30, 18, $bialy, $czcionka, $ipserwera);
  22. header('Content-type: image/png');
  23. imagepng($image);
  24. imagedestroy($image);
  25. ?>

SmokAnalog
Po co używasz buforowania?

Najlepszym sposobem na sprawdzenie dlaczego obrazek nie zostaje wyświetlony jest wyłączenie Content-type: image/png. Zakomentuj tę linię i zobacz jaki masz efekt - pewnie wkradł się gdzieś tam po drodze błąd.
Rakom111
Pokazał się taki błąd:

Zaczyna się na ‰PNG  IHDR i później jakieś litery...

http://pastebin.com/RVSNd64D (wsadziłem na pastebin, bo to ma dużą objętość)
nospor
Sprawdz czy przypadkiem twoje kodowanie pliku to nie UTF-8 z BOM. Ma byc utf8 bez BOM
Rakom111
Jest bez BOM, zresztą cały czas w takim koduje.

//edit

Zakomentowałem ify i include i obrazek się pojawił. Jest to wina pliku query.php, kod daje poniżej.

  1. <?php
  2.  
  3. /**
  4.   * Minecraft Server Status Query
  5.   * @author Julian Spravil <julian.spr@t-online.de> <a href="https://github.com/FunnyItsElmo" target="_blank">https://github.com/FunnyItsElmo</a>
  6.   * @license Free to use but dont remove the author, license and copyright
  7.   * @copyright ˆ 2013 Julian Spravil
  8.   */
  9. class MinecraftServerStatus {
  10. private $timeout;
  11.  
  12. /**
  13.   * Prepares the class.
  14.   * @param int $timeout default(3)
  15.   */
  16. public function __construct($timeout = 3) {
  17. $this->timeout = $timeout;
  18. }
  19.  
  20. /**
  21.   * Gets the status of the target server.
  22.   * @param string $host domain or ip address
  23.   * @param int $port default(25565)
  24.   */
  25. public function getStatus($host = '127.0.0.1', $port = 25565) {
  26.  
  27. //Transform domain to ip address.
  28. if (substr_count($host , '.') != 4) $host = gethostbyname($host);
  29.  
  30. //Get timestamp for the ping
  31. $start = microtime(true);
  32.  
  33. //Connect to the server
  34. if(!$socket = @stream_socket_client('tcp://'.$host.':'.$port, $errno, $errstr, $this->timeout)) {
  35.  
  36. //Server is offline
  37. return false;
  38.  
  39.  
  40. } else {
  41.  
  42. stream_set_timeout($socket, $this->timeout);
  43.  
  44. //Write and read data
  45. fwrite($socket, "\xFE\x01");
  46. $data = fread($socket, 2048);
  47. fclose($socket);
  48. if($data == null) return false;
  49.  
  50. //Calculate the ping
  51. $ping = round((microtime(true)-$start)*1000);
  52.  
  53. //Evaluate the received data
  54. if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){
  55.  
  56. $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
  57. $motd = preg_replace("/(§.)/", "",$result[1]);
  58.  
  59. }else{
  60.  
  61. $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
  62.  
  63. $motd = "";
  64. foreach ($result as $key => $string) {
  65. if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
  66. $motd .= '§'.$string;
  67. }
  68. }
  69.  
  70. $motd = preg_replace("/(§.)/", "", $motd);
  71.  
  72. }
  73. //Remove all special characters from a string
  74. $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);
  75.  
  76. //Set variables
  77. $res = array();
  78. $res['hostname'] = $host;
  79. $res['version'] = $result[0];
  80. $res['motd'] = $motd;
  81. $res['players'] = $result[sizeof($result)-2];
  82. $res['maxplayers'] = $result[sizeof($result)-1];
  83. $res['ping'] = $ping;
  84.  
  85. //return obj
  86. return $res;
  87. }
  88.  
  89. }
  90. }
  91.  
  92. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.