Skip to content

Instantly share code, notes, and snippets.

@jasonclemons
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonclemons/fa9372274bedae9f6371 to your computer and use it in GitHub Desktop.
Save jasonclemons/fa9372274bedae9f6371 to your computer and use it in GitHub Desktop.
Simple PHP script to rotate avatar images.
<?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