-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathSmiley.class.php
More file actions
159 lines (142 loc) · 4.44 KB
/
Smiley.class.php
File metadata and controls
159 lines (142 loc) · 4.44 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace wcf\data\smiley;
use wcf\data\DatabaseObject;
use wcf\data\ITitledObject;
use wcf\system\WCF;
use wcf\util\StringUtil;
/**
* Represents a smiley.
*
* @author Alexander Ebert
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @property-read int $smileyID unique id of the smiley
* @property-read int $packageID id of the package which delivers the smiley
* @property-read ?int $categoryID id of the category the smiley belongs to or `null` if it belongs to the default category
* @property-read string $smileyPath path to the smiley file relative to wcf's default path
* @property-read string $smileyPath2x path to the smiley file relative to wcf's default path (2x version)
* @property-read string $smileyTitle title of the smiley or name of language item that contains the title
* @property-read string $smileyCode code used for displaying the smiley
* @property-read string $aliases alternative codes used for displaying the smiley
* @property-read int $showOrder position of the smiley in relation to the other smileys in the same category
* @property-read string $emoji unicode emoji that replaces this smiley when content is reprocessed
*/
class Smiley extends DatabaseObject implements ITitledObject
{
/**
* @var ?int
*/
protected $height;
/**
* @var ?int
*/
protected $width;
/**
* @var string[]
*/
public $smileyCodes;
/**
* @since 5.2
*/
#[\Override]
public function getTitle(): string
{
return WCF::getLanguage()->get($this->smileyTitle);
}
/**
* Returns the url to this smiley.
*
* @return string
*/
public function getURL()
{
return WCF::getPath() . $this->smileyPath;
}
/**
* Returns the url to the 2x version of the smiley.
*
* @return string
*/
public function getURL2x()
{
return ($this->smileyPath2x) ? WCF::getPath() . $this->smileyPath2x : '';
}
/**
* Returns all aliases for this smiley.
*
* @return string[]
*/
public function getAliases()
{
if (!$this->aliases) {
return [];
}
return \explode("\n", StringUtil::unifyNewlines($this->aliases));
}
/**
* Returns the dimensions of the smiley.
*
* @since 5.4
* @return int[]
*/
public function getDimensions()
{
if ($this->height === null) {
$this->height = $this->width = 0;
$file = \WCF_DIR . $this->smileyPath;
if (\file_exists($file) && \preg_match('~\.(gif|jpe?g|png|webp)$~', $file)) {
$data = \getimagesize($file);
if ($data !== false) {
// The first two indices of `getimagesize()` represent the image dimensions.
[$this->width, $this->height] = $data;
}
}
}
return [
'width' => $this->width,
'height' => $this->height,
];
}
/**
* Returns the height of the smiley.
*
* @return int
*/
public function getHeight()
{
return $this->getDimensions()['height'];
}
/**
* Returns the width of the smiley.
*
* @since 5.4
* @return int
*/
public function getWidth()
{
return $this->getDimensions()['width'];
}
/**
* Returns the html code to render the smiley.
*
* @param string $class (additional) class(es) of the smiley element
* @return string
*/
public function getHtml(string $class = '')
{
$srcset = ($this->smileyPath2x) ? ' srcset="' . StringUtil::encodeHTML($this->getURL2x()) . ' 2x"' : '';
$height = ($this->getHeight()) ? ' height="' . $this->getHeight() . '"' : '';
$width = ($this->getWidth()) ? ' width="' . $this->getWidth() . '"' : '';
return \sprintf(
'<img src="%s" alt="%s" title="%s" class="%s" %s %s %s loading="eager" translate="no">',
StringUtil::encodeHTML($this->getURL()),
StringUtil::encodeHTML($this->smileyCode),
StringUtil::encodeHTML(WCF::getLanguage()->get($this->smileyTitle)),
'smiley' . ($class ? " {$class}" : ''),
$srcset,
$height,
$width,
);
}
}