Skip to content

Commit acd5704

Browse files
committed
[FEATURE] So far imageopt was forcing all images to be resized on fronted by default. Right now its configurable in Typoscript.
1 parent 0147d9c commit acd5704

File tree

6 files changed

+81
-85
lines changed

6 files changed

+81
-85
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/***************************************************************
4+
* Copyright notice
5+
*
6+
* All rights reserved
7+
*
8+
* This script is part of the TYPO3 project. The TYPO3 project is
9+
* free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The GNU General Public License can be found at
15+
* http://www.gnu.org/copyleft/gpl.html.
16+
*
17+
* This script is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* This copyright notice MUST APPEAR in all copies of the script!
23+
***************************************************************/
24+
25+
namespace SourceBroker\Imageopt\Utility;
26+
27+
/**
28+
* Manage temporary files
29+
*/
30+
class FrontendProcessingUtility
31+
{
32+
public static function isAllowedToForceFrontendImageProcessing($filePath)
33+
{
34+
return !empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_imageopt.']['imageProcessing.']['force'])
35+
&& (empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_imageopt.']['imageProcessing.']['exclusion.']['regexp'])
36+
|| (
37+
!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_imageopt.']['imageProcessing.']['exclusion.']['regexp'])
38+
&&
39+
!preg_match($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_imageopt.']['imageProcessing.']['exclusion.']['regexp'],
40+
$filePath)
41+
)
42+
);
43+
}
44+
}

Classes/Xclass/ContentObjectRenderer.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,34 @@
2424

2525
namespace SourceBroker\Imageopt\Xclass;
2626

27+
use SourceBroker\Imageopt\Utility\FrontendProcessingUtility;
28+
use TYPO3\CMS\Core\Resource\File;
29+
use TYPO3\CMS\Core\Resource\FileReference;
30+
2731
class ContentObjectRenderer extends \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
2832
{
29-
3033
/**
31-
* Xclassed because when the parameters ($fileArray['params.']) are changed the hash is changed then we force processed files
34+
* TypoScript processed images
3235
*
33-
* @param string|File|FileReference $file A "imgResource" TypoScript data type. Either a TypoScript file resource, a file or a file reference object or the string GIFBUILDER. See description above.
36+
* 5 = IMAGE
37+
* 5 {
38+
* file = fileadmin/img.jpg
39+
* file.width = 20
40+
* }
41+
*
42+
* @param string |File|FileReference $file A "imgResource" TypoScript data type. Either a TypoScript file resource, a file or a file reference object or the string GIFBUILDER. See description above.
3443
* @param array $fileArray TypoScript properties for the imgResource type
3544
* @return array|NULL Returns info-array
3645
*/
3746
public function getImgResource($file, $fileArray)
3847
{
39-
$paramsValue = isset($fileArray['params.']) ? $this->stdWrap($fileArray['params'],
40-
$fileArray['params.']) : $fileArray['params'];
41-
unset($fileArray['params.']);
42-
// Make $fileArray['params'] at least one space to count that towards hash in order to make TYPO3 to process image even if it was not processed without this.
43-
$fileArray['params'] = $paramsValue . ' ';
44-
48+
if (FrontendProcessingUtility::isAllowedToForceFrontendImageProcessing($file)) {
49+
$paramsValue = isset($fileArray['params.']) ? $this->stdWrap($fileArray['params'],
50+
$fileArray['params.']) : $fileArray['params'];
51+
unset($fileArray['params.']);
52+
// Make $fileArray['params'] at least one space to count that towards hash in order to make TYPO3 to process image even if it would not be processed without this.
53+
$fileArray['params'] = $paramsValue . ' ';
54+
}
4555
return parent::getImgResource($file, $fileArray);
4656
}
4757
}

Classes/Xclass/FileProcessingService.php

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@
2424

2525
namespace SourceBroker\Imageopt\Xclass;
2626

27+
use SourceBroker\Imageopt\Utility\FrontendProcessingUtility;
2728
use TYPO3\CMS\Core\Resource;
28-
use TYPO3\CMS\Core\Utility\GeneralUtility;
2929

3030
class FileProcessingService extends \TYPO3\CMS\Core\Resource\Service\FileProcessingService
3131
{
32-
3332
/**
34-
*
35-
* Xclassed because when the $configuration['additionalParameters'] is changed the hash is changed
36-
* and we force to process file even if regulary TYPO3 would use original.
33+
* Fluid processed images
3734
*
3835
* @param Resource\FileInterface $fileObject The file object
3936
* @param Resource\ResourceStorage $targetStorage The storage to store the processed file in
@@ -50,30 +47,10 @@ public function processFile(
5047
$taskType,
5148
$configuration
5249
) {
53-
/** @var $processedFileRepository Resource\ProcessedFileRepository */
54-
$processedFileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
55-
56-
// LOCC add neutral additionalParameters that will make the image to be processed even if not needed
57-
$configuration['additionalParameters'] = 'MUST_RECREATE';
58-
59-
$processedFile = $processedFileRepository->findOneByOriginalFileAndTaskTypeAndConfiguration($fileObject,
60-
$taskType, $configuration);
61-
62-
// set the storage of the processed file
63-
// Pre-process the file
64-
65-
$this->emitPreFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
66-
67-
// Only handle the file if it is not processed yet
68-
// (maybe modified or already processed by a signal)
69-
// or (in case of preview images) already in the DB/in the processing folder
70-
if (!$processedFile->isProcessed()) {
71-
$this->process($processedFile, $targetStorage);
50+
if (FrontendProcessingUtility::isAllowedToForceFrontendImageProcessing($fileObject->getPublicUrl())) {
51+
// Make additionalParameters at least one space to count that towards hash in order to make TYPO3 to process image even if it would not be processed without this.
52+
$configuration['additionalParameters'] = ' ';
7253
}
73-
74-
// Post-process (enrich) the file
75-
$this->emitPostFileProcessSignal($processedFile, $fileObject, $taskType, $configuration);
76-
77-
return $processedFile;
54+
return parent::processFile($fileObject, $targetStorage, $taskType, $configuration);
7855
}
7956
}

Classes/Xclass/GifBuilder.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

ext_localconf.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\\CMS\\Core\\Resource\\Service\\FileProcessingService'] = [
1717
'className' => 'SourceBroker\\Imageopt\\Xclass\\FileProcessingService'
1818
];
19-
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\\CMS\\Frontend\\Imaging\\GifBuilder'] = [
20-
'className' => 'SourceBroker\\Imageopt\\Xclass\\GifBuilder'
21-
];
2219
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer'] = [
2320
'className' => 'SourceBroker\\Imageopt\\Xclass\\ContentObjectRenderer'
2421
];

ext_typoscript_setup.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
plugin.tx_imageopt {
2+
imageProcessing {
3+
// Force processing of all images on frontend because imageopt should not optimize original images.
4+
force = 1
5+
exclusion {
6+
// Regexp on filepath and filename. When true this file will not be forced to be processed on frontend.
7+
// Example /animation.*\.gif/ -> do not force gif files that have animation in name or folder name.
8+
// Example /\.gif/ -> do not force gif files
9+
regexp =
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)