xref: /dokuwiki/inc/Ui/Media/Display.php (revision 4f33babf5709281d9b8753c9620d1257ced36f67)
1bf9684bcSAndreas Gohr<?php
2bf9684bcSAndreas Gohr
3bf9684bcSAndreas Gohrnamespace dokuwiki\Ui\Media;
4bf9684bcSAndreas Gohr
5bf9684bcSAndreas Gohruse dokuwiki\Media\MediaFile;
6bf9684bcSAndreas Gohr
7bf9684bcSAndreas Gohrclass Display
8bf9684bcSAndreas Gohr{
9bf9684bcSAndreas Gohr
10bf9684bcSAndreas Gohr    protected $mediaFile;
11bf9684bcSAndreas Gohr
12bf9684bcSAndreas Gohr    /**
13bf9684bcSAndreas Gohr     * Display constructor.
14bf9684bcSAndreas Gohr     * @param MediaFile $mediaFile
15bf9684bcSAndreas Gohr     */
16bf9684bcSAndreas Gohr    public function __construct(MediaFile $mediaFile)
17bf9684bcSAndreas Gohr    {
18bf9684bcSAndreas Gohr        $this->mediaFile = $mediaFile;
19bf9684bcSAndreas Gohr    }
20bf9684bcSAndreas Gohr
21bf9684bcSAndreas Gohr    /**
22bf9684bcSAndreas Gohr     * Get the HTML to display a preview image if possible, otherwise show an icon
23bf9684bcSAndreas Gohr     *
24bf9684bcSAndreas Gohr     * @param int $w bounding box width to resize pixel based images to
25bf9684bcSAndreas Gohr     * @param int $h bounding box height to resize pixel based images to
26bf9684bcSAndreas Gohr     * @return string
27bf9684bcSAndreas Gohr     */
28bf9684bcSAndreas Gohr    public function getPreviewHtml($w, $h)
29bf9684bcSAndreas Gohr    {
30bf9684bcSAndreas Gohr        if ($this->mediaFile->isImage()) {
31*4f33babfSAndreas Gohr            $src = ml($this->mediaFile->getId(), ['w' => $w, 'h' => $h]);
32bf9684bcSAndreas Gohr        } else {
33bf9684bcSAndreas Gohr            $src = $this->getIconUrl();
34bf9684bcSAndreas Gohr        }
35bf9684bcSAndreas Gohr
36bf9684bcSAndreas Gohr        return '<img src="' . $src . '" alt="' . hsc($this->mediaFile->getDisplayName()) . '" loading="lazy" />';
37bf9684bcSAndreas Gohr    }
38bf9684bcSAndreas Gohr
39bf9684bcSAndreas Gohr    /**
40bf9684bcSAndreas Gohr     * Return the URL to the icon for this file
41bf9684bcSAndreas Gohr     *
42bf9684bcSAndreas Gohr     * @return string
43bf9684bcSAndreas Gohr     */
44bf9684bcSAndreas Gohr    public function getIconUrl()
45bf9684bcSAndreas Gohr    {
46bf9684bcSAndreas Gohr        $link = 'lib/images/fileicons/svg/' . $this->mediaFile->getIcoClass() . '.svg';
47bf9684bcSAndreas Gohr        if (!file_exists(DOKU_INC . $link)) $link = 'lib/images/fileicons/svg/file.svg';
48bf9684bcSAndreas Gohr        return DOKU_BASE . $link;
49bf9684bcSAndreas Gohr    }
50bf9684bcSAndreas Gohr
51bf9684bcSAndreas Gohr    /** @return string */
52bf9684bcSAndreas Gohr    protected function formatDate()
53bf9684bcSAndreas Gohr    {
54bf9684bcSAndreas Gohr        return dformat($this->mediaFile->getLastModified());
55bf9684bcSAndreas Gohr    }
56bf9684bcSAndreas Gohr
57bf9684bcSAndreas Gohr    /**
58bf9684bcSAndreas Gohr     * Output the image dimension if any
59bf9684bcSAndreas Gohr     *
60bf9684bcSAndreas Gohr     * @param string $empty what to show when no dimensions are available
61bf9684bcSAndreas Gohr     * @return string
62bf9684bcSAndreas Gohr     */
63bf9684bcSAndreas Gohr    protected function formatDimensions($empty = '&#160')
64bf9684bcSAndreas Gohr    {
65bf9684bcSAndreas Gohr        $w = $this->mediaFile->getWidth();
66bf9684bcSAndreas Gohr        $h = $this->mediaFile->getHeight();
67bf9684bcSAndreas Gohr        if ($w && $h) {
68bf9684bcSAndreas Gohr            return $w . '&#215;' . $h;
69bf9684bcSAndreas Gohr        } else {
70bf9684bcSAndreas Gohr            return $empty;
71bf9684bcSAndreas Gohr        }
72bf9684bcSAndreas Gohr    }
73bf9684bcSAndreas Gohr
74bf9684bcSAndreas Gohr    /** @return string */
75bf9684bcSAndreas Gohr    protected function formatFileSize()
76bf9684bcSAndreas Gohr    {
77bf9684bcSAndreas Gohr        return filesize_h($this->mediaFile->getFileSize());
78bf9684bcSAndreas Gohr    }
79bf9684bcSAndreas Gohr}
80