[BUGFIX] Ensure variable imageSizes is an array before accessing its values
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Type / File / ImageInfo.php
1 <?php
2 namespace TYPO3\CMS\Core\Type\File;
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * A SPL FileInfo class providing information related to an image.
21 */
22 class ImageInfo extends FileInfo {
23
24 /**
25 * @var array
26 */
27 protected $imageSizes;
28
29 /**
30 * Returns the width of the Image.
31 *
32 * @return int
33 */
34 public function getWidth() {
35 $imageSizes = $this->getImageSizes();
36 return $imageSizes[0];
37 }
38
39 /**
40 * Returns the height of the Image.
41 *
42 * @return int
43 */
44 public function getHeight() {
45 $imageSizes = $this->getImageSizes();
46 return $imageSizes[1];
47 }
48
49 /**
50 * @return array
51 */
52 protected function getImageSizes() {
53 if (is_null($this->imageSizes)) {
54 $this->imageSizes = getimagesize($this->getPathname());
55
56 // In case the image size could not be retrieved, log the incident as a warning.
57 if ($this->imageSizes === FALSE) {
58 $this->getLogger()->warning('I could not retrieve the image size for file ' . $this->getPathname());
59 $this->imageSizes = array(0, 0);
60 }
61 }
62 return $this->imageSizes;
63 }
64
65 /**
66 * @return \TYPO3\CMS\Core\Log\Logger
67 */
68 protected function getLogger(){
69 /** @var $loggerManager \TYPO3\CMS\Core\Log\LogManager */
70 $loggerManager = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager');
71
72 return $loggerManager->getLogger(get_class($this));
73 }
74
75 }