diff --git a/Classes/FileAspects.php b/Classes/FileAspects.php new file mode 100644 index 0000000..85a1307 --- /dev/null +++ b/Classes/FileAspects.php @@ -0,0 +1,31 @@ +service = GeneralUtility::makeInstance(OptimizeImageService::class); + } + + /** + * Called when a file was processed + * + * @param \TYPO3\CMS\Core\Resource\Service\FileProcessingService $fileProcessingService + * @param \TYPO3\CMS\Core\Resource\Driver\DriverInterface $driver + * @param \TYPO3\CMS\Core\Resource\ProcessedFile $processedFile + */ + public function processFile($fileProcessingService, $driver, $processedFile) { + if ($processedFile->isUpdated() === TRUE) { + // ToDo: Find better possibility for getPublicUrl() + $this->service->process(PATH_site . $processedFile->getPublicUrl(), $processedFile->getExtension()); + } + } +} \ No newline at end of file diff --git a/Classes/Hook/ClearImages.php b/Classes/Hook/ClearImages.php new file mode 100644 index 0000000..bbfea33 --- /dev/null +++ b/Classes/Hook/ClearImages.php @@ -0,0 +1,87 @@ +getTSConfigVal('options.clearCache.tx_cswebp') == NULL || $GLOBALS['BE_USER']->getTSConfigVal('options.clearCache.tx_cswebp')) { + $title = LocalizationUtility::translate('cache_action.title', 'cs_webp'); + + $identifier = 'clear_processed_images_icon'; + $iconRegistry = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\IconRegistry::class); + $iconRegistry->registerIcon($identifier, \TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider::class, ['source' => 'EXT:cs_webp/Resources/Public/Images/clear_cache_icon.png']); + + $iconFactory = GeneralUtility::makeInstance(IconFactory::class); + $icon = $iconFactory->getIcon($identifier, \TYPO3\CMS\Core\Imaging\Icon::SIZE_SMALL); + + // Clearing of processed images + $cacheActions[] = array( + 'id' => 'tx_cswebp', + 'title' => $title, + 'href' => BackendUtility::getModuleUrl('tce_db', [ + 'vC' => $GLOBALS['BE_USER']->veriCode(), + 'cacheCmd' => 'tx_cswebp', + 'ajaxCall' => 1 + ]), + 'icon' => $icon + ); + } + } + + /** + * This method is called by the CacheMenuItem in the Backend + * @param \array $_params + * @param \TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler + */ + public static function clear($_params, $dataHandler) { + if ($_params['cacheCmd'] == 'tx_cswebp') { + $repository = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository'); + $cacheManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager'); + + // remove all processed files + $repository->removeAll(); + + $command = sprintf('rm -rf %sfileadmin/_processed_/*', PATH_site); + CommandUtility::exec($command); + + // clear page caches + $cacheManager->flushCachesInGroup('pages'); + } + } +} \ No newline at end of file diff --git a/Classes/Service/OptimizeImageService.php b/Classes/Service/OptimizeImageService.php new file mode 100644 index 0000000..0cb48fb --- /dev/null +++ b/Classes/Service/OptimizeImageService.php @@ -0,0 +1,44 @@ +configuration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['cs_webp']); + } + + /** + * Perform image optimization + * + * @param string $file + * @param string $extension + */ + public function process($file, $extension = NULL) { + if ($extension === NULL) { + $pathinfo = pathinfo($file); + if ($pathinfo['extension'] !== NULL) { + $extension = $pathinfo['extension']; + } + } + $extension = strtolower($extension); + + if ($extension == 'jpg' || $extension == 'jpeg' || $extension == 'png') { + $webpfile = str_replace("." . $extension, ".webp", $file); + $command = sprintf('convert %s -define webp:lossless=true %s', $file, $webpfile); + } + + if (isset($command)) { + $output = []; + $returnValue = 0; + CommandUtility::exec($command, $output, $returnValue); + if ((bool)$this->configuration['debug'] === TRUE && is_object($GLOBALS['BE_USER'])) { + $GLOBALS['BE_USER']->simplelog($command . ' exited with ' . $returnValue . '. Output was: ' . implode(' ', $output), 'cs_webp', 0); + } + } + } +} \ No newline at end of file diff --git a/Documentation/Images.txt b/Documentation/Images.txt new file mode 100644 index 0000000..0645faa --- /dev/null +++ b/Documentation/Images.txt @@ -0,0 +1,10 @@ +.. |img-1| image:: Images/Clickstorm.jpg + :height: 256 + :width: 256 +.. :align: center +.. :alt: clickstorm logo +.. :border: 0 +.. :name: clickstorm logo +.. :vspace: 52 +.. :target: http://www.clickstorm.de + diff --git a/Documentation/Images/Clickstorm.jpg b/Documentation/Images/Clickstorm.jpg new file mode 100644 index 0000000..bbc4847 Binary files /dev/null and b/Documentation/Images/Clickstorm.jpg differ diff --git a/Documentation/Images/Network.jpg b/Documentation/Images/Network.jpg new file mode 100644 index 0000000..76047ce Binary files /dev/null and b/Documentation/Images/Network.jpg differ diff --git a/Documentation/Images/Screenshot.png b/Documentation/Images/Screenshot.png new file mode 100644 index 0000000..d1920b6 Binary files /dev/null and b/Documentation/Images/Screenshot.png differ diff --git a/Documentation/Includes.txt b/Documentation/Includes.txt new file mode 100644 index 0000000..a111144 --- /dev/null +++ b/Documentation/Includes.txt @@ -0,0 +1,21 @@ +.. ================================================== +.. FOR YOUR INFORMATION +.. -------------------------------------------------- +.. -*- coding: utf-8 -*- with BOM. + +.. This is 'Includes.txt'. It is included at the very top of each and + every ReST source file in this documentation project (= manual). + + +.. ================================================== +.. DEFINE SOME TEXT ROLES +.. -------------------------------------------------- + +.. role:: typoscript(code) + +.. role:: ts(typoscript) + :class: typoscript + +.. role:: php(code) + +.. highlight:: php diff --git a/Documentation/Index.rst b/Documentation/Index.rst new file mode 100644 index 0000000..30b7680 --- /dev/null +++ b/Documentation/Index.rst @@ -0,0 +1,60 @@ +.. ================================================== +.. FOR YOUR INFORMATION +.. -------------------------------------------------- +.. -*- coding: utf-8 -*- with BOM. + +.. include:: Includes.txt +.. include:: Images.txt + +|img-1| + +.. _start: + +============================================================= +Clear Processed Images +============================================================= + +.. only:: html + + :Classification: + cs_webp + + :Version: + |release| + + :Language: + en + + :Description: + The extension stores png and jpg images in webp format too (only fileadmin/_processed_ folder). The backend user can delete the webp files with deleting the processed images by a new cache action. This extension can increase the points of Google PageSpeed Insights. + + :Keywords: + webp, processed, images, editors, cache, pagespeed + + :Copyright: + 2017 + + :Author: + Angela Dudtkowski + + :Email: + dudtkowski@clickstorm.de + + :License: + This document is published under the Open Content License + available from http://www.opencontent.org/opl.shtml + + :Rendered: + |today| + + The content of this document is related to TYPO3, + a GNU/GPL CMS/Framework available from `www.typo3.org `_. + + **Table of Contents** + +.. toctree:: + :maxdepth: 3 + :titlesonly: + + Introduction/Index + Links diff --git a/Documentation/Introduction/Index.rst b/Documentation/Introduction/Index.rst new file mode 100644 index 0000000..bdaac96 --- /dev/null +++ b/Documentation/Introduction/Index.rst @@ -0,0 +1,84 @@ +.. ================================================== +.. FOR YOUR INFORMATION +.. -------------------------------------------------- +.. -*- coding: utf-8 -*- with BOM. + +.. include:: ../Includes.txt + +.. _introduction: + +Introduction +============ + + +.. _what-it-does: + +What does it do? +---------------- + +The extension copies and converts all png and jpg images in the fileadmin/_processed_ folder to the webp format. Browser which could read webp images load the webp images instead of the jpg or png files. The webp images are generated when new processed images in jpg or png format are added in the _processed_ folder. + +You can reach more Points on Google PageSpeed Insights if webp images are rendered. + +On the other hand the extension adds a link to the clear cache menu (flush caches) for editors and admins. On click the processed images will be +deleted like the button in the install tool would do. But with this action the webp images are deleted also. Afterwards the frontend cache is cleared automatically. + +This link in the clear cache menu could be deactivated for editors. For this just add options.clearCache.tx_cswebp = 0 in the UserTSConfig. + +.. important:: + + Every jpg or png image is stored as webp file a second time in the folder (bigger storage size). + + +.. _installation: + +Installation +---------------- + +- install the extension +- clear the processed images in the install tool +- reload the backend +- add the code in the head of your root .htaccess file. This .htaccess Code rewrites the jpg and png images to the webp files if webp is readable in the browser. + +.. code-block:: php + + + RewriteEngine On + RewriteCond %{HTTP_ACCEPT} image/webp + RewriteCond %{DOCUMENT_ROOT}/$1.webp -f + RewriteRule ^(fileadmin/_processed_.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1] + + + + Header append Vary Accept env=REDIRECT_accept + + + AddType image/webp .webp + + +.. _check-if-webp-images-are-rendered: + +Check if webp images are rendered +---------------- + + +.. figure:: ../Images/Network.jpg + :width: 1000px + :alt: Check if webp images are rendered + + You can check if the webp image is rendered in the network section in your developer tool of the browser. + If the response header of the image is image/webp, the webp image is rendered. Notice that the filename itself doesn't change to webp. + + +.. _screenshots: + +Screenshots +----------- + + + +.. figure:: ../Images/Screenshot.png + :width: 516px + :alt: Clear processed images and webp images + + The clear processed images and webp images link for editors and admins. \ No newline at end of file diff --git a/Documentation/Links.rst b/Documentation/Links.rst new file mode 100644 index 0000000..798e11c --- /dev/null +++ b/Documentation/Links.rst @@ -0,0 +1,27 @@ +.. ================================================== +.. FOR YOUR INFORMATION +.. -------------------------------------------------- +.. -*- coding: utf-8 -*- with BOM. + +.. include:: Includes.txt + + +.. _links: + +Links +----- + +:TER: + https://typo3.org/extensions/repository/view/cs_webp + +:Bug Tracker: + https://forge.typo3.org/projects/extension-cs_webp + +:Git Repository: + https://github.com/clickstorm/cs_webp + +:Contact: + `@clickstorm_gmbh `__ + +:Website: + http://www.clickstorm.de diff --git a/Documentation/Settings.yml b/Documentation/Settings.yml new file mode 100644 index 0000000..b95b866 --- /dev/null +++ b/Documentation/Settings.yml @@ -0,0 +1,32 @@ +# This is the project specific Settings.yml file. +# Place Sphinx specific build information here. +# Settings given here will replace the settings of 'conf.py'. + +# Below is an example of intersphinx mapping declaration +# Add more mappings depending on what manual you want to link to +# Remove entirely if you don't need cross-linking + +--- +conf.py: + copyright: 2017 + project: Convert images to webp + version: 1 + release: 1 + intersphinx_mapping: + t3tsref: + - http://docs.typo3.org/typo3cms/TyposcriptReference/ + - null + latex_documents: + - - Index + - cs_webp.tex + - Convert images to webp + - Angela Dudtkowski + - manual + latex_elements: + papersize: a4paper + pointsize: 10pt + preamble: \usepackage + html_theme_options: + github_repository: TYPO3-Documentation/TYPO3CMS-Example-ExtensionManual + github_branch: latest +... diff --git a/README.md b/README.md index 57752aa..8905b18 100644 --- a/README.md +++ b/README.md @@ -1 +1,17 @@ -# cs_webp \ No newline at end of file +# [clickstorm] Convert images to Webp + +Clickstorm Clear Processed Images is a TYPO3 extension that stores png and jpg images in webp format too (only fileadmin/_processed_ folder). The backend user can delete the webp files with deleting the processed images by a new cache action. + +Feel free to test and give feedback. + +## More information + + * [TYPO3 Repository] - Official download page + * [Extension Manual] - Official documentation with all features and configurations + * [clickstorm Blog] - Current information in german + + + +[TYPO3 Repository]: +[Extension Manual]: +[clickstorm Blog]: diff --git a/Resources/Private/.htaccess b/Resources/Private/.htaccess new file mode 100644 index 0000000..96d0729 --- /dev/null +++ b/Resources/Private/.htaccess @@ -0,0 +1,11 @@ +# Apache < 2.3 + + Order allow,deny + Deny from all + Satisfy All + + +# Apache >= 2.3 + + Require all denied + diff --git a/Resources/Private/Language/de.locallang.xlf b/Resources/Private/Language/de.locallang.xlf new file mode 100644 index 0000000..957320a --- /dev/null +++ b/Resources/Private/Language/de.locallang.xlf @@ -0,0 +1,14 @@ + + + +
+ LFEditor +
+ + + Clear Processed Images (webp images too) + Lösche verarbeitete Bilder (auch webp-Bilder) + + +
+
\ No newline at end of file diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf new file mode 100644 index 0000000..29492ec --- /dev/null +++ b/Resources/Private/Language/locallang.xlf @@ -0,0 +1,13 @@ + + + +
+ LFEditor +
+ + + Clear Processed Images (webp images too) + + +
+
\ No newline at end of file diff --git a/Resources/Public/Images/clear_cache_icon.png b/Resources/Public/Images/clear_cache_icon.png new file mode 100644 index 0000000..d0428ab Binary files /dev/null and b/Resources/Public/Images/clear_cache_icon.png differ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..bfb814d --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "clickstorm/cs_webp", + "type": "typo3-cms-extension", + "description": "[clickstorm] Convert images to webp", + "homepage": "https://www.clickstorm.de", + "license": [ + "GPL-2.0+" + ], + "keywords": [ + "typo3", + "cms", + "clickstorm" + ], + "version": "1.0.0", + "dist": { + "url": "https://github.com/clickstorm/cs_webp.git", + "type": "git" + }, + "replace": { + "cs_webp": "*", + "cs_clear_images": "*" + }, + "require": { + "typo3/cms-core": "7.6" + }, + "suggest": { + }, + "autoload": { + "psr-4": { + "Clickstorm\\CsWebp\\": "Classes/" + } + } +} \ No newline at end of file diff --git a/ext_conf_template.txt b/ext_conf_template.txt new file mode 100644 index 0000000..8b6acb8 --- /dev/null +++ b/ext_conf_template.txt @@ -0,0 +1,2 @@ +# cat=basic/debug/1000; type=boolean; label=Enable Debugging. The output goes to backend log module when you are logged in +debug = 0 \ No newline at end of file diff --git a/ext_emconf.php b/ext_emconf.php new file mode 100644 index 0000000..b0c1145 --- /dev/null +++ b/ext_emconf.php @@ -0,0 +1,25 @@ + '[clickstorm] Convert images to webp', + 'description' => 'Store png and jpg images in webp format too (only fileadmin/_processed_ folder). Delete webp files with processed images. Increases the points of Google PageSpeed Insights.', + 'category' => 'plugin', + 'author' => 'Angela Dudtkowski', + 'author_email' => 'dudtkowski@clickstorm.de', + 'author_company' => 'clickstorm GmbH', + 'uploadfolder' => false, + 'state' => 'stable', + 'clearCacheOnLoad' => 1, + 'version' => '1.0.0', + 'constraints' => [ + 'depends' => [ + 'typo3' => '7.6.0-7.9.99', + ], + 'conflicts' => [], + 'suggests' => [], + ], +]; diff --git a/ext_icon.png b/ext_icon.png new file mode 100644 index 0000000..64518fe Binary files /dev/null and b/ext_icon.png differ diff --git a/ext_localconf.php b/ext_localconf.php new file mode 100644 index 0000000..5f7c5f4 --- /dev/null +++ b/ext_localconf.php @@ -0,0 +1,20 @@ +connect( + 'TYPO3\\CMS\\Core\\Resource\\ResourceStorage', + \TYPO3\CMS\Core\Resource\Service\FileProcessingService::SIGNAL_PostFileProcess, + 'Clickstorm\\CsWebp\\FileAspects', + 'processFile' +); + +/* + * add hooks to clear cache + */ +// The Backend-MenuItem in ClearCache-Pulldown +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions']['tx_cswebp'] = + 'EXT:cs_webp/Classes/Hook/ClearImages.php:Clickstorm\\CsWebp\\Hook\\ClearImages'; + +// The AjaxCall to clear the cache +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc'][] = + 'EXT:cs_webp/Classes/Hook/ClearImages.php:Clickstorm\\CsWebp\\Hook\\ClearImages->clear'; \ No newline at end of file