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