Skip to content

Commit

Permalink
1.1.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbarkowsky committed Dec 28, 2021
1 parent 58d0947 commit eb8e8c1
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 114 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

[![Latest Stable Version](https://poser.pugx.org/christianbarkowsky/contao-tiny-compress-images/v/stable)](https://packagist.org/packages/christianbarkowsky/contao-tiny-compress-images) [![Total Downloads](https://poser.pugx.org/christianbarkowsky/contao-tiny-compress-images/downloads)](https://packagist.org/packages/christianbarkowsky/contao-tiny-compress-images) [![License](https://poser.pugx.org/christianbarkowsky/contao-tiny-compress-images/license)](https://packagist.org/packages/christianbarkowsky/contao-tiny-compress-images)

**Speed up your website. Optimize your JPEG and PNG images automatically with TinyPNG.**
**Speed up your website. Smart WebP, PNG and JPEG compression**

This plugin automatically optimizes your images by integrating with the popular image compression services TinyJPG and TinyPNG.


## Install using Contao Manager

Search for **tinyjpg** or **tinypng** and you will find this extension.
Search for **tinyjpg**, **tinypng** or **tinywebp** and you will find this extension.

## Install using Composer

Expand All @@ -21,8 +21,3 @@ Search for **tinyjpg** or **tinypng** and you will find this extension.
Install this extension and obtain your free API key from [https://tinypng.com/developers](https://tinypng.com/developers).
The first 500 compressions per month are completely free.
Put the API key to your Contao settings.


## Thank you

* Marco Zinsmeister ([profimedien.net](http://www.profimedien.net))
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name":"christianbarkowsky/contao-tiny-compress-images",
"description":"This plugin automatically optimizes your images by integrating with the popular image compression services TinyJPG and TinyPNG.",
"keywords":["contao", "tinypng", "tinyjpg", "images", "compress", "upload", "tiny"],
"keywords":["contao", "tinypng", "tinyjpg", "images", "compress", "upload", "tiny", "tinywebp"],
"type":"contao-module",
"homepage": "https://brkwsky.de/contao-tinypng-tinyjpg-erweiterung",
"homepage": "https://plenta.io/contao-tinypng-tinyjpg-erweiterung",
"license":"LGPL-3.0-or-later",
"authors":[
{
"name":"Christian Barkowsky",
"homepage":"https://brkwsky.de",
"role":"Developer"
"homepage":"https://plenta.io"
}
],
"funding": [
Expand All @@ -24,9 +23,8 @@
"docs": "https://brkwsky.de/contao-tinypng-tinyjpg-erweiterung"
},
"require":{
"php":">=5.5",
"contao/core-bundle": "~3.5 || ~4.4",
"contao-community-alliance/composer-plugin": "~2.4 || ~3.0"
"php": "^7.4 || ^8.0",
"contao/core-bundle": "^4.9"
},
"extra": {
"contao": {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/public/compress.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Resources/public/compressed.svg
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 src/Resources/public/tinyfy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 84 additions & 25 deletions src/classes/TinyCompressImages.php
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';
Expand All @@ -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 '';
}
}
1 change: 0 additions & 1 deletion src/config/autoload.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
;;
requires[] = "core"


;;
; Configure what you want the autoload creator to register
;;
Expand Down
12 changes: 4 additions & 8 deletions src/config/autoload.php
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
*/
Expand Down
16 changes: 5 additions & 11 deletions src/config/config.php
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'];
20 changes: 20 additions & 0 deletions src/dca/tl_files.php
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 ''"
];
37 changes: 16 additions & 21 deletions src/dca/tl_settings.php
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')
;
21 changes: 21 additions & 0 deletions src/languages/de/default.xlf
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>


13 changes: 13 additions & 0 deletions src/languages/de/tl_files.xlf
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>


Loading

0 comments on commit eb8e8c1

Please sign in to comment.