-
Notifications
You must be signed in to change notification settings - Fork 12
/
Avatar_body.php
78 lines (66 loc) · 1.81 KB
/
Avatar_body.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Avatar;
class Avatars {
public static function getLinkFor($username, $res = false) {
global $wgScriptPath;
$path = "$wgScriptPath/extensions/Avatar/avatar.php?user=$username";
if ($res !== false) {
return $path . '&res=' . $res;
} else {
return $path;
}
}
public static function normalizeResolution($res) {
if ($res === 'original') {
return 'original';
}
$res = intval($res);
global $wgAllowedAvatarRes;
foreach ($wgAllowedAvatarRes as $r) {
if ($res <= $r) {
return $r;
}
}
return 'original';
}
public static function getAvatar(\User $user, $res) {
global $wgDefaultAvatarRes;
$path = null;
// If user exists
if ($user && $user->getId()) {
global $wgAvatarUploadDirectory;
$avatarPath = "/{$user->getId()}/$res.png";
// Check if requested avatar thumbnail exists
if (file_exists($wgAvatarUploadDirectory . $avatarPath)) {
$path = $avatarPath;
} else if ($res !== 'original') {
// Dynamically generate upon request
$originalAvatarPath = "/{$user->getId()}/original.png";
if (file_exists($wgAvatarUploadDirectory . $originalAvatarPath)) {
$image = Thumbnail::open($wgAvatarUploadDirectory . $originalAvatarPath);
$image->createThumbnail($res, $wgAvatarUploadDirectory . $avatarPath);
$image->cleanup();
$path = $avatarPath;
}
}
}
return $path;
}
public static function hasAvatar(\User $user) {
global $wgDefaultAvatar;
return self::getAvatar($user, 'original') !== null;
}
public static function deleteAvatar(\User $user) {
global $wgAvatarUploadDirectory;
$dirPath = $wgAvatarUploadDirectory . "/{$user->getId()}/";
if (!is_dir($dirPath)) {
return false;
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
unlink($file);
}
rmdir($dirPath);
return true;
}
}