sohotcall

gsBatteryLog.php -- Battery Percentage Logger -- part 1/2

Mar 5th, 2020
193
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //const MULTIPLIER = 2.5826799920626124e-6;
  3. const MULTIPLIER = 1e-6;
  4. const WIDTH = 1000;
  5. $now = time();
  6. $today = strtotime("today");
  7. $dow = date('N', $now );
  8. $im = imagecreatetruecolor( WIDTH, 100 );
  9. imagefilledrectangle( $im, 0, 0, WIDTH-1, 99, 0 );
  10. for ( $lastX=WIDTH-1, $t =- ( $now - $today ) % ( 24 * 3600 ); true; $t -= 24 * 3600 ){
  11.     $x = WIDTH * exp( $t * MULTIPLIER );
  12.     imageline( $im, $x, 0, $x, 99, 255 * ( $x / WIDTH ) );
  13.     if ( $dow < 6 ){ // senin-jumat masuk kerja
  14.         $x1 = WIDTH * exp( ( $t + 7 * 3600 ) * MULTIPLIER ); // jam 07 masuk kerja
  15.         imageline( $im, $x1, 0, $x1, 99, 80 * ( $x / WIDTH ) );
  16.         $x1 = WIDTH * exp( ( $t + ( $dow == 5 ? 13 : 16 ) * 3600 ) * MULTIPLIER ); // jam 16 pulang, atau jam 13 jumat
  17.         imageline( $im, $x1, 0, $x1, 99, 80 * ( $x / WIDTH ) );
  18.     }
  19.     $dow = ( $dow + 12 ) % 7 + 1;
  20.     foreach ( array( 10, 25, 50, 75, 90 ) as $v )
  21.         imageline( $im, $x, $v, $lastX, $v, 80 * ( $x / WIDTH ) );
  22.     $lastX = $x;
  23.     if ( $x < 1 ) break;
  24. }
  25.  
  26. $contents = file_get_contents( "D:/scripts/.gsBatteryLog.txt" ); // Log file location
  27. foreach ( explode( "\n", $contents ) as $row ){
  28.     if ( preg_match( "#(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d) (\d+)#", $row, $m ) ){
  29.         $t = strtotime( str_replace("T", " ", $m[1]) ) - $now;
  30.         $x = $t * MULTIPLIER;
  31.         $x = (WIDTH-1) * exp( $x );
  32.         $y = 100 - $m[2];
  33.         imagesetpixel( $im, $x, $y, ( 255 * ( $x / WIDTH ) ) << 8 );
  34.         $lastY = $y;
  35.     }
  36. }
  37. header( "Content-Type: image/png" );
  38. imagepng( $im );
RAW Paste Data