Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('APPROOT', dirname(basename(__FILE__)));
- define('IMAGEROOT', APPROOT . '/images/');
- define('SIZE_X', 200);
- define('SIZE_Y', 200);
- define('CHANGE_TIME', 3600);
- /**
- * Resize image, keeping aspect ratio. Image will be within the max sizes specified
- * return an array containing resized image resource, neww, newh
- *
- * @param resource $image
- * @param int $iw (current width)
- * @param int $ih (current height)
- * @param int $maxw (max permitted width)
- * @param int $maxh (max permitted height)
- * @return array(resource $newImg, int $neww, int $newh)
- */
- function resizeImage($image, $iw, $ih, $maxw, $maxh)
- {
- if ($iw > $maxw || $ih > $maxh){
- if ($iw>$maxw && $ih<=$maxh){//too wide, height is OK
- $proportion=($maxw*100)/$iw;
- $neww=$maxw;
- $newh=ceil(($ih*$proportion)/100);
- }
- else if ($iw<=$maxw && $ih>$maxh){//too high, width is OK
- $proportion=($maxh*100)/$ih;
- $newh=$maxh;
- $neww=ceil(($iw*$proportion)/100);
- }
- else {//too high and too wide
- if ($iw/$maxw > $ih/$maxh){//width is the bigger problem
- $proportion=($maxw*100)/$iw;
- $neww=$maxw;
- $newh=ceil(($ih*$proportion)/100);
- }
- else {//height is the bigger problem
- $proportion=($maxh*100)/$ih;
- $newh=$maxh;
- $neww=ceil(($iw*$proportion)/100);
- }
- }
- }
- else {//copy image even if not resizing
- $neww=$iw;
- $newh=$ih;
- }
- if (function_exists("imagecreatetruecolor")){//GD 2.0=good!
- $newImage=imagecreatetruecolor($neww, $newh);
- imagecopyresampled($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih);
- } else {//GD 1.8=only 256 colours
- $newImage=imagecreate($neww, $newh);
- imagecopyresized($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih);
- }
- return Array($newImage, $neww, $newh);
- }
- $lasttime = null;
- if (file_exists(APPROOT . '/lasttime')) {
- $lasttime = file_get_contents(APPROOT . '/lasttime');
- }
- header('Content-Type: image/png');
- if ($lasttime + CHANGE_TIME >= time() && file_exists(APPROOT . '/curavatar.png')) {
- $image = @imagecreatefrompng(APPROOT . '/curavatar.png');
- @imagepng($image);
- @imagedestroy($image);
- exit;
- }
- $images = scandir(IMAGEROOT);
- while (true) {
- $selectedFile = IMAGEROOT . '/' . $images[array_rand($images)];
- if (file_exists($selectedFile) && !is_dir($selectedFile) && is_readable($selectedFile)) {
- $image = @imagecreatefromstring(file_get_contents($selectedFile));
- $size = @getimagesize($selectedFile);
- $ret = @resizeImage($image, $size[0], $size[1], SIZE_X, SIZE_Y);
- $image = $ret[0];
- @imagepng($image, APPROOT . '/curavatar.png');
- @imagepng($image);
- @imagedestroy($image);
- break;
- }
- }
- file_put_contents(APPROOT . '/lasttime', time());
Advertisement
Add Comment
Please, Sign In to add comment