Guest User

Simphax

a guest
Jan 24th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?php
  2.  
  3. /* CONFIG */
  4.  
  5. $skinurl = 'http://s3.amazonaws.com/MinecraftSkins/'; //Where to download the full skins.
  6.  
  7. $cachelifetime = 7; // Set how long time (in days) the face will be loaded from a cached copy before updating from minecraft.net. Set to 0 if you do not want this feature.
  8.  
  9. $cachepath = 'cache/'; // Where to store cached copies.
  10.  
  11. $timeout = 2; // Timeout for fetching a skin png from minecraft.net. Needed if their servers die.
  12.  
  13. /* END CONFIG */
  14.  
  15. function fallback() {
  16.     header('Content-Type: image/png');
  17.     readfile('player.png');
  18.     exit();
  19. }
  20.  
  21. if(isset($_GET['nick'])) {
  22.  
  23.     $nick = $_GET['nick'];
  24.     $cachedpng = $cachepath.$nick.'.png';
  25.     $skinurl = $skinurl.$nick.'.png';
  26.     $old = ini_set('default_socket_timeout', $timeout);
  27.    
  28.     if (file_exists($cachedpng) && filemtime($cachedpng) >= (time() - ($cachelifetime * 24 * 60 * 60))) {
  29.        
  30.         header('Content-Type: image/png');
  31.         readfile($cachedpng);
  32.        
  33.         exit();
  34.    
  35.     } elseif ($file = @fopen($skinurl, "r")) {
  36.        
  37.             $src = @imagecreatefrompng($skinurl)
  38.                 or fallback();
  39.             $dest = imagecreatetruecolor(24, 24);
  40.            
  41.             imagecopyresized($dest, $src, 0, 0, 8, 8, 24, 24, 8, 8);
  42.            
  43.             header('Content-Type: image/png');
  44.             imagepng($dest);
  45.            
  46.             if($cachelifetime != 0)
  47.                 imagepng($dest,$cachedpng);
  48.            
  49.             imagedestroy($dest);
  50.             imagedestroy($src);
  51.            
  52.             exit();
  53.        
  54.     }
  55.    
  56.     ini_set('default_socket_timeout', $old);
  57.    
  58. } elseif (isset($_GET['clearcache'])) {
  59.  
  60.     $mask = $cachepath.'*.png';
  61.     array_map( 'unlink', glob( $mask ) );
  62.    
  63.     echo 'Cache clear: DONE';
  64.    
  65.     exit();
  66.  
  67. }
  68.  
  69. // On error or no input:
  70. fallback();
  71.    
  72. ?>
Add Comment
Please, Sign In to add comment