Guest User

Random Avatar

a guest
Jan 10th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?php
  2. define('APPROOT', dirname(basename(__FILE__)));
  3. define('IMAGEROOT', APPROOT . '/images/');
  4. define('SIZE_X', 200);
  5. define('SIZE_Y', 200);
  6. define('CHANGE_TIME', 3600);
  7.  
  8. /**
  9. * Resize image, keeping aspect ratio. Image will be within the max sizes specified
  10. * return an array containing resized image resource, neww, newh
  11. *
  12. * @param resource $image
  13. * @param int $iw (current width)
  14. * @param int $ih (current height)
  15. * @param int $maxw (max permitted width)
  16. * @param int $maxh (max permitted height)
  17. * @return array(resource $newImg, int $neww, int $newh)
  18. */
  19. function resizeImage($image, $iw, $ih, $maxw, $maxh)
  20. {
  21.     if ($iw > $maxw || $ih > $maxh){
  22.  
  23.         if ($iw>$maxw && $ih<=$maxh){//too wide, height is OK
  24.             $proportion=($maxw*100)/$iw;
  25.             $neww=$maxw;
  26.             $newh=ceil(($ih*$proportion)/100);
  27.         }
  28.        
  29.         else if ($iw<=$maxw && $ih>$maxh){//too high, width is OK
  30.             $proportion=($maxh*100)/$ih;
  31.             $newh=$maxh;
  32.             $neww=ceil(($iw*$proportion)/100);
  33.         }
  34.        
  35.         else {//too high and too wide
  36.  
  37.             if ($iw/$maxw > $ih/$maxh){//width is the bigger problem
  38.                 $proportion=($maxw*100)/$iw;
  39.                 $neww=$maxw;
  40.                 $newh=ceil(($ih*$proportion)/100);
  41.             }
  42.            
  43.             else {//height is the bigger problem
  44.                 $proportion=($maxh*100)/$ih;
  45.                 $newh=$maxh;
  46.                 $neww=ceil(($iw*$proportion)/100);
  47.             }
  48.         }
  49.     }
  50.    
  51.     else {//copy image even if not resizing
  52.         $neww=$iw;
  53.         $newh=$ih;
  54.     }
  55.    
  56.     if (function_exists("imagecreatetruecolor")){//GD 2.0=good!
  57.         $newImage=imagecreatetruecolor($neww, $newh);
  58.         imagecopyresampled($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih);
  59.     } else {//GD 1.8=only 256 colours
  60.         $newImage=imagecreate($neww, $newh);
  61.         imagecopyresized($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih);
  62.     }
  63.  
  64.     return Array($newImage, $neww, $newh);
  65. }
  66.  
  67. $lasttime = null;
  68. if (file_exists(APPROOT . '/lasttime')) {
  69.     $lasttime = file_get_contents(APPROOT . '/lasttime');
  70. }
  71.  
  72. header('Content-Type: image/png');
  73.  
  74. if ($lasttime + CHANGE_TIME >= time() && file_exists(APPROOT . '/curavatar.png')) {
  75.     $image = @imagecreatefrompng(APPROOT . '/curavatar.png');
  76.     @imagepng($image);
  77.     @imagedestroy($image);
  78.     exit;
  79. }
  80.  
  81. $images = scandir(IMAGEROOT);
  82. while (true) {
  83.     $selectedFile = IMAGEROOT . '/' . $images[array_rand($images)];
  84.     if (file_exists($selectedFile) && !is_dir($selectedFile) && is_readable($selectedFile)) {
  85.         $image = @imagecreatefromstring(file_get_contents($selectedFile));
  86.         $size = @getimagesize($selectedFile);
  87.        
  88.         $ret = @resizeImage($image, $size[0], $size[1], SIZE_X, SIZE_Y);
  89.         $image = $ret[0];
  90.         @imagepng($image, APPROOT . '/curavatar.png');
  91.         @imagepng($image);
  92.         @imagedestroy($image);
  93.         break;
  94.     }
  95. }
  96.  
  97. file_put_contents(APPROOT . '/lasttime', time());
Advertisement
Add Comment
Please, Sign In to add comment