ee129f663af36e5febaee7dcb3d3100126d99e63
3 namespace wcf\data\smiley
;
5 use wcf\data\DatabaseObject
;
6 use wcf\data\ITitledObject
;
8 use wcf\util\StringUtil
;
11 * Represents a smiley.
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @property-read int $smileyID unique id of the smiley
18 * @property-read int $packageID id of the package which delivers the smiley
19 * @property-read int|null $categoryID id of the category the smiley belongs to or `null` if it belongs to the default category
20 * @property-read string $smileyPath path to the smiley file relative to wcf's default path
21 * @property-read string $smileyPath2x path to the smiley file relative to wcf's default path (2x version)
22 * @property-read string $smileyTitle title of the smiley or name of language item that contains the title
23 * @property-read string $smileyCode code used for displaying the smiley
24 * @property-read string $aliases alternative codes used for displaying the smiley
25 * @property-read int $showOrder position of the smiley in relation to the other smileys in the same category
27 class Smiley
extends DatabaseObject
implements ITitledObject
39 public function getTitle(): string
41 return WCF
::getLanguage()->get($this->smileyTitle
);
45 * Returns the url to this smiley.
49 public function getURL()
51 return WCF
::getPath() . $this->smileyPath
;
55 * Returns the url to the 2x version of the smiley.
59 public function getURL2x()
61 return ($this->smileyPath2x
) ? WCF
::getPath() . $this->smileyPath2x
: '';
65 * Returns all aliases for this smiley.
69 public function getAliases()
71 if (!$this->aliases
) {
75 return \
explode("\n", StringUtil
::unifyNewlines($this->aliases
));
79 * Returns the dimensions of the smiley.
84 public function getDimensions()
86 if ($this->height
=== null) {
87 $this->height
= $this->width
= 0;
89 $file = WCF_DIR
. $this->smileyPath
;
90 if (\file_exists
($file) && \
preg_match('~\.(gif|jpe?g|png|webp)$~', $file)) {
91 $data = \
getimagesize($file);
92 if ($data !== false) {
93 // The first two indices of `getimagesize()` represent the image dimensions.
94 [$this->width
, $this->height
] = $data;
100 'width' => $this->width
,
101 'height' => $this->height
,
106 * Returns the height of the smiley.
110 public function getHeight()
112 return $this->getDimensions()['height'];
116 * Returns the width of the smiley.
121 public function getWidth()
123 return $this->getDimensions()['width'];
127 * Returns the html code to render the smiley.
129 * @param string $class (additional) class(es) of the smiley element
132 public function getHtml($class = '')
134 $srcset = ($this->smileyPath2x
) ?
' srcset="' . StringUtil
::encodeHTML($this->getURL2x()) . ' 2x"' : '';
135 $height = ($this->getHeight()) ?
' height="' . $this->getHeight() . '"' : '';
136 $width = ($this->getWidth()) ?
' width="' . $this->getWidth() . '"' : '';
139 '<img src="%s" alt="%s" title="%s" class="%s" %s %s %s loading="eager" translate="no">',
140 StringUtil
::encodeHTML($this->getURL()),
141 StringUtil
::encodeHTML($this->smileyCode
),
142 StringUtil
::encodeHTML(WCF
::getLanguage()->get($this->smileyTitle
)),
143 'smiley' . ($class ?
" {$class}" : ''),