qbus-agentur /
qbtools
| 1 | <?php |
||
| 2 | namespace Qbus\Qbtools\ViewHelpers; |
||
| 3 | |||
| 4 | use TYPO3\CMS\Core\Resource\FileInterface; |
||
| 5 | use TYPO3\CMS\Extbase\Domain\Model\FileReference; |
||
| 6 | use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
||
| 7 | use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
||
| 8 | use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; |
||
| 9 | |||
| 10 | /* ************************************************************** |
||
| 11 | * Copyright notice |
||
| 12 | * |
||
| 13 | * (c) 2014 Benjamin Franzke <[email protected]>, Qbus Internetagentur GmbH |
||
| 14 | * |
||
| 15 | * All rights reserved |
||
| 16 | * |
||
| 17 | * This script is part of the TYPO3 project. The TYPO3 project is |
||
| 18 | * free software; you can redistribute it and/or modify |
||
| 19 | * it under the terms of the GNU General Public License as published by |
||
| 20 | * the Free Software Foundation; either version 3 of the License, or |
||
| 21 | * (at your option) any later version. |
||
| 22 | * |
||
| 23 | * The GNU General Public License can be found at |
||
| 24 | * http://www.gnu.org/copyleft/gpl.html. |
||
| 25 | * |
||
| 26 | * This script is distributed in the hope that it will be useful, |
||
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 29 | * GNU General Public License for more details. |
||
| 30 | * |
||
| 31 | * This copyright notice MUST APPEAR in all copies of the script! |
||
| 32 | * ************************************************************* */ |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later |
||
| 36 | */ |
||
| 37 | class CalculateBoundsViewHelper extends AbstractViewHelper |
||
| 38 | { |
||
| 39 | use CompileWithRenderStatic; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Initialize arguments |
||
| 43 | */ |
||
| 44 | public function initializeArguments() |
||
| 45 | { |
||
| 46 | $this->registerArgument('images', 'array', '', true); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param array $arguments |
||
| 51 | * @param \Closure $renderChildrenClosure |
||
| 52 | * @param RenderingContextInterface $renderingContext |
||
| 53 | * @return array<string,int> |
||
| 54 | */ |
||
| 55 | public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): array |
||
| 56 | { |
||
| 57 | $images = $arguments['images']; |
||
| 58 | |||
| 59 | $result = array('minWidth' => 0, 'minHeight' => 0, |
||
| 60 | 'maxWidth' => 0, 'maxHeight' => 0, |
||
| 61 | 'minRatio' => 0, 'maxRatio' => 0, |
||
| 62 | 'minRatioWidth' => 0, 'minRatioHeight' => 0, |
||
| 63 | 'maxRatioWidth' => 0, 'maxRatioHeight' => 0); |
||
| 64 | |||
| 65 | foreach ($images as $image) { |
||
| 66 | foreach (array('height', 'width') as $type) { |
||
| 67 | $val = self::getCroppedProperty($image, $type); |
||
| 68 | if ($val === null) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | $min = 'min' . ucfirst($type); |
||
| 73 | $max = 'max' . ucfirst($type); |
||
| 74 | |||
| 75 | if ($result[$min] == 0 || $result[$min] > $val) { |
||
| 76 | $result[$min] = $val; |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($result[$max] < $val) { |
||
| 80 | $result[$max] = $val; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | $width = self::getCroppedProperty($image, 'width'); |
||
| 84 | $height = self::getCroppedProperty($image, 'height'); |
||
| 85 | if ($width === null || $height === null || $width == 0) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | $ratio = $height / $width * 100; |
||
| 90 | if ($result['minRatio'] == 0 || $ratio < $result['minRatio']) { |
||
| 91 | $result['minRatio'] = $ratio; |
||
| 92 | $result['minRatioWidth'] = $width; |
||
| 93 | $result['minRatioHeight'] = $height; |
||
| 94 | } |
||
| 95 | if ($ratio > $result['maxRatio']) { |
||
| 96 | $result['maxRatio'] = $ratio; |
||
| 97 | $result['maxRatioWidth'] = $width; |
||
| 98 | $result['maxRatioHeight'] = $height; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return $result; |
||
|
0 ignored issues
–
show
|
|||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * When retrieving the height or width for a media file |
||
| 107 | * a possible cropping needs to be taken into account. |
||
| 108 | * |
||
| 109 | * @param FileInterface|FileReference $fileObject |
||
| 110 | * @param string $dimensionalProperty 'width' or 'height' |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | protected static function getCroppedProperty($fileObject, $dimensionalProperty): int |
||
| 114 | { |
||
| 115 | if ($fileObject instanceof FileReference) { |
||
| 116 | $fileObject = $fileObject->getOriginalResource(); |
||
| 117 | } |
||
| 118 | if (!$fileObject->hasProperty('crop') || empty($fileObject->getProperty('crop'))) { |
||
| 119 | return (int) $fileObject->getProperty($dimensionalProperty); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (version_compare(TYPO3_branch, '8', '>=')) { |
||
| 123 | $croppingConfiguration = $fileObject->getProperty('crop'); |
||
| 124 | $cropVariantCollection = \TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection::create((string)$croppingConfiguration); |
||
| 125 | |||
| 126 | return (int) $cropVariantCollection->getCropArea('default')->makeAbsoluteBasedOnFile($fileObject)->asArray()[$dimensionalProperty]; |
||
| 127 | } else { |
||
| 128 | $croppingConfiguration = json_decode($fileObject->getProperty('crop'), true); |
||
| 129 | |||
| 130 | return (int)$croppingConfiguration[$dimensionalProperty]; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: