+ $width = intval($width);
+ $height = !is_null($height) ? intval($height) : null;
+ if (is_null($height)) {
+ $height = $width;
+ }
+
+ $size = "{$width}x{$height}";
+
+ if (!isset(self::$_avatars[$target->id])) {
+ self::$_avatars[$target->id] = array();
+ } elseif (isset(self::$_avatars[$target->id][$size])){
+ return self::$_avatars[$target->id][$size];
+ }
+
+ $avatar = null;
+
+ if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
+ $avatar = self::pkeyGet(
+ array(
+ 'profile_id' => $target->id,
+ 'width' => $width,
+ 'height' => $height,
+ )
+ );
+
+ Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
+ }
+
+ if (is_null($avatar)) {
+ // Obviously we can't find an avatar, so let's resize the original!
+ $avatar = Avatar::newSize($target, $width);
+ } elseif (!($avatar instanceof Avatar)) {
+ throw new NoAvatarException($target, $avatar);
+ }
+
+ self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
+ return $avatar;
+ }
+
+ public static function getUploaded(Profile $target)
+ {
+ $avatar = new Avatar();
+ $avatar->profile_id = $target->id;
+ $avatar->original = true;
+ if (!$avatar->find(true)) {
+ throw new NoAvatarException($target, $avatar);
+ }
+ if (!file_exists(Avatar::path($avatar->filename))) {
+ // The delete call may be odd for, say, unmounted filesystems
+ // that cause a file to currently not exist, but actually it does...
+ $avatar->delete();
+ throw new NoAvatarException($target, $avatar);
+ }
+ return $avatar;
+ }
+
+ public static function getProfileAvatars(Profile $target) {
+ $avatar = new Avatar();
+ $avatar->profile_id = $target->id;
+ if (!$avatar->find()) {
+ throw new NoAvatarException($target, $avatar);
+ }
+ return $avatar->fetchAll();