[TASK] Use ImageInfo class instead of direct PHP GD call
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / FileContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Type\File\ImageInfo;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
19
20 /**
21 * Contains FILE class object.
22 */
23 class FileContentObject extends AbstractContentObject
24 {
25 /**
26 * Rendering the cObject, FILE
27 *
28 * @param array $conf Array of TypoScript properties
29 * @return string Output
30 */
31 public function render($conf = [])
32 {
33 $theValue = '';
34 $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
35 $file = $this->getTypoScriptFrontendController()->tmpl->getFileName($file);
36 if ($file !== null && file_exists($file)) {
37 $fileInfo = GeneralUtility::split_fileref($file);
38 $extension = $fileInfo['fileext'];
39 if ($extension === 'jpg' || $extension === 'jpeg' || $extension === 'gif' || $extension === 'png') {
40 $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $file);
41 $altParameters = trim($this->cObj->getAltParam($conf, false));
42 $theValue = '<img src="'
43 . htmlspecialchars($this->getTypoScriptFrontendController()->absRefPrefix . $file)
44 . '" width="' . (int)$imageInfo->getWidth() . '" height="' . (int)$imageInfo->getHeight()
45 . '"' . $this->cObj->getBorderAttr(' border="0"') . ' ' . $altParameters . ' />';
46 } elseif (filesize($file) < 1024 * 1024) {
47 $theValue = file_get_contents($file);
48 }
49 }
50
51 $linkWrap = isset($conf['linkWrap.']) ? $this->cObj->stdWrap($conf['linkWrap'], $conf['linkWrap.']) : $conf['linkWrap'];
52 if ($linkWrap) {
53 $theValue = $this->cObj->linkWrap($theValue, $linkWrap);
54 }
55 $wrap = isset($conf['wrap.']) ? $this->cObj->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
56 if ($wrap) {
57 $theValue = $this->cObj->wrap($theValue, $wrap);
58 }
59 if (isset($conf['stdWrap.'])) {
60 $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
61 }
62 return $theValue;
63 }
64
65 /**
66 * @return TypoScriptFrontendController
67 */
68 protected function getTypoScriptFrontendController()
69 {
70 return $GLOBALS['TSFE'];
71 }
72 }