scroll.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. ini_set("display_errors", 1);
  3. error_reporting(E_ALL);
  4. if(isset($_GET['update'])) echo "Update!";
  5. $name = array("Mon" => "Mo", "Tue" => "Di", "Wed" => "Mi", "Thu" => "Do", "Fri" => "Fr", "Sat" => "Sa", "Sun" => "So");
  6. $cache_file = "data.txt";
  7. $url = 'http://api.openweathermap.org/data/2.5/forecast/daily?id=<YourTownId>&appid=<YourAppId>&units=metric';
  8. if (!isset($_GET['update']) && file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 30 ))) {
  9. // Cache file is less than five minutes old.
  10. // Don't bother refreshing, just use the file as-is.
  11. $txt = file_get_contents($cache_file);
  12. } else {
  13. // Our cache is out-of-date, so load the data from our remote server,
  14. // and also save it over our cache for next time.
  15. $data = json_decode(file_get_contents($url));
  16. $str = "Keine Daten gefunden!";
  17. if(!empty($data->city->name)) {
  18. $str="";
  19. foreach($data->list as $day) {
  20. $dt = $name[date("D", $day->dt)];
  21. $temp = number_format($day->temp->day, 1, ',', '');
  22. $min = number_format($day->temp->min, 0, ',', '');
  23. $max = number_format($day->temp->max, 0, ',', '');
  24. $wlk = $day->weather[0]->description;
  25. if(isset($day->rain)) $rain = $day->rain; else $rain = 0;
  26. if(isset($day->snow)) $snow = $day->snow; else $snow = 0;
  27. if($rain + $snow > 0.05) $ns = number_format($rain + $snow, 2, ',', '') . "mm"; else $ns = "";
  28. $str.="$dt: $min-$max" . "°C $wlk $ns ";
  29. }
  30. } else {
  31. }
  32. //echo "<pre>" . json_encode($data, JSON_PRETTY_PRINT) . "</pre>";
  33. //echo $str;
  34. //$str = substr($str,0,123);//126
  35. $img_width = strlen($str)*5;
  36. $im = imagecreatetruecolor($img_width, 5);
  37. // Initialise colours;
  38. $black = imagecolorallocate($im, 0, 0, 0);
  39. $white = imagecolorallocate($im, 255, 255, 255);
  40. $size = imagettftext($im, 4, 0, 5, 5, $white, "/var/www/led/5by5.ttf", $str);
  41. if ($size[2] < 615) $width = $size[2]; else $width = 615;
  42. imagetruecolortopalette($im, false, 2);
  43. $txt = "";
  44. for($y=0; $y<5; $y++) {
  45. $txt .= "\n";
  46. $byte=0;
  47. $index=0;
  48. for($x=0;$x<=$width;$x++) {//chr 32 to 160
  49. if(imagecolorat($im, $x, $y) != $black) {
  50. $byte += 1 << $index;
  51. }
  52. $index++;
  53. if($index >= 7) {
  54. $index=0;
  55. $txt .= chr($byte+32);
  56. $byte=0;
  57. }
  58. }
  59. if($index < 7) {
  60. $txt .= chr($byte+32);
  61. }
  62. }
  63. if(isset($_GET['update'])) imagepng($im, "img.png");
  64. imagedestroy($im);
  65. file_put_contents($cache_file, $txt, LOCK_EX);
  66. }
  67. echo "" . $txt;
  68. ?>