Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

Commit

Permalink
[INIT]
Browse files Browse the repository at this point in the history
  • Loading branch information
adudtkowski committed Feb 24, 2017
1 parent fdcf77b commit d6dbbcb
Show file tree
Hide file tree
Showing 22 changed files with 531 additions and 1 deletion.
31 changes: 31 additions & 0 deletions Classes/FileAspects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Clickstorm\CsWebp;

use Clickstorm\CsWebp\Service\OptimizeImageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class FileAspects {

/**
* @var OptimizeImageService
*/
protected $service;

public function __construct() {
$this->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());
}
}
}
87 changes: 87 additions & 0 deletions Classes/Hook/ClearImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace Clickstorm\CsWebp\Hook;

/***************************************************************
*
* Copyright notice
*
* (c) 2017 Angela Dudtkowski
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Core\Utility\CommandUtility;

class ClearImages implements \TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface {
/**
* Add an entry to the CacheMenuItems array
*
* @param array $cacheActions Array of CacheMenuItems
* @param array $optionValues Array of AccessConfigurations-identifiers (typically used by userTS with options.clearCache.identifier)
*/
public function manipulateCacheActions(&$cacheActions, &$optionValues) {
if ($GLOBALS['BE_USER']->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');
}
}
}
44 changes: 44 additions & 0 deletions Classes/Service/OptimizeImageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Clickstorm\CsWebp\Service;

use TYPO3\CMS\Core\Utility\CommandUtility;

class OptimizeImageService {

/**
* Initialize
*/
public function __construct() {
$this->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);
}
}
}
}
10 changes: 10 additions & 0 deletions Documentation/Images.txt
Original file line number Diff line number Diff line change
@@ -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

Binary file added Documentation/Images/Clickstorm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Documentation/Images/Network.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Documentation/Images/Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions Documentation/Includes.txt
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions Documentation/Index.rst
Original file line number Diff line number Diff line change
@@ -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:
[email protected]

: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 <http://www.typo3.org/>`_.

**Table of Contents**

.. toctree::
:maxdepth: 3
:titlesonly:

Introduction/Index
Links
84 changes: 84 additions & 0 deletions Documentation/Introduction/Index.rst
Original file line number Diff line number Diff line change
@@ -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
<IfModule mod_rewrite.c>
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]
</IfModule>
<IfModule mod_headers.c>
Header append Vary Accept env=REDIRECT_accept
</IfModule>
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.
27 changes: 27 additions & 0 deletions Documentation/Links.rst
Original file line number Diff line number Diff line change
@@ -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 <https://twitter.com/clickstorm_gmbh>`__

:Website:
http://www.clickstorm.de
Loading

0 comments on commit d6dbbcb

Please sign in to comment.