Removed broken alt-attribute in avatar img tags
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / avatar / UserAvatar.class.php
1 <?php
2 namespace wcf\data\user\avatar;
3 use wcf\data\DatabaseObject;
4 use wcf\util\StringUtil;
5 use wcf\system\WCF;
6
7 /**
8 * Represents a user's avatar.
9 *
10 * @author Alexander Ebert
11 * @copyright 2001-2015 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package com.woltlab.wcf
14 * @subpackage data.user.avatar
15 * @category Community Framework
16 */
17 class UserAvatar extends DatabaseObject implements IUserAvatar {
18 /**
19 * needed avatar thumbnail sizes
20 * @var array<integer>
21 */
22 public static $avatarThumbnailSizes = array(32, 96, 128);
23
24 /**
25 * @see \wcf\data\DatabaseObject::$databaseTableName
26 */
27 protected static $databaseTableName = 'user_avatar';
28
29 /**
30 * @see \wcf\data\DatabaseObject::$databaseTableIndexName
31 */
32 protected static $databaseTableIndexName = 'avatarID';
33
34 /**
35 * maximum thumbnail size
36 * @var integer
37 */
38 public static $maxThumbnailSize = 128;
39
40 /**
41 * minimum height and width of an uploaded avatar
42 * @var integer
43 */
44 const MIN_AVATAR_SIZE = 96;
45
46 /**
47 * Returns the physical location of this avatar.
48 *
49 * @param integer $size
50 * @return string
51 */
52 public function getLocation($size = null) {
53 return WCF_DIR . 'images/avatars/' . $this->getFilename($size);
54 }
55
56 /**
57 * Returns the file name of this avatar.
58 *
59 * @param integer $size
60 * @return string
61 */
62 public function getFilename($size = null) {
63 switch ($size) {
64 case 16:
65 case 24:
66 $size = 32;
67 break;
68
69 case 48:
70 case 64:
71 if ($this->width > 96 || $this->height > 96) {
72 $size = 96;
73 }
74 else {
75 $size = null;
76 }
77 break;
78 }
79
80 return substr($this->fileHash, 0, 2) . '/' . ($this->avatarID) . '-' . $this->fileHash . ($size !== null ? ('-' . $size) : '') . '.' . $this->avatarExtension;
81 }
82
83 /**
84 * @see \wcf\data\user\avatar\IUserAvatar::getURL()
85 */
86 public function getURL($size = null) {
87 if ($size !== null && $size !== 'resized') {
88 if ($size >= $this->width || $size >= $this->height) $size = null;
89 }
90
91 return WCF::getPath() . 'images/avatars/' . $this->getFilename($size);
92 }
93
94 /**
95 * @see \wcf\data\user\avatar\IUserAvatar::getImageTag()
96 */
97 public function getImageTag($size = null) {
98 $width = $this->width;
99 $height = $this->height;
100 if ($size !== null) {
101 if ($this->width > $size && $this->height > $size) {
102 $width = $height = $size;
103 }
104 else if ($this->width > $size || $this->height > $size) {
105 $widthFactor = $size / $this->width;
106 $heightFactor = $size / $this->height;
107
108 if ($widthFactor < $heightFactor) {
109 $width = $size;
110 $height = round($this->height * $widthFactor, 0);
111 }
112 else {
113 $width = round($this->width * $heightFactor, 0);
114 $height = $size;
115 }
116 }
117 }
118
119 $retinaSize = null;
120 switch ($size) {
121 case 16:
122 $retinaSize = 32;
123 break;
124
125 case 24:
126 case 32:
127 case 48:
128 $retinaSize = 96;
129 break;
130
131 case 64:
132 case 96:
133 if ($this->width >= 128 && $this->height >= 128) {
134 $retinaSize = 128;
135 }
136 break;
137 }
138
139 return '<img src="'.StringUtil::encodeHTML($this->getURL($size)).'" '.($retinaSize !== null ? ('srcset="'.StringUtil::encodeHTML($this->getURL($retinaSize)).' 2x" ') : '').'style="width: '.$width.'px; height: '.$height.'px" alt="" class="userAvatarImage" />';
140 }
141
142 /**
143 * @see \wcf\data\user\avatar\IUserAvatar::getCropImageTag()
144 */
145 public function getCropImageTag($size = null) {
146 $imageTag = $this->getImageTag($size);
147
148 // append CSS classes and append title
149 $title = StringUtil::encodeHTML(WCF::getLanguage()->get('wcf.user.avatar.type.custom.crop'));
150
151 return str_replace('class="userAvatarImage"', 'class="userAvatarImage userAvatarCrop jsTooltip" title="'.$title.'"', $imageTag);
152 }
153
154 /**
155 * @see \wcf\data\user\avatar\IUserAvatar::getWidth()
156 */
157 public function getWidth() {
158 return $this->width;
159 }
160
161 /**
162 * @see \wcf\data\user\avatar\IUserAvatar::getHeight()
163 */
164 public function getHeight() {
165 return $this->height;
166 }
167
168 /**
169 * @see \wcf\data\user\avatar\IUserAvatar::canCrop()
170 */
171 public function canCrop() {
172 return $this->width != $this->height && $this->width > self::$maxThumbnailSize && $this->height > self::$maxThumbnailSize;
173 }
174 }