-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58d0947
commit eb8e8c1
Showing
17 changed files
with
212 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (C) 2015-2019 Christian Barkowsky | ||
* | ||
* @author Christian Barkowsky <[email protected]> | ||
* @copyright Christian Barkowsky <https://brkwsky.de> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
* @copyright Copyright (c) 2015-2021, Plenta.io & Christian Barkowsky | ||
* @author Christian Barkowsky <[email protected]> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
*/ | ||
|
||
namespace Barkowsky; | ||
|
||
use Contao\Image; | ||
use Contao\System; | ||
use Contao\Message; | ||
use Contao\Request; | ||
use Contao\FilesModel; | ||
use Contao\StringUtil; | ||
use Contao\Controller; | ||
use Contao\CoreBundle\Monolog\ContaoContext; | ||
|
||
/** | ||
* Class TinyCompressImages | ||
* @package Barkowsky | ||
* Hook "postUpload". | ||
*/ | ||
class TinyCompressImages extends System | ||
{ | ||
/** | ||
* Compress images | ||
* | ||
* @param boolean $arrFiles File array | ||
* @var array|string[] | ||
*/ | ||
public function processPostUpload($arrFiles) | ||
protected array $extensions = [ | ||
'png', 'jpg', 'jpeg', 'webp' | ||
]; | ||
|
||
/** | ||
* Compress images. | ||
* @param array $files | ||
* @return void | ||
*/ | ||
public function processPostUpload(array $files): void | ||
{ | ||
if (is_array($arrFiles) && $GLOBALS['TL_CONFIG']['tinypng_api_key'] != '') { | ||
if (count($files) > 0 && !empty($GLOBALS['TL_CONFIG']['tinypng_api_key'])) { | ||
$strUrl = 'https://api.tinypng.com/shrink'; | ||
$strKey = $GLOBALS['TL_CONFIG']['tinypng_api_key']; | ||
$strAuthorization = 'Basic '.base64_encode("api:$strKey"); | ||
$logger = System::getContainer()->get('monolog.logger.contao'); | ||
$compressionCount = null; | ||
|
||
foreach ($arrFiles as $file) { | ||
foreach ($files as $file) { | ||
$objFile = FilesModel::findByPath($file); | ||
|
||
if (in_array($objFile->extension, array('png', 'jpg', 'jpeg'))) { | ||
$strFile = TL_ROOT . '/' . $objFile->path; | ||
if (in_array($objFile->extension, $this->extensions, true)) { | ||
$strFile = TL_ROOT.'/'.$objFile->path; | ||
|
||
$objRequest = new Request(); | ||
$objRequest->method = 'post'; | ||
|
@@ -46,26 +59,72 @@ public function processPostUpload($arrFiles) | |
$objRequest->setHeader('Authorization', $strAuthorization); | ||
$objRequest->send($strUrl); | ||
|
||
$compressionCount = $objRequest->headers['Compression-Count']; | ||
$arrResponse = json_decode($objRequest->response); | ||
|
||
if ($objRequest->code == 201) { | ||
file_put_contents($strFile, fopen($arrResponse->output->url, "rb", false)); | ||
if (201 == $objRequest->code) { | ||
file_put_contents($strFile, fopen($arrResponse->output->url, 'r', false)); | ||
|
||
$objFile->tstamp = time(); | ||
$objFile->path = $file; | ||
$objFile->hash = md5_file(TL_ROOT . '/' . $objFile->path); | ||
$objFile->path = $file; | ||
$objFile->hash = md5_file(TL_ROOT.'/'.$objFile->path); | ||
$objFile->compressed = true; | ||
$objFile->save(); | ||
|
||
System::log('Compression was successful. (File: ' . $objFile->path . ')', __METHOD__, TL_FILES); | ||
Message::addInfo( | ||
sprintf($GLOBALS['TL_LANG']['MSC']['TINYCOMPRESSIMAGES']['successful'], $objFile->path) | ||
); | ||
|
||
$logger->addInfo( | ||
sprintf($GLOBALS['TL_LANG']['MSC']['TINYCOMPRESSIMAGES']['successful'], $objFile->path), | ||
['contao' => new ContaoContext(__METHOD__, ContaoContext::FILES)] | ||
); | ||
} else { | ||
System::log( | ||
'Compression failed. (' . $arrResponse->message . ') (File: ' . $objFile->path . ')', | ||
METHOD__, | ||
TL_FILES | ||
Message::addError( | ||
sprintf( | ||
$GLOBALS['TL_LANG']['MSC']['TINYCOMPRESSIMAGES']['failed'], | ||
$objFile->path, | ||
$arrResponse->message | ||
) | ||
); | ||
|
||
$logger->addError( | ||
sprintf( | ||
$GLOBALS['TL_LANG']['MSC']['TINYCOMPRESSIMAGES']['failed'], | ||
$objFile->path, | ||
$arrResponse->message | ||
), | ||
['contao' => new ContaoContext(__METHOD__, ContaoContext::FILES)] | ||
); | ||
} | ||
} | ||
} | ||
|
||
if (null !== $compressionCount) { | ||
Message::addInfo( | ||
sprintf($GLOBALS['TL_LANG']['MSC']['TINYCOMPRESSIMAGES']['count'], $compressionCount) | ||
); | ||
|
||
$logger->addNotice( | ||
sprintf($GLOBALS['TL_LANG']['MSC']['TINYCOMPRESSIMAGES']['count'], $compressionCount), | ||
['contao' => new ContaoContext(__METHOD__, ContaoContext::GENERAL)] | ||
); | ||
} | ||
} | ||
} | ||
|
||
public function addIcon(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes) | ||
{ | ||
if ('folder' === (string) $row['type']) { | ||
return ''; | ||
} | ||
|
||
$model = FilesModel::findByPath(rawurldecode($row['id'])); | ||
|
||
if (null !== $model && true === (bool) $model->compressed) { | ||
return Image::getHtml($icon, $label); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
;; | ||
requires[] = "core" | ||
|
||
|
||
;; | ||
; Configure what you want the autoload creator to register | ||
;; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright (C) 2015-2016 Christian Barkowsky | ||
* | ||
* @author Christian Barkowsky <[email protected]> | ||
* @copyright Christian Barkowsky <http://christianbarkowsky.de> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
* @copyright Copyright (c) 2015-2021, Plenta.io & Christian Barkowsky | ||
* @author Christian Barkowsky <[email protected]> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
*/ | ||
|
||
|
||
\Contao\ClassLoader::addNamespace('Barkowsky'); | ||
|
||
|
||
/** | ||
* Register the classes | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,10 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright (C) 2015-2016 Christian Barkowsky | ||
* | ||
* @author Christian Barkowsky <[email protected]> | ||
* @copyright Christian Barkowsky <http://christianbarkowsky.de> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
* @copyright Copyright (c) 2015-2021, Plenta.io & Christian Barkowsky | ||
* @author Christian Barkowsky <[email protected]> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
*/ | ||
|
||
|
||
/** | ||
* Hook | ||
*/ | ||
$GLOBALS['TL_HOOKS']['postUpload'][] = array('TinyCompressImages', 'processPostUpload'); | ||
$GLOBALS['TL_HOOKS']['postUpload'][] = ['TinyCompressImages', 'processPostUpload']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (c) 2015-2021, Plenta.io & Christian Barkowsky | ||
* @author Christian Barkowsky <[email protected]> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
*/ | ||
|
||
use Barkowsky\TinyCompressImages; | ||
|
||
$GLOBALS['TL_DCA']['tl_files']['list']['operations']['tinify'] = [ | ||
'label' => &$GLOBALS['TL_LANG']['tl_files']['tinify'], | ||
'icon' => 'bundles/tiny-compress-images/tinyfy.png', | ||
'button_callback' => [TinyCompressImages::class, 'addIcon'] | ||
]; | ||
|
||
$GLOBALS['TL_DCA']['tl_files']['fields']['compressed'] = [ | ||
'sql' => "char(1) NOT NULL default ''" | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright (C) 2015-2016 Christian Barkowsky | ||
* | ||
* @author Christian Barkowsky <[email protected]> | ||
* @copyright Christian Barkowsky <http://christianbarkowsky.de> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
* @copyright Copyright (c) 2015-2021, Plenta.io & Christian Barkowsky | ||
* @author Christian Barkowsky <[email protected]> | ||
* @package tiny-compress-images | ||
* @license LGPL | ||
*/ | ||
|
||
use Contao\CoreBundle\DataContainer\PaletteManipulator; | ||
|
||
/** | ||
* Palettes | ||
*/ | ||
$GLOBALS['TL_DCA']['tl_settings']['palettes']['default'] = $GLOBALS['TL_DCA']['tl_settings']['palettes']['default'] . ';{tiny_compress_images_legend},tinypng_api_key'; | ||
|
||
$GLOBALS['TL_DCA']['tl_settings']['fields']['tinypng_api_key'] = [ | ||
'label' => &$GLOBALS['TL_LANG']['tl_settings']['tinypng_api_key'], | ||
'exclude' => true, | ||
'inputType' => 'text', | ||
'eval' => ['maxlength' => 255, 'tl_class' => 'w50'], | ||
]; | ||
|
||
/** | ||
* Fields | ||
*/ | ||
$GLOBALS['TL_DCA']['tl_settings']['fields']['tinypng_api_key'] = array | ||
( | ||
'label' => &$GLOBALS['TL_LANG']['tl_settings']['tinypng_api_key'], | ||
'exclude' => true, | ||
'inputType' => 'text', | ||
'eval' => array('maxlength'=>255, 'tl_class'=>'w50') | ||
); | ||
PaletteManipulator::create() | ||
->addLegend('tiny_compress_images_legend', 'chmod_legend', PaletteManipulator::POSITION_AFTER) | ||
->addField('tinypng_api_key', 'tiny_compress_images_legend', PaletteManipulator::POSITION_APPEND) | ||
->applyToPalette('default', 'tl_settings') | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xliff version="1.1"> | ||
<file> | ||
<body> | ||
<trans-unit id="MSC.TINYCOMPRESSIMAGES.successful"> | ||
<source>File %s compression was successful.</source> | ||
<target>Die Datei %s wurde erfolgreich komprimiert.</target> | ||
</trans-unit> | ||
<trans-unit id="MSC.TINYCOMPRESSIMAGES.failed"> | ||
<source>File %s compression failed (%s).</source> | ||
<target>Dateikomprimierung %s fehlgeschlagen (%s).</target> | ||
</trans-unit> | ||
<trans-unit id="MSC.TINYCOMPRESSIMAGES.count"> | ||
<source>You made %s compressions this month (tinyfy.com).</source> | ||
<target>Du hast %s Bildkompressionen in diesem Monat durchgeführt (tinyfy.com).</target> | ||
</trans-unit> | ||
</body> | ||
</file> | ||
</xliff> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xliff version="1.1"> | ||
<file> | ||
<body> | ||
<trans-unit id="tl_files.tinify"> | ||
<source>Image is compressed.</source> | ||
<target>Bild ist komprimiert.</target> | ||
</trans-unit> | ||
</body> | ||
</file> | ||
</xliff> | ||
|
||
|
Oops, something went wrong.