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