xref: /webtrees/app/MediaFile.php (revision 60e3c46a332906949fc288e4ea34cb0f20109d5f)
18f5f5da8SGreg Roach<?php
28f5f5da8SGreg Roach/**
38f5f5da8SGreg Roach * webtrees: online genealogy
48f5f5da8SGreg Roach * Copyright (C) 2017 webtrees development team
58f5f5da8SGreg Roach * This program is free software: you can redistribute it and/or modify
68f5f5da8SGreg Roach * it under the terms of the GNU General Public License as published by
78f5f5da8SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88f5f5da8SGreg Roach * (at your option) any later version.
98f5f5da8SGreg Roach * This program is distributed in the hope that it will be useful,
108f5f5da8SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118f5f5da8SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128f5f5da8SGreg Roach * GNU General Public License for more details.
138f5f5da8SGreg Roach * You should have received a copy of the GNU General Public License
148f5f5da8SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158f5f5da8SGreg Roach */
168f5f5da8SGreg Roachnamespace Fisharebest\Webtrees;
178f5f5da8SGreg Roach
184a9f750fSGreg Roachuse ErrorException;
198f5f5da8SGreg Roachuse League\Glide\Urls\UrlBuilderFactory;
208f5f5da8SGreg Roach
218f5f5da8SGreg Roach/**
228f5f5da8SGreg Roach * A GEDCOM media file.  A media object can contain many media files,
238f5f5da8SGreg Roach * such as scans of both sides of a document, the transcript of an audio
248f5f5da8SGreg Roach * recording, etc.
258f5f5da8SGreg Roach */
268f5f5da8SGreg Roachclass MediaFile {
278f5f5da8SGreg Roach	/** @var string The filename */
288f5f5da8SGreg Roach	private $multimedia_file_refn = '';
298f5f5da8SGreg Roach
308f5f5da8SGreg Roach	/** @var string The file extension; jpeg, txt, mp4, etc. */
318f5f5da8SGreg Roach	private $multimedia_format = '';
328f5f5da8SGreg Roach
338f5f5da8SGreg Roach	/** @var string The type of document; newspaper, microfiche, etc. */
348f5f5da8SGreg Roach	private $source_media_type = '';
358f5f5da8SGreg Roach	/** @var string The filename */
368f5f5da8SGreg Roach
378f5f5da8SGreg Roach	/** @var string The name of the document */
388f5f5da8SGreg Roach	private $descriptive_title = '';
398f5f5da8SGreg Roach
408f5f5da8SGreg Roach	/** @var Media $media The media object to which this file belongs */
418f5f5da8SGreg Roach	private $media;
428f5f5da8SGreg Roach
4364b90bf1SGreg Roach	/** @var string */
4464b90bf1SGreg Roach	private $fact_id;
4564b90bf1SGreg Roach
468f5f5da8SGreg Roach	/**
478f5f5da8SGreg Roach	 * Create a MediaFile from raw GEDCOM data.
488f5f5da8SGreg Roach	 *
498f5f5da8SGreg Roach	 * @param string $gedcom
508f5f5da8SGreg Roach	 * @param Media  $media
518f5f5da8SGreg Roach	 */
528f5f5da8SGreg Roach	public function __construct($gedcom, Media $media) {
538f5f5da8SGreg Roach		$this->media   = $media;
5464b90bf1SGreg Roach		$this->fact_id = md5($gedcom);
558f5f5da8SGreg Roach
568f5f5da8SGreg Roach		if (preg_match('/^\d FILE (.+)/m', $gedcom, $match)) {
578f5f5da8SGreg Roach			$this->multimedia_file_refn = $match[1];
588f5f5da8SGreg Roach		}
598f5f5da8SGreg Roach
608f5f5da8SGreg Roach		if (preg_match('/^\d FORM (.+)/m', $gedcom, $match)) {
618f5f5da8SGreg Roach			$this->multimedia_format = $match[1];
628f5f5da8SGreg Roach		}
638f5f5da8SGreg Roach
648f5f5da8SGreg Roach		if (preg_match('/^\d TYPE (.+)/m', $gedcom, $match)) {
658f5f5da8SGreg Roach			$this->source_media_type = $match[1];
668f5f5da8SGreg Roach		}
678f5f5da8SGreg Roach
688f5f5da8SGreg Roach		if (preg_match('/^\d TITL (.+)/m', $gedcom, $match)) {
698f5f5da8SGreg Roach			$this->descriptive_title = $match[1];
708f5f5da8SGreg Roach		}
718f5f5da8SGreg Roach	}
728f5f5da8SGreg Roach
738f5f5da8SGreg Roach	/**
7464b90bf1SGreg Roach	 * Get the filename.
758f5f5da8SGreg Roach	 *
768f5f5da8SGreg Roach	 * @return string
778f5f5da8SGreg Roach	 */
786e6a9947SGreg Roach	public function filename(): string {
796e6a9947SGreg Roach		return $this->multimedia_file_refn;
808f5f5da8SGreg Roach	}
818f5f5da8SGreg Roach
828f5f5da8SGreg Roach	/**
83d6641c58SGreg Roach	 * Get the base part of the filename.
84d6641c58SGreg Roach	 *
85d6641c58SGreg Roach	 * @return string
86d6641c58SGreg Roach	 */
87d6641c58SGreg Roach	public function basename(): string {
88d6641c58SGreg Roach		return basename($this->multimedia_file_refn);
89d6641c58SGreg Roach	}
90d6641c58SGreg Roach
91d6641c58SGreg Roach	/**
92d6641c58SGreg Roach	 * Get the folder part of the filename.
93d6641c58SGreg Roach	 *
94d6641c58SGreg Roach	 * @return string
95d6641c58SGreg Roach	 */
96d6641c58SGreg Roach	public function dirname(): string {
97d6641c58SGreg Roach		$dirname = dirname($this->multimedia_file_refn);
98d6641c58SGreg Roach
99d6641c58SGreg Roach		if ($dirname === '.') {
100d6641c58SGreg Roach			return '';
101d6641c58SGreg Roach		} else {
102d6641c58SGreg Roach			return $dirname;
103d6641c58SGreg Roach		}
104d6641c58SGreg Roach	}
105d6641c58SGreg Roach
106d6641c58SGreg Roach	/**
10764b90bf1SGreg Roach	 * Get the format.
1088f5f5da8SGreg Roach	 *
1098f5f5da8SGreg Roach	 * @return string
1108f5f5da8SGreg Roach	 */
1116e6a9947SGreg Roach	public function format(): string {
1128f5f5da8SGreg Roach		return $this->multimedia_format;
1138f5f5da8SGreg Roach	}
1148f5f5da8SGreg Roach
1158f5f5da8SGreg Roach	/**
11664b90bf1SGreg Roach	 * Get the type.
1178f5f5da8SGreg Roach	 *
1188f5f5da8SGreg Roach	 * @return string
1198f5f5da8SGreg Roach	 */
1206e6a9947SGreg Roach	public function type(): string {
1218f5f5da8SGreg Roach		return $this->source_media_type;
1228f5f5da8SGreg Roach	}
1238f5f5da8SGreg Roach
1248f5f5da8SGreg Roach	/**
12564b90bf1SGreg Roach	 * Get the title.
1268f5f5da8SGreg Roach	 *
1278f5f5da8SGreg Roach	 * @return string
1288f5f5da8SGreg Roach	 */
1296e6a9947SGreg Roach	public function title(): string {
1308f5f5da8SGreg Roach		return $this->descriptive_title;
1318f5f5da8SGreg Roach	}
1328f5f5da8SGreg Roach
1338f5f5da8SGreg Roach	/**
13464b90bf1SGreg Roach	 * Get the fact ID.
13564b90bf1SGreg Roach	 *
13664b90bf1SGreg Roach	 * @return string
13764b90bf1SGreg Roach	 */
13864b90bf1SGreg Roach	public function factId(): string {
13964b90bf1SGreg Roach		return $this->fact_id;
14064b90bf1SGreg Roach	}
14164b90bf1SGreg Roach
14264b90bf1SGreg Roach	/**
143d6641c58SGreg Roach	 * @return bool
144d6641c58SGreg Roach	 */
145d6641c58SGreg Roach	public function isPendingAddtion() {
146d6641c58SGreg Roach		foreach ($this->media->getFacts() as $fact) {
147d6641c58SGreg Roach			if ($fact->getFactId() === $this->fact_id) {
148d6641c58SGreg Roach				return $fact->isPendingAddition();
149d6641c58SGreg Roach			}
150d6641c58SGreg Roach		}
151d6641c58SGreg Roach
152d6641c58SGreg Roach		return false;
153d6641c58SGreg Roach	}
154d6641c58SGreg Roach
155d6641c58SGreg Roach	/**
156d6641c58SGreg Roach	 * @return bool
157d6641c58SGreg Roach	 */
158d6641c58SGreg Roach	public function isPendingDeletion() {
159d6641c58SGreg Roach		foreach ($this->media->getFacts() as $fact) {
160d6641c58SGreg Roach			if ($fact->getFactId() === $this->fact_id) {
161d6641c58SGreg Roach				return $fact->isPendingDeletion();
162d6641c58SGreg Roach			}
163d6641c58SGreg Roach		}
164d6641c58SGreg Roach
165d6641c58SGreg Roach		return false;
166d6641c58SGreg Roach	}
167d6641c58SGreg Roach
168d6641c58SGreg Roach	/**
16964b90bf1SGreg Roach	 * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox.
17064b90bf1SGreg Roach	 *
17164b90bf1SGreg Roach	 * @param int      $width      Pixels
17264b90bf1SGreg Roach	 * @param int      $height     Pixels
17364b90bf1SGreg Roach	 * @param string   $fit        "crop" or "contain"
17464b90bf1SGreg Roach	 * @param string[] $attributes Additional HTML attributes
17564b90bf1SGreg Roach	 *
17664b90bf1SGreg Roach	 * @return string
17764b90bf1SGreg Roach	 */
17864b90bf1SGreg Roach	public function displayImage($width, $height, $fit, $attributes = []) {
17964b90bf1SGreg Roach		if ($this->isExternal()) {
18064b90bf1SGreg Roach			$src    = $this->multimedia_file_refn;
18164b90bf1SGreg Roach			$srcset = [];
18264b90bf1SGreg Roach		} else {
18364b90bf1SGreg Roach			// Generate multiple images for displays with higher pixel densities.
18464b90bf1SGreg Roach			$src    = $this->imageUrl($width, $height, $fit);
18564b90bf1SGreg Roach			$srcset = [];
18664b90bf1SGreg Roach			foreach ([2, 3, 4] as $x) {
18764b90bf1SGreg Roach				$srcset[] = $this->imageUrl($width * $x, $height * $x, $fit) . ' ' . $x . 'x';
18864b90bf1SGreg Roach			}
18964b90bf1SGreg Roach		}
19064b90bf1SGreg Roach
19164b90bf1SGreg Roach		$image = '<img ' . Html::attributes($attributes + [
19264b90bf1SGreg Roach					'dir'    => 'auto',
19364b90bf1SGreg Roach					'src'    => $src,
19464b90bf1SGreg Roach					'srcset' => implode(',', $srcset),
19564b90bf1SGreg Roach					'alt'    => strip_tags($this->media->getFullName()),
19664b90bf1SGreg Roach				]) . '>';
19764b90bf1SGreg Roach
19864b90bf1SGreg Roach		$attributes = Html::attributes([
19964b90bf1SGreg Roach			'class'      => 'gallery',
20064b90bf1SGreg Roach			'type'       => $this->mimeType(),
201*60e3c46aSGreg Roach			'href'       => $this->imageUrl(0, 0, 'contain'),
202*60e3c46aSGreg Roach			'data-title' => strip_tags($this->media->getFullName()),
20364b90bf1SGreg Roach		]);
20464b90bf1SGreg Roach
20564b90bf1SGreg Roach		return '<a ' . $attributes . '>' . $image . '</a>';
20664b90bf1SGreg Roach	}
20764b90bf1SGreg Roach
2084a9f750fSGreg Roach	/**
2094a9f750fSGreg Roach	 * A list of image attributes
2104a9f750fSGreg Roach	 *
2114a9f750fSGreg Roach	 * @return string[]
2124a9f750fSGreg Roach	 */
2134a9f750fSGreg Roach	public function attributes(): array {
2144a9f750fSGreg Roach		$attributes = [];
21564b90bf1SGreg Roach
2164a9f750fSGreg Roach		if (!$this->isExternal() || $this->fileExists()) {
2174a9f750fSGreg Roach			$file = $this->folder() . $this->multimedia_file_refn;
2184a9f750fSGreg Roach
2194a9f750fSGreg Roach			$attributes['__FILE_SIZE__'] = $this->fileSizeKB();
2204a9f750fSGreg Roach
2214a9f750fSGreg Roach			$imgsize = getimagesize($file);
2224a9f750fSGreg Roach			if (is_array($imgsize) && !empty($imgsize['0'])) {
2234a9f750fSGreg Roach				$attributes['__IMAGE_SIZE__'] = I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1']));
2244a9f750fSGreg Roach			}
2254a9f750fSGreg Roach		}
2264a9f750fSGreg Roach
2274a9f750fSGreg Roach		return $attributes;
2284a9f750fSGreg Roach	}
2294a9f750fSGreg Roach
2304a9f750fSGreg Roach	/**
2314a9f750fSGreg Roach	 * check if the file exists on this server
2324a9f750fSGreg Roach	 *
2334a9f750fSGreg Roach	 * @return bool
2344a9f750fSGreg Roach	 */
2354a9f750fSGreg Roach	public function fileExists() {
236d6641c58SGreg Roach		return !$this->isExternal() && file_exists($this->folder() . $this->multimedia_file_refn);
2374a9f750fSGreg Roach	}
2384a9f750fSGreg Roach
2394a9f750fSGreg Roach	/**
2404a9f750fSGreg Roach	 * Is the media file actually a URL?
2414a9f750fSGreg Roach	 */
2424a9f750fSGreg Roach	public function isExternal(): bool {
2434a9f750fSGreg Roach		return strpos($this->multimedia_file_refn, '://') !== false;
2444a9f750fSGreg Roach	}
2454a9f750fSGreg Roach
2464a9f750fSGreg Roach	/**
2474a9f750fSGreg Roach	 * Is the media file an image?
2484a9f750fSGreg Roach	 */
2494a9f750fSGreg Roach	public function isImage(): bool {
2504a9f750fSGreg Roach		return in_array($this->extension(), ['jpeg', 'jpg', 'gif', 'png']);
2514a9f750fSGreg Roach	}
2524a9f750fSGreg Roach
2534a9f750fSGreg Roach	/**
25444379edcSGreg Roach	 * Where is the file stored on disk?
25544379edcSGreg Roach	 */
25644379edcSGreg Roach	public function folder(): string {
25744379edcSGreg Roach		return WT_DATA_DIR . $this->media->getTree()->getPreference('MEDIA_DIRECTORY');
25844379edcSGreg Roach	}
25944379edcSGreg Roach
26044379edcSGreg Roach	/**
2614a9f750fSGreg Roach	 * A user-friendly view of the file size
2624a9f750fSGreg Roach	 *
2634a9f750fSGreg Roach	 * @return int
2644a9f750fSGreg Roach	 */
2654a9f750fSGreg Roach	private function fileSizeBytes(): int {
2664a9f750fSGreg Roach		try {
2674a9f750fSGreg Roach			return filesize($this->folder() . $this->multimedia_file_refn);
2684a9f750fSGreg Roach		} catch (ErrorException $ex) {
2694a9f750fSGreg Roach			DebugBar::addThrowable($ex);
2704a9f750fSGreg Roach
2714a9f750fSGreg Roach			return 0;
2724a9f750fSGreg Roach		}
2734a9f750fSGreg Roach	}
2744a9f750fSGreg Roach
2754a9f750fSGreg Roach	/**
2764a9f750fSGreg Roach	 * get the media file size in KB
2774a9f750fSGreg Roach	 *
2784a9f750fSGreg Roach	 * @return string
2794a9f750fSGreg Roach	 */
2804a9f750fSGreg Roach	public function fileSizeKB() {
2814a9f750fSGreg Roach		$size = $this->filesizeBytes();
2824a9f750fSGreg Roach		$size = (int) (($size + 1023) / 1024);
2834a9f750fSGreg Roach
2844a9f750fSGreg Roach		return /* I18N: size of file in KB */ I18N::translate('%s KB', I18N::number($size));
2854a9f750fSGreg Roach	}
28664b90bf1SGreg Roach
28764b90bf1SGreg Roach	///////////////////////////////////////////////////////////////////////////
28864b90bf1SGreg Roach
28964b90bf1SGreg Roach	/**
2908f5f5da8SGreg Roach	 * Get the filename on the server - for those (very few!) functions which actually
2918f5f5da8SGreg Roach	 * need the filename, such as mediafirewall.php and the PDF reports.
2928f5f5da8SGreg Roach	 *
2938f5f5da8SGreg Roach	 * @return string
2948f5f5da8SGreg Roach	 */
2958f5f5da8SGreg Roach	public function getServerFilename() {
2968f5f5da8SGreg Roach		$MEDIA_DIRECTORY = $this->media->getTree()->getPreference('MEDIA_DIRECTORY');
2978f5f5da8SGreg Roach
29864b90bf1SGreg Roach		if ($this->isExternal() || !$this->multimedia_file_refn) {
2998f5f5da8SGreg Roach			// External image, or (in the case of corrupt GEDCOM data) no image at all
30064b90bf1SGreg Roach			return $this->multimedia_file_refn;
3018f5f5da8SGreg Roach		} else {
3028f5f5da8SGreg Roach			// Main image
30364b90bf1SGreg Roach			return WT_DATA_DIR . $MEDIA_DIRECTORY . $this->multimedia_file_refn;
3048f5f5da8SGreg Roach		}
3058f5f5da8SGreg Roach	}
3068f5f5da8SGreg Roach
3078f5f5da8SGreg Roach	/**
3088f5f5da8SGreg Roach	 * get image properties
3098f5f5da8SGreg Roach	 *
3108f5f5da8SGreg Roach	 * @return array
3118f5f5da8SGreg Roach	 */
3128f5f5da8SGreg Roach	public function getImageAttributes() {
3138f5f5da8SGreg Roach		$imgsize = [];
3148f5f5da8SGreg Roach		if ($this->fileExists()) {
3158f5f5da8SGreg Roach			try {
3168f5f5da8SGreg Roach				$imgsize = getimagesize($this->getServerFilename());
3178f5f5da8SGreg Roach				if (is_array($imgsize) && !empty($imgsize['0'])) {
3188f5f5da8SGreg Roach					// this is an image
3198f5f5da8SGreg Roach					$imageTypes     = ['', 'GIF', 'JPG', 'PNG', 'SWF', 'PSD', 'BMP', 'TIFF', 'TIFF', 'JPC', 'JP2', 'JPX', 'JB2', 'SWC', 'IFF', 'WBMP', 'XBM'];
3208f5f5da8SGreg Roach					$imgsize['ext'] = $imageTypes[0 + $imgsize[2]];
3218f5f5da8SGreg Roach					// this is for display purposes, always show non-adjusted info
3228f5f5da8SGreg Roach					$imgsize['WxH'] = /* I18N: image dimensions, width × height */
3238f5f5da8SGreg Roach						I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1']));
3248f5f5da8SGreg Roach				}
3254a9f750fSGreg Roach			} catch (ErrorException $ex) {
3268f5f5da8SGreg Roach				DebugBar::addThrowable($ex);
3278f5f5da8SGreg Roach
3288f5f5da8SGreg Roach				// Not an image, or not a valid image?
3298f5f5da8SGreg Roach				$imgsize = false;
3308f5f5da8SGreg Roach			}
3318f5f5da8SGreg Roach		}
3328f5f5da8SGreg Roach
3338f5f5da8SGreg Roach		if (!is_array($imgsize) || empty($imgsize['0'])) {
3348f5f5da8SGreg Roach			// this is not an image, OR the file doesn’t exist OR it is a url
3358f5f5da8SGreg Roach			$imgsize[0]      = 0;
3368f5f5da8SGreg Roach			$imgsize[1]      = 0;
3378f5f5da8SGreg Roach			$imgsize['ext']  = '';
3388f5f5da8SGreg Roach			$imgsize['mime'] = '';
3398f5f5da8SGreg Roach			$imgsize['WxH']  = '';
3408f5f5da8SGreg Roach		}
3418f5f5da8SGreg Roach
3428f5f5da8SGreg Roach		if (empty($imgsize['mime'])) {
3438f5f5da8SGreg Roach			// this is not an image, OR the file doesn’t exist OR it is a url
3448f5f5da8SGreg Roach			// set file type equal to the file extension - can’t use parse_url because this may not be a full url
3454a9f750fSGreg Roach			$exp            = explode('?', $this->multimedia_file_refn);
3468f5f5da8SGreg Roach			$imgsize['ext'] = strtoupper(pathinfo($exp[0], PATHINFO_EXTENSION));
3478f5f5da8SGreg Roach			// all mimetypes we wish to serve with the media firewall must be added to this array.
3488f5f5da8SGreg Roach			$mime = [
3498f5f5da8SGreg Roach				'DOC' => 'application/msword',
3508f5f5da8SGreg Roach				'MOV' => 'video/quicktime',
3518f5f5da8SGreg Roach				'MP3' => 'audio/mpeg',
3528f5f5da8SGreg Roach				'PDF' => 'application/pdf',
3538f5f5da8SGreg Roach				'PPT' => 'application/vnd.ms-powerpoint',
3548f5f5da8SGreg Roach				'RTF' => 'text/rtf',
3558f5f5da8SGreg Roach				'SID' => 'image/x-mrsid',
3568f5f5da8SGreg Roach				'TXT' => 'text/plain',
3578f5f5da8SGreg Roach				'XLS' => 'application/vnd.ms-excel',
3588f5f5da8SGreg Roach				'WMV' => 'video/x-ms-wmv',
3598f5f5da8SGreg Roach			];
3608f5f5da8SGreg Roach			if (empty($mime[$imgsize['ext']])) {
3618f5f5da8SGreg Roach				// if we don’t know what the mimetype is, use something ambiguous
3628f5f5da8SGreg Roach				$imgsize['mime'] = 'application/octet-stream';
3638f5f5da8SGreg Roach				if ($this->fileExists()) {
3648f5f5da8SGreg Roach					// alert the admin if we cannot determine the mime type of an existing file
3658f5f5da8SGreg Roach					// as the media firewall will be unable to serve this file properly
3664a9f750fSGreg Roach					Log::addMediaLog('Media Firewall error: >Unknown Mimetype< for file >' . $this->multimedia_file_refn . '<');
3678f5f5da8SGreg Roach				}
3688f5f5da8SGreg Roach			} else {
3698f5f5da8SGreg Roach				$imgsize['mime'] = $mime[$imgsize['ext']];
3708f5f5da8SGreg Roach			}
3718f5f5da8SGreg Roach		}
3728f5f5da8SGreg Roach
3738f5f5da8SGreg Roach		return $imgsize;
3748f5f5da8SGreg Roach	}
3758f5f5da8SGreg Roach
3768f5f5da8SGreg Roach	/**
3778f5f5da8SGreg Roach	 * Generate a URL for an image.
3788f5f5da8SGreg Roach	 *
3798f5f5da8SGreg Roach	 * @param int    $width  Maximum width in pixels
3808f5f5da8SGreg Roach	 * @param int    $height Maximum height in pixels
3818f5f5da8SGreg Roach	 * @param string $fit    "crop" or "contain"
3828f5f5da8SGreg Roach	 *
3838f5f5da8SGreg Roach	 * @return string
3848f5f5da8SGreg Roach	 */
3858f5f5da8SGreg Roach	public function imageUrl($width, $height, $fit) {
3868f5f5da8SGreg Roach		// Sign the URL, to protect against mass-resize attacks.
3878f5f5da8SGreg Roach		$glide_key = Site::getPreference('glide-key');
3888f5f5da8SGreg Roach		if (empty($glide_key)) {
3898f5f5da8SGreg Roach			$glide_key = bin2hex(random_bytes(128));
3908f5f5da8SGreg Roach			Site::setPreference('glide-key', $glide_key);
3918f5f5da8SGreg Roach		}
3928f5f5da8SGreg Roach
39364b90bf1SGreg Roach		if (Auth::accessLevel($this->media->getTree()) > $this->media->getTree()->getPreference('SHOW_NO_WATERMARK')) {
3948f5f5da8SGreg Roach			$mark = 'watermark.png';
3958f5f5da8SGreg Roach		} else {
3968f5f5da8SGreg Roach			$mark = '';
3978f5f5da8SGreg Roach		}
3988f5f5da8SGreg Roach
3994a9f750fSGreg Roach		$url_builder = UrlBuilderFactory::create(WT_BASE_URL, $glide_key);
4004a9f750fSGreg Roach
4014a9f750fSGreg Roach		$url = $url_builder->getUrl('index.php', [
4024a9f750fSGreg Roach			'route'     => 'media-thumbnail',
4034a9f750fSGreg Roach			'xref'      => $this->media->getXref(),
40464b90bf1SGreg Roach			'ged'       => $this->media->getTree()->getName(),
4054a9f750fSGreg Roach			'fact_id'   => $this->fact_id,
4068f5f5da8SGreg Roach			'w'         => $width,
4078f5f5da8SGreg Roach			'h'         => $height,
4088f5f5da8SGreg Roach			'fit'       => $fit,
4098f5f5da8SGreg Roach			'mark'      => $mark,
4108f5f5da8SGreg Roach			'markh'     => '100h',
4118f5f5da8SGreg Roach			'markw'     => '100w',
4128f5f5da8SGreg Roach			'markalpha' => 25,
4138f5f5da8SGreg Roach			'or'        => 0, // Intervention uses exif_read_data() which is very buggy.
4148f5f5da8SGreg Roach		]);
4158f5f5da8SGreg Roach
4168f5f5da8SGreg Roach		return $url;
4178f5f5da8SGreg Roach	}
4188f5f5da8SGreg Roach
4198f5f5da8SGreg Roach	/**
4208f5f5da8SGreg Roach	 * What file extension is used by this file?
4218f5f5da8SGreg Roach	 *
4228f5f5da8SGreg Roach	 * @return string
4238f5f5da8SGreg Roach	 */
4248f5f5da8SGreg Roach	public function extension() {
425a99c6938SGreg Roach		if (preg_match('/\.([a-zA-Z0-9]+)$/', $this->multimedia_file_refn, $match)) {
4268f5f5da8SGreg Roach			return strtolower($match[1]);
4278f5f5da8SGreg Roach		} else {
4288f5f5da8SGreg Roach			return '';
4298f5f5da8SGreg Roach		}
4308f5f5da8SGreg Roach	}
4318f5f5da8SGreg Roach
4328f5f5da8SGreg Roach	/**
4338f5f5da8SGreg Roach	 * What is the mime-type of this object?
4348f5f5da8SGreg Roach	 * For simplicity and efficiency, use the extension, rather than the contents.
4358f5f5da8SGreg Roach	 *
4368f5f5da8SGreg Roach	 * @return string
4378f5f5da8SGreg Roach	 */
4388f5f5da8SGreg Roach	public function mimeType() {
4398f5f5da8SGreg Roach		// Themes contain icon definitions for some/all of these mime-types
4408f5f5da8SGreg Roach		switch ($this->extension()) {
4418f5f5da8SGreg Roach			case 'bmp':
4428f5f5da8SGreg Roach				return 'image/bmp';
4438f5f5da8SGreg Roach			case 'doc':
4448f5f5da8SGreg Roach				return 'application/msword';
4458f5f5da8SGreg Roach			case 'docx':
4468f5f5da8SGreg Roach				return 'application/msword';
4478f5f5da8SGreg Roach			case 'ged':
4488f5f5da8SGreg Roach				return 'text/x-gedcom';
4498f5f5da8SGreg Roach			case 'gif':
4508f5f5da8SGreg Roach				return 'image/gif';
4518f5f5da8SGreg Roach			case 'htm':
4528f5f5da8SGreg Roach				return 'text/html';
4538f5f5da8SGreg Roach			case 'html':
4548f5f5da8SGreg Roach				return 'text/html';
4558f5f5da8SGreg Roach			case 'jpeg':
4568f5f5da8SGreg Roach				return 'image/jpeg';
4578f5f5da8SGreg Roach			case 'jpg':
4588f5f5da8SGreg Roach				return 'image/jpeg';
4598f5f5da8SGreg Roach			case 'mov':
4608f5f5da8SGreg Roach				return 'video/quicktime';
4618f5f5da8SGreg Roach			case 'mp3':
4628f5f5da8SGreg Roach				return 'audio/mpeg';
4638f5f5da8SGreg Roach			case 'mp4':
4648f5f5da8SGreg Roach				return 'video/mp4';
4658f5f5da8SGreg Roach			case 'ogv':
4668f5f5da8SGreg Roach				return 'video/ogg';
4678f5f5da8SGreg Roach			case 'pdf':
4688f5f5da8SGreg Roach				return 'application/pdf';
4698f5f5da8SGreg Roach			case 'png':
4708f5f5da8SGreg Roach				return 'image/png';
4718f5f5da8SGreg Roach			case 'rar':
4728f5f5da8SGreg Roach				return 'application/x-rar-compressed';
4738f5f5da8SGreg Roach			case 'swf':
4748f5f5da8SGreg Roach				return 'application/x-shockwave-flash';
4758f5f5da8SGreg Roach			case 'svg':
4768f5f5da8SGreg Roach				return 'image/svg';
4778f5f5da8SGreg Roach			case 'tif':
4788f5f5da8SGreg Roach				return 'image/tiff';
4798f5f5da8SGreg Roach			case 'tiff':
4808f5f5da8SGreg Roach				return 'image/tiff';
4818f5f5da8SGreg Roach			case 'xls':
4828f5f5da8SGreg Roach				return 'application/vnd-ms-excel';
4838f5f5da8SGreg Roach			case 'xlsx':
4848f5f5da8SGreg Roach				return 'application/vnd-ms-excel';
4858f5f5da8SGreg Roach			case 'wmv':
4868f5f5da8SGreg Roach				return 'video/x-ms-wmv';
4878f5f5da8SGreg Roach			case 'zip':
4888f5f5da8SGreg Roach				return 'application/zip';
4898f5f5da8SGreg Roach			default:
4908f5f5da8SGreg Roach				return 'application/octet-stream';
4918f5f5da8SGreg Roach		}
4928f5f5da8SGreg Roach	}
4938f5f5da8SGreg Roach}
494