2 namespace wcf\data\user\avatar
;
3 use wcf\data\DatabaseObject
;
4 use wcf\util\StringUtil
;
8 * Represents a user's avatar.
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
17 class UserAvatar
extends DatabaseObject
implements IUserAvatar
{
19 * needed avatar thumbnail sizes
22 public static $avatarThumbnailSizes = array(32, 96, 128);
25 * @see \wcf\data\DatabaseObject::$databaseTableName
27 protected static $databaseTableName = 'user_avatar';
30 * @see \wcf\data\DatabaseObject::$databaseTableIndexName
32 protected static $databaseTableIndexName = 'avatarID';
35 * maximum thumbnail size
38 public static $maxThumbnailSize = 128;
41 * minimum height and width of an uploaded avatar
44 const MIN_AVATAR_SIZE
= 96;
47 * Returns the physical location of this avatar.
49 * @param integer $size
52 public function getLocation($size = null) {
53 return WCF_DIR
. 'images/avatars/' . $this->getFilename($size);
57 * Returns the file name of this avatar.
59 * @param integer $size
62 public function getFilename($size = null) {
71 if ($this->width
> 96 ||
$this->height
> 96) {
80 return substr($this->fileHash
, 0, 2) . '/' . ($this->avatarID
) . '-' . $this->fileHash
. ($size !== null ?
('-' . $size) : '') . '.' . $this->avatarExtension
;
84 * @see \wcf\data\user\avatar\IUserAvatar::getURL()
86 public function getURL($size = null) {
87 if ($size !== null && $size !== 'resized') {
88 if ($size >= $this->width ||
$size >= $this->height
) $size = null;
91 return WCF
::getPath() . 'images/avatars/' . $this->getFilename($size);
95 * @see \wcf\data\user\avatar\IUserAvatar::getImageTag()
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;
104 else if ($this->width
> $size ||
$this->height
> $size) {
105 $widthFactor = $size / $this->width
;
106 $heightFactor = $size / $this->height
;
108 if ($widthFactor < $heightFactor) {
110 $height = round($this->height
* $widthFactor, 0);
113 $width = round($this->width
* $heightFactor, 0);
133 if ($this->width
>= 128 && $this->height
>= 128) {
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" />';
143 * @see \wcf\data\user\avatar\IUserAvatar::getCropImageTag()
145 public function getCropImageTag($size = null) {
146 $imageTag = $this->getImageTag($size);
148 // append CSS classes and append title
149 $title = StringUtil
::encodeHTML(WCF
::getLanguage()->get('wcf.user.avatar.type.custom.crop'));
151 return str_replace('class="userAvatarImage"', 'class="userAvatarImage userAvatarCrop jsTooltip" title="'.$title.'"', $imageTag);
155 * @see \wcf\data\user\avatar\IUserAvatar::getWidth()
157 public function getWidth() {
162 * @see \wcf\data\user\avatar\IUserAvatar::getHeight()
164 public function getHeight() {
165 return $this->height
;
169 * @see \wcf\data\user\avatar\IUserAvatar::canCrop()
171 public function canCrop() {
172 return $this->width
!= $this->height
&& $this->width
> self
::$maxThumbnailSize && $this->height
> self
::$maxThumbnailSize;