Posts Tagged PHP

Uptime image

With PHP, creating dynamic images is easy. For example combined with some shell commands, it is possible to generate a dynamic image which displays server uptime and some kernel infos:

Uptime image

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
  header("Content-type: image/png");
  $image = imagecreatefrompng("uptime.linux.png");
  $font_size = 7.5;
  $color = imagecolorallocate($image, 255, 255, 255);
 
  $uptime = exec("/bin/cat /proc/uptime | /usr/bin/cut -f 1 -d ' '");
  $days = (int) ($uptime / 86400);
  $uptime -= $days * 86400;
  $hours = (int) ($uptime / 3600);
  $uptime -= $hours * 3600;
  $minutes = (int) ($uptime / 60);
 
  $act_dir = getcwd();
  ImageTTFText($image, $font_size, 0, 55, 15, $color, $act_dir."/Tahoma.ttf", "http://www.dresselhaus.biz");
  ImageTTFText($image, $font_size, 0, 55, 34, $color, $act_dir."/Tahoma.ttf", "Host: ".exec("hostname -f"));
  ImageTTFText($image, $font_size, 0, 55, 45, $color, $act_dir."/Tahoma.ttf", "Uptime: ".$days. "d ".$hours."h ".$minutes."m");
  ImageTTFText($image, $font_size, 0, 55, 56, $color, $act_dir."/Tahoma.ttf", "System: ".exec("uname")." (".exec("uname -m").")");
  ImageTTFText($image, $font_size, 0, 55, 67, $color, $act_dir."/Tahoma.ttf", "Kernel: ".exec("uname -r"));
  imagepng($image);
  imagedestroy($image);
?>

,

No Comments