1 | <?php |
||
23 | class FocusCropService extends AbstractService |
||
24 | { |
||
25 | const SIGNAL_tempImageCropped = 'tempImageCropped'; |
||
26 | |||
27 | /** |
||
28 | * @var Dispatcher |
||
29 | */ |
||
30 | protected $signalSlotDispatcher; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $tempImageFolder; |
||
36 | |||
37 | /** |
||
38 | * get the image. |
||
39 | * |
||
40 | * @param $src |
||
41 | * @param $image |
||
42 | * @param $treatIdAsReference |
||
43 | * |
||
44 | * @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException |
||
45 | * @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException |
||
46 | * |
||
47 | * @return \TYPO3\CMS\Core\Resource\File|FileInterface|CoreFileReference|\TYPO3\CMS\Core\Resource\Folder |
||
48 | */ |
||
49 | public function getViewHelperImage($src, $image, $treatIdAsReference) |
||
68 | |||
69 | /** |
||
70 | * Helper function for view helpers. |
||
71 | * |
||
72 | * @param $src |
||
73 | * @param $image |
||
74 | * @param $treatIdAsReference |
||
75 | * @param $ratio |
||
76 | * |
||
77 | * @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException |
||
78 | * @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getCroppedImageSrcForViewHelper($src, $image, $treatIdAsReference, string $ratio) |
||
88 | |||
89 | /** |
||
90 | * Get the cropped image. |
||
91 | * |
||
92 | * @param string $fileReference |
||
93 | * @param string $ratio |
||
94 | * |
||
95 | * @return string The new filename |
||
96 | */ |
||
97 | public function getCroppedImageSrcByFileReference($fileReference, string $ratio) |
||
107 | |||
108 | /** |
||
109 | * Get the cropped image by File Object. |
||
110 | * |
||
111 | * @param FileInterface $file |
||
112 | * @param string $ratio |
||
113 | * |
||
114 | * @return string The new filename |
||
115 | */ |
||
116 | public function getCroppedImageSrcByFile(FileInterface $file, string $ratio) |
||
130 | |||
131 | /** |
||
132 | * Get the cropped image by src. |
||
133 | * |
||
134 | * @param string $src Relative file name |
||
135 | * @param string $ratio |
||
136 | * @param int $x |
||
137 | * @param int $y |
||
138 | * |
||
139 | * @return string The new filename |
||
140 | */ |
||
141 | public function getCroppedImageSrcBySrc(string $src, string $ratio, int $x, int $y): string |
||
142 | { |
||
143 | $absoluteImageName = GeneralUtility::getFileAbsFileName($src); |
||
144 | if (!\is_file($absoluteImageName)) { |
||
145 | return ''; |
||
146 | } |
||
147 | |||
148 | /** @var \TYPO3\CMS\Core\Http\NormalizedParams $params */ |
||
149 | $params = $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams'); |
||
150 | $docRoot = \rtrim($params->getDocumentRoot(), '/') . '/'; |
||
151 | $relativeSrc = \str_replace($docRoot, '', $absoluteImageName); |
||
152 | $focusPointX = MathUtility::forceIntegerInRange((int) $x, -100, 100, 0); |
||
153 | $focusPointY = MathUtility::forceIntegerInRange((int) $y, -100, 100, 0); |
||
154 | |||
155 | if (0 === $focusPointX && 0 === $focusPointY) { |
||
156 | $row = GeneralUtility::makeInstance(FileStandaloneRepository::class)->findOneByRelativeFilePath($relativeSrc); |
||
157 | if (isset($row['focus_point_x'])) { |
||
158 | $focusPointX = MathUtility::forceIntegerInRange((int) $row['focus_point_x'], -100, 100, 0); |
||
159 | $focusPointY = MathUtility::forceIntegerInRange((int) $row['focus_point_y'], -100, 100, 0); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | $tempImageFolder = $this->getTempImageFolder(); |
||
164 | $tempImageName = $this->generateTempImageName($absoluteImageName, $ratio, $focusPointX, $focusPointY); |
||
165 | $tempImageName = $tempImageFolder . $tempImageName; |
||
166 | |||
167 | $absoluteTempImageName = GeneralUtility::getFileAbsFileName($tempImageName); |
||
168 | |||
169 | if (\is_file($absoluteTempImageName)) { |
||
170 | return $tempImageName; |
||
171 | } |
||
172 | |||
173 | $absoluteTempImageFolder = GeneralUtility::getFileAbsFileName($tempImageFolder); |
||
174 | if (!\is_dir($absoluteTempImageFolder)) { |
||
175 | GeneralUtility::mkdir_deep($absoluteTempImageFolder); |
||
176 | } |
||
177 | |||
178 | $imageSizeInformation = \getimagesize($absoluteImageName); |
||
179 | $width = $imageSizeInformation[0]; |
||
180 | $height = $imageSizeInformation[1]; |
||
181 | |||
182 | // dimensions |
||
183 | /** @var DimensionService $dimensionService */ |
||
184 | $dimensionService = GeneralUtility::makeInstance(DimensionService::class); |
||
185 | list($focusWidth, $focusHeight) = $dimensionService->getFocusWidthAndHeight($width, $height, $ratio); |
||
186 | |||
187 | $cropMode = $dimensionService->getCropMode($width, $height, $ratio); |
||
188 | list($sourceX, $sourceY) = $dimensionService->calculateSourcePosition( |
||
189 | $cropMode, |
||
190 | $width, |
||
191 | $height, |
||
192 | $focusWidth, |
||
193 | $focusHeight, |
||
194 | $focusPointX, |
||
195 | $focusPointY |
||
196 | ); |
||
197 | |||
198 | $cropService = GeneralUtility::makeInstance(CropService::class); |
||
199 | $cropService->createImage( |
||
200 | $absoluteImageName, |
||
201 | $focusWidth, |
||
202 | $focusHeight, |
||
203 | $sourceX, |
||
204 | $sourceY, |
||
205 | $absoluteTempImageName |
||
206 | ); |
||
207 | |||
208 | $this->emitTempImageCropped($tempImageName); |
||
209 | |||
210 | return $tempImageName; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Emit tempImageCropped signal. |
||
215 | * |
||
216 | * @param string $tempImageName |
||
217 | * |
||
218 | * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException |
||
219 | * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException |
||
220 | */ |
||
221 | protected function emitTempImageCropped(string $tempImageName) |
||
225 | |||
226 | /** |
||
227 | * Get the SignalSlot dispatcher. |
||
228 | * |
||
229 | * @return Dispatcher |
||
230 | */ |
||
231 | protected function getSignalSlotDispatcher() |
||
239 | |||
240 | /** |
||
241 | * Gets the ObjectManager. |
||
242 | * |
||
243 | * @return ObjectManager |
||
244 | */ |
||
245 | protected function getObjectManager() |
||
249 | |||
250 | /** |
||
251 | * @param string $absoluteImageName |
||
252 | * @param string $ratio |
||
253 | * @param int $focusPointX |
||
254 | * @param int $focusPointY |
||
255 | * |
||
256 | * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException |
||
257 | * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function generateTempImageName(string $absoluteImageName, string $ratio, int $focusPointX, int $focusPointY): string |
||
290 | |||
291 | /** |
||
292 | * Return the folder for generated images. |
||
293 | * |
||
294 | * @return string Path relative to PATH_site |
||
295 | */ |
||
296 | protected function getTempImageFolder(): string |
||
309 | } |
||
310 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.