Last active
August 29, 2015 14:01
-
-
Save jasonclemons/fa9372274bedae9f6371 to your computer and use it in GitHub Desktop.
Simple PHP script to rotate avatar images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Avatar | |
{ | |
public $avatar_dir; | |
public function headers() | |
{ | |
header('Content-type: image/jpeg'); | |
} | |
public function generate_avatar() | |
{ | |
// Populate an array with all files in avatar directory | |
$images = array_diff(scandir($this->avatar_dir), array('.', '..', 'index.php')); | |
// Count how many images we're rotating through | |
$count = count($images); | |
// Generate a random number | |
$rand = rand(0, $count); | |
$image = $this->avatar_dir . '/' . $images[$rand]; | |
$avatar = @imagecreatefromjpeg($image); | |
if (!$avatar) { | |
exit('Avatar not found'); | |
} | |
imagejpeg($avatar); | |
imagedestroy($avatar); | |
} | |
} | |
$img = new Avatar(); | |
$img->avatar_dir = __DIR__ . '/avatars'; | |
$img->headers(); | |
$img->generate_avatar(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment