[TASK] Deprecate GraphicalFunctions->init method 23/58023/3
authorBenni Mack <benni@typo3.org>
Fri, 24 Aug 2018 15:15:35 +0000 (17:15 +0200)
committerAndreas Fernandez <a.fernandez@scripting-base.de>
Sat, 25 Aug 2018 12:27:25 +0000 (14:27 +0200)
The init method of GraphicalFunctions/Gifbuilder was used instead of
a constructor to set up various internal properties, and is now
shifted into a new constructor, making the extra call to init() superfluous.

Resolves: #85978
Releases: master
Change-Id: I71ee3bc8045f48b1636e46681dddd742ba1f56c4
Reviewed-on: https://review.typo3.org/58023
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
15 files changed:
typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
typo3/sysext/core/Classes/Resource/OnlineMedia/Processing/PreviewProcessing.php
typo3/sysext/core/Classes/Resource/Processing/LocalCropScaleMaskHelper.php
typo3/sysext/core/Classes/Resource/Processing/LocalImageProcessor.php
typo3/sysext/core/Classes/Resource/Processing/LocalPreviewHelper.php
typo3/sysext/core/Classes/Type/File/ImageInfo.php
typo3/sysext/core/Documentation/Changelog/master/Deprecation-85978-DeprecateGraphicalFunctions-initMethod.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Imaging/GraphicalFunctionsTest.php
typo3/sysext/core/Tests/Unit/Type/File/ImageInfoTest.php
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Classes/ContentObject/Menu/GraphicalMenuContentObject.php
typo3/sysext/frontend/Classes/ContentObject/Menu/ImageMenuContentObject.php
typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
typo3/sysext/install/Classes/Controller/EnvironmentController.php
typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php

index 0ca004b..e60d637 100644 (file)
@@ -135,9 +135,10 @@ class GraphicalFunctions
     ];
 
     /**
+     * Whether ImageMagick/GraphicsMagick is enabled or not
      * @var bool
      */
-    protected $NO_IMAGE_MAGICK = false;
+    protected $processorEnabled;
 
     /**
      * @var bool
@@ -298,10 +299,10 @@ class GraphicalFunctions
     protected $im;
 
     /**
-     * Init function. Must always call this when using the class.
-     * This function will read the configuration information from $GLOBALS['TYPO3_CONF_VARS']['GFX'] can set some values in internal variables.
+     * Reads configuration information from $GLOBALS['TYPO3_CONF_VARS']['GFX']
+     * and sets some values in internal variables.
      */
-    public function init()
+    public function __construct()
     {
         $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
         if (function_exists('imagecreatefromjpeg') && function_exists('imagejpeg')) {
@@ -319,9 +320,54 @@ class GraphicalFunctions
             $this->colorspace = $gfxConf['processor_colorspace'];
         }
 
-        if (!$gfxConf['processor_enabled']) {
-            $this->NO_IMAGE_MAGICK = true;
+        $this->processorEnabled = (bool)$gfxConf['processor_enabled'];
+        // Setting default JPG parameters:
+        $this->jpegQuality = MathUtility::forceIntegerInRange($gfxConf['jpg_quality'], 10, 100, 85);
+        $this->addFrameSelection = (bool)$gfxConf['processor_allowFrameSelection'];
+        if ($gfxConf['gdlib_png']) {
+            $this->gifExtension = 'png';
+        }
+        $this->imageFileExt = GeneralUtility::trimExplode(',', $gfxConf['imagefile_ext']);
+
+        // Boolean. This is necessary if using ImageMagick 5+.
+        // Effects in Imagemagick 5+ tends to render very slowly!!
+        // - therefore must be disabled in order not to perform sharpen, blurring and such.
+        $this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace ' . $this->colorspace . ' -quality ' . $this->jpegQuality;
+
+        // ... but if 'processor_effects' is set, enable effects
+        if ($gfxConf['processor_effects']) {
+            $this->processorEffectsEnabled = true;
+            $this->cmds['jpg'] .= $this->v5_sharpen(10);
+            $this->cmds['jpeg'] .= $this->v5_sharpen(10);
+        }
+        // Secures that images are not scaled up.
+        $this->mayScaleUp = (bool)$gfxConf['processor_allowUpscaling'];
+        $this->csConvObj = GeneralUtility::makeInstance(CharsetConverter::class);
+    }
+
+    /**
+     * @deprecated will be removed in TYPO3 v10.0. As the constructor does everything directly
+     */
+    public function init()
+    {
+        trigger_error('GraphicalFunctions->init() will be removed in TYPO3 v10.0. Simply remove the call to the method, since this is now evaluted in the constructor.', E_USER_DEPRECATED);
+        $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
+        if (function_exists('imagecreatefromjpeg') && function_exists('imagejpeg')) {
+            $this->gdlibExtensions[] = 'jpg';
+            $this->gdlibExtensions[] = 'jpeg';
+        }
+        if (function_exists('imagecreatefrompng') && function_exists('imagepng')) {
+            $this->gdlibExtensions[] = 'png';
+        }
+        if (function_exists('imagecreatefromgif') && function_exists('imagegif')) {
+            $this->gdlibExtensions[] = 'gif';
+        }
+
+        if ($gfxConf['processor_colorspace'] && in_array($gfxConf['processor_colorspace'], $this->allowedColorSpaceNames, true)) {
+            $this->colorspace = $gfxConf['processor_colorspace'];
         }
+
+        $this->processorEnabled = (bool)$gfxConf['processor_enabled'];
         // Setting default JPG parameters:
         $this->jpegQuality = MathUtility::forceIntegerInRange($gfxConf['jpg_quality'], 10, 100, 85);
         $this->addFrameSelection = (bool)$gfxConf['processor_allowFrameSelection'];
@@ -2053,7 +2099,7 @@ class GraphicalFunctions
      */
     public function imageMagickConvert($imagefile, $newExt = '', $w = '', $h = '', $params = '', $frame = '', $options = [], $mustCreate = false)
     {
-        if ($this->NO_IMAGE_MAGICK) {
+        if (!$this->processorEnabled) {
             // Returning file info right away
             return $this->getImageDimensions($imagefile);
         }
@@ -2406,7 +2452,7 @@ class GraphicalFunctions
      */
     public function imageMagickIdentify($imagefile)
     {
-        if ($this->NO_IMAGE_MAGICK) {
+        if (!$this->processorEnabled) {
             return null;
         }
 
@@ -2449,7 +2495,7 @@ class GraphicalFunctions
      */
     public function imageMagickExec($input, $output, $params, $frame = 0)
     {
-        if ($this->NO_IMAGE_MAGICK) {
+        if (!$this->processorEnabled) {
             return '';
         }
         // If addFrameSelection is set in the Install Tool, a frame number is added to
@@ -2475,7 +2521,7 @@ class GraphicalFunctions
      */
     public function combineExec($input, $overlay, $mask, $output)
     {
-        if ($this->NO_IMAGE_MAGICK) {
+        if (!$this->processorEnabled) {
             return '';
         }
         $theMask = $this->randomName() . '.' . $this->gifExtension;
index 2954985..0b21f23 100644 (file)
@@ -165,9 +165,7 @@ class PreviewProcessing
     protected function cropScaleImage($originalFileName, $temporaryFileName, $configuration)
     {
         if (file_exists($originalFileName)) {
-            /** @var $gifBuilder GifBuilder */
             $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
-            $gifBuilder->init();
 
             $options = $this->getConfigurationForImageCropScaleMask($configuration, $gifBuilder);
             $info = $gifBuilder->getImageDimensions($originalFileName);
@@ -245,10 +243,8 @@ class PreviewProcessing
     /**
      * @return GraphicalFunctions
      */
-    protected function getGraphicalFunctionsObject()
+    protected function getGraphicalFunctionsObject(): GraphicalFunctions
     {
-        $graphicalFunctionsObject = GeneralUtility::makeInstance(GraphicalFunctions::class);
-        $graphicalFunctionsObject->init();
-        return $graphicalFunctionsObject;
+        return GeneralUtility::makeInstance(GraphicalFunctions::class);
     }
 }
index af335aa..b5b3ca4 100644 (file)
@@ -64,9 +64,7 @@ class LocalCropScaleMaskHelper
         $sourceFile = $task->getSourceFile();
 
         $originalFileName = $sourceFile->getForLocalProcessing(false);
-        /** @var $gifBuilder GifBuilder */
         $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
-        $gifBuilder->init();
 
         $configuration = $targetFile->getProcessingConfiguration();
         $configuration['additionalParameters'] = $this->modifyImageMagickStripProfileParameters($configuration['additionalParameters'], $configuration);
index 5c20e50..240b260 100644 (file)
@@ -140,10 +140,8 @@ class LocalImageProcessor implements ProcessorInterface
     /**
      * @return GraphicalFunctions
      */
-    protected function getGraphicalFunctionsObject()
+    protected function getGraphicalFunctionsObject(): GraphicalFunctions
     {
-        $graphicalFunctionsObject = GeneralUtility::makeInstance(GraphicalFunctions::class);
-        $graphicalFunctionsObject->init();
-        return $graphicalFunctionsObject;
+        return GeneralUtility::makeInstance(GraphicalFunctions::class);
     }
 }
index 4fe216f..f14bf6e 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Utility\CommandUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
+use TYPO3\CMS\Frontend\Imaging\GifBuilder;
 
 /**
  * Helper for creating local image previews using TYPO3s image processing classes.
@@ -123,7 +124,6 @@ class LocalPreviewHelper
         ) {
             // Create a default image
             $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
-            $graphicalFunctions->init();
             $graphicalFunctions->getTemporaryImageWithText(
                 $targetFilePath,
                 'Not imagefile!',
@@ -137,9 +137,7 @@ class LocalPreviewHelper
 
         $originalFileName = $file->getForLocalProcessing(false);
         if ($file->getExtension() === 'svg') {
-            /** @var $gifBuilder \TYPO3\CMS\Frontend\Imaging\GifBuilder */
-            $gifBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class);
-            $gifBuilder->init();
+            $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
             $info = $gifBuilder->getImageDimensions($originalFileName);
             $newInfo = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], []);
             $result = [
@@ -159,7 +157,6 @@ class LocalPreviewHelper
                 if (!file_exists($targetFilePath)) {
                     // Create an error gif
                     $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
-                    $graphicalFunctions->init();
                     $graphicalFunctions->getTemporaryImageWithText(
                         $targetFilePath,
                         'No thumb',
index 5cfd90a..45aeda4 100644 (file)
@@ -95,7 +95,6 @@ class ImageInfo extends FileInfo implements LoggerAwareInterface
             // Fallback to IM/GM identify
             if ($this->imageSizes === false) {
                 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
-                $graphicalFunctions->init();
                 $this->imageSizes = $graphicalFunctions->imageMagickIdentify($this->getPathname());
             }
 
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-85978-DeprecateGraphicalFunctions-initMethod.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-85978-DeprecateGraphicalFunctions-initMethod.rst
new file mode 100644 (file)
index 0000000..681db74
--- /dev/null
@@ -0,0 +1,32 @@
+.. include:: ../../Includes.txt
+
+=================================================================
+Deprecation: #85978 - Deprecate GraphicalFunctions->init() method
+=================================================================
+
+See :issue:`85978`
+
+Description
+===========
+
+The init method of :php:`GraphicalFunctions/Gifbuilder` was used instead of a constructor to set up various internal properties, and is now shifted into a new constructor, making the extra call to init() superfluous.
+
+
+Impact
+======
+
+Calling the method directly will trigger a deprecation warning.
+
+
+Affected Installations
+======================
+
+Any TYPO3 installation with extensions directly calling this method.
+
+
+Migration
+=========
+
+No migration needed, the constructor of :php:`GraphicalFunctions/Gifbuilder` takes care of the initalization of all settings.
+
+.. index:: PHP-API, FullyScanned, ext:core
\ No newline at end of file
index b9bf56f..569bd9e 100644 (file)
@@ -14,25 +14,11 @@ namespace TYPO3\CMS\Core\Tests\Unit\Imaging;
  * The TYPO3 project - inspiring people to share!
  */
 
-/**
- * Testcase for \TYPO3\CMS\Core\Imaging\GraphicalFunctions
- */
+use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
+
 class GraphicalFunctionsTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
 {
     /**
-     * @var \TYPO3\CMS\Core\Imaging\GraphicalFunctions
-     */
-    protected $subject;
-
-    /**
-     * Set up
-     */
-    protected function setUp()
-    {
-        $this->subject = new \TYPO3\CMS\Core\Imaging\GraphicalFunctions();
-    }
-
-    /**
      * Dataprovider for getScaleForImage
      *
      * @return array
@@ -104,7 +90,8 @@ class GraphicalFunctionsTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestC
      */
     public function getScaleForImage($info, $width, $height, $options, $expected)
     {
-        $result = $this->subject->getImageScale($info, $width, $height, $options);
+        $this->resetSingletonInstances = true;
+        $result = (new GraphicalFunctions())->getImageScale($info, $width, $height, $options);
         $this->assertEquals($result, $expected);
     }
 }
index f98d4bc..df9886f 100644 (file)
@@ -53,7 +53,6 @@ class ImageInfoTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         ];
 
         $graphicalFunctionsProphecy = $this->prophesize(GraphicalFunctions::class);
-        $graphicalFunctionsProphecy->init()->shouldBeCalled();
         $graphicalFunctionsProphecy->imageMagickIdentify($root->url() . '/' . $testFile)->willReturn(null);
         GeneralUtility::addInstance(GraphicalFunctions::class, $graphicalFunctionsProphecy->reveal());
 
index 3ef023c..766fca4 100644 (file)
@@ -4618,9 +4618,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
         }
         $imageResource = null;
         if ($file === 'GIFBUILDER') {
-            /** @var GifBuilder $gifCreator */
             $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-            $gifCreator->init();
             $theImage = '';
             if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
                 $gifCreator->start($fileArray, $this->data);
@@ -4725,9 +4723,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
         if (!isset($imageResource)) {
             try {
                 $theImage = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize((string)$file);
-                $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-                $gifCreator->init();
-                $info = $gifCreator->imageMagickConvert($theImage, 'WEB');
+                $info = GeneralUtility::makeInstance(GifBuilder::class)->imageMagickConvert($theImage, 'WEB');
                 $info['origFile'] = $theImage;
                 // This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder, ln 100ff in order for the setup-array to create a unique filename hash.
                 $info['origFile_mtime'] = @filemtime($theImage);
index a13bcf6..1cca967 100644 (file)
@@ -153,7 +153,6 @@ class GraphicalMenuContentObject extends AbstractMenuContentObject
             if ($isGD) {
                 // Pre-working the item
                 $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-                $gifCreator->init();
                 $gifCreator->start($val, $this->menuArr[$key]);
                 // If useLargestItemH/W is specified
                 if (!empty($totalWH) && ($this->mconf['useLargestItemX'] || $this->mconf['useLargestItemY'])) {
@@ -167,7 +166,6 @@ class GraphicalMenuContentObject extends AbstractMenuContentObject
                     // Regenerate the new values...
                     $val['XY'] = implode(',', $tempXY);
                     $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-                    $gifCreator->init();
                     $gifCreator->start($val, $this->menuArr[$key]);
                 }
                 // If distributeH/W is specified
@@ -188,7 +186,6 @@ class GraphicalMenuContentObject extends AbstractMenuContentObject
                     // Regenerate the new values...
                     $val['XY'] = implode(',', $tempXY);
                     $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-                    $gifCreator->init();
                     $gifCreator->start($val, $this->menuArr[$key]);
                 }
                 // If max dimensions are specified
@@ -205,7 +202,6 @@ class GraphicalMenuContentObject extends AbstractMenuContentObject
                     if ($maxFlag) {
                         $val['XY'] = implode(',', $tempXY);
                         $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-                        $gifCreator->init();
                         $gifCreator->start($val, $this->menuArr[$key]);
                     }
                 }
@@ -323,7 +319,6 @@ class GraphicalMenuContentObject extends AbstractMenuContentObject
             }
             $c++;
             $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-            $gifCreator->init();
             $gifCreator->start($val, $this->menuArr[$key]);
             if ($maxDim) {
                 $tempXY = explode(',', $val['XY']);
@@ -338,7 +333,6 @@ class GraphicalMenuContentObject extends AbstractMenuContentObject
                 if ($maxFlag) {
                     $val['XY'] = implode(',', $tempXY);
                     $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-                    $gifCreator->init();
                     $gifCreator->start($val, $this->menuArr[$key]);
                 }
             }
index 3d7dc4c..be0829b 100644 (file)
@@ -61,7 +61,6 @@ class ImageMenuContentObject extends AbstractMenuContentObject
         }
         if (is_array($this->mconf['main.'])) {
             $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-            $gifCreator->init();
             $itemsConf = $conf;
             $conf = $this->mconf['main.'];
             if (is_array($conf)) {
index 30a2275..daeba4a 100644 (file)
@@ -62,7 +62,7 @@ class PageLinkBuilder extends AbstractTypolinkBuilder
         if (empty($page)) {
             throw new UnableToLinkException('Page id "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.', 1490987336, null, $linkText);
         }
-        $language = $page[$GLOBALS['TCA']['pages']['ctrl']['languageField']];
+        $language = (int)$page[$GLOBALS['TCA']['pages']['ctrl']['languageField']];
         if ($language === 0 && GeneralUtility::hideIfDefaultLanguage($page['l18n_cfg'])) {
             throw new UnableToLinkException('Default language of page  "' . $linkDetails['typoLinkParameter'] . '" is hidden, so "' . $linkText . '" was not linked.', 1529527301, null, $linkText);
         }
index d2ca399..5be842e 100644 (file)
@@ -826,7 +826,6 @@ class EnvironmentController extends AbstractController
     protected function initializeImageProcessor(): GraphicalFunctions
     {
         $imageProcessor = GeneralUtility::makeInstance(GraphicalFunctions::class);
-        $imageProcessor->init();
         $imageProcessor->dontCheckForExistingTempFile = true;
         $imageProcessor->filenamePrefix = 'installTool-';
         $imageProcessor->dontCompress = true;
index a037475..f67192e 100644 (file)
@@ -3005,4 +3005,18 @@ return [
             'Deprecation-85960-CompareUidentDeprecated.rst'
         ],
     ],
+    'TYPO3\CMS\Core\Imaging\GraphicalFunctions->init' => [
+        'numberOfMandatoryArguments' => 0,
+        'maximumNumberOfArguments' => 0,
+        'restFiles' => [
+            'Deprecation-85978-DeprecateGraphicalFunctions-initMethod.rst'
+        ],
+    ],
+    'TYPO3\CMS\Frontend\Imaging\GifBuilder->init' => [
+        'numberOfMandatoryArguments' => 0,
+        'maximumNumberOfArguments' => 0,
+        'restFiles' => [
+            'Deprecation-85978-DeprecateGraphicalFunctions-initMethod.rst'
+        ],
+    ],
 ];