Skip to content

Commit

Permalink
Merge pull request from GHSA-277c-5vvj-9pwx
Browse files Browse the repository at this point in the history
* Introduce allowed formats and max scale factor

* Add documentation, upgrade notes and use floatNode for max_scale_factor

* Update to max_scaling_factor

* Cast format to lower

* Fix typo
  • Loading branch information
mattamon committed May 3, 2024
1 parent 5dc4513 commit a6821a1
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 3 deletions.
12 changes: 12 additions & 0 deletions bundles/CoreBundle/src/DependencyInjection/Configuration.php
Expand Up @@ -352,6 +352,18 @@ private function addAssetNode(ArrayNodeDefinition $rootNode): void
->arrayNode('assets')
->addDefaultsIfNotSet()
->children()
->arrayNode('thumbnails')
->addDefaultsIfNotSet()
->children()
->arrayNode('allowed_formats')
->defaultValue(['avif', 'eps', 'gif', 'jpeg', 'jpg', 'pjpeg', 'png', 'svg', 'tiff', 'webm', 'webp'])
->scalarPrototype()->end()
->end()
->floatNode('max_scaling_factor')
->defaultValue(5.0)
->end()
->end()
->end()
->arrayNode('frontend_prefixes')
->addDefaultsIfNotSet()
->children()
Expand Down
12 changes: 11 additions & 1 deletion doc/04_Assets/03_Working_with_Thumbnails/01_Image_Thumbnails.md
Expand Up @@ -9,7 +9,8 @@ which are not stored as an asset inside Pimcore.
> **IMPORTANT**
> Use Imagick PECL extension for best results, GDlib is just a fallback with limited functionality
> (only PNG, JPG, GIF) and less quality!
> Using ImageMagick Pimcore supports hundreds of formats including: AI, EPS, TIFF, PNG, JPG, GIF, PSD, ...
> Using ImageMagick Pimcore can support hundreds of formats including: AI, EPS, TIFF, PNG, JPG, GIF, PSD, etc.
> Not all formats are allowed out of the box. To extend the list [see](./README.md#allowed-formats).
To use the thumbnailing service of Pimcore, you have to create a transformation pipeline first. To do so, open
_Settings_ > _Thumbnails_ and click on _Add Thumbnail_ to create a new configuration.
Expand Down Expand Up @@ -329,6 +330,15 @@ This is a special functionality to allow embedding high resolution (ppi/dpi) ima
The following is only necessary in special use-cases like Web-to-Print, in typical web-based cases, Pimcore
automatically adds the `srcset` attribute to `<img>` and `<picture>` tags automatically, so no manual work is necessary.

The high resolution scaling factor is limited to `5.0` eg. `@5x`. Float values are supported.
If you need to scale an image more than that, you can use the `max_scaling_factor` option in the configuration.
```yaml
pimcore:
assets:
thumbnails:
max_scaling_factor: 6.0
```

### Use in the Thumbnail Configuration:

![High Resolution](../../img/thumbnail_high_resolution.png)
Expand Down
27 changes: 26 additions & 1 deletion doc/04_Assets/03_Working_with_Thumbnails/README.md
Expand Up @@ -4,7 +4,32 @@ Pimcore provides a sophisticated thumbnail processing engine for calculating thu
different output channels Pimcore can calculate and provide optimized images in terms of dimensions, file sizes, formats
and much more.

This functionality allows true single source publishing with Pimcore.
This functionality allows true single source publishing with Pimcore.

### Allowed formats
Pimcore allows the following formats for thumbnails out of the box:
`'avif', 'eps', 'gif', 'jpeg', 'jpg', 'pjpeg', 'png', 'svg', 'tiff', 'webm', 'webp'`.

If you want to use a different format, you can easily extend the list of supported formats.
Keep in mind that you must copy the whole list of formats and add your desired format to it.
```yaml
pimcore:
assets:
thumbnails:
allowed_formats:
- 'avif'
- 'eps'
- 'gif'
- 'jpeg'
- 'jpg'
- 'pjpeg'
- 'png'
- 'svg'
- 'tiff'
- 'webm'
- 'webp'
- 'pdf' # Add your desired format here
```

##### Thumbnails are available for following file types:
* [Image Thumbnails](./01_Image_Thumbnails.md)
Expand Down
30 changes: 30 additions & 0 deletions doc/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md
@@ -1,5 +1,35 @@
# Upgrade Notes

## Pimcore 11.2.4
### Assets Thumbnails
- Thumbnail generation for Assets, Documents and Videos now only support the following formats out of the box: `'avif', 'eps', 'gif', 'jpeg', 'jpg', 'pjpeg', 'png', 'svg', 'tiff', 'webm', 'webp'`.
- You can extend this list by adding your formats on the bottom:
```yaml
pimcore:
assets:
thumbnails:
allowed_formats:
- 'avif'
- 'eps'
- 'gif'
- 'jpeg'
- 'jpg'
- 'pjpeg'
- 'png'
- 'svg'
- 'tiff'
- 'webm'
- 'webp'
- 'pdf' # Add your desired format here
```
- High resolution scaling factor for image thumbnails has now been limited to a maximum of `5.0`. If you need to scale an image more than that, you can use the `max_scaling_factor` option in the configuration.
```yaml
pimcore:
assets:
thumbnails:
max_scaling_factor: 6.0
```

## Pimcore 11.2.0
### Elements
#### [Documents]:
Expand Down
8 changes: 8 additions & 0 deletions models/Asset/Document/ImageThumbnail.php
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Model;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Exception\NotFoundException;
use Pimcore\Model\Exception\ThumbnailFormatNotSupportedException;
use Pimcore\Tool\Storage;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Lock\LockFactory;
Expand Down Expand Up @@ -70,8 +71,15 @@ public function getPath(array $args = []): string
return $path;
}

/**
* @throws ThumbnailFormatNotSupportedException
*/
public function generate(bool $deferredAllowed = true): void
{
if (!$this->checkAllowedFormats($this->config->getFormat())) {
throw new ThumbnailFormatNotSupportedException();
}

$deferred = $deferredAllowed && $this->deferred;
$generated = false;

Expand Down
12 changes: 12 additions & 0 deletions models/Asset/Image/Thumbnail.php
Expand Up @@ -24,6 +24,8 @@
use Pimcore\Model\Asset\Image\Thumbnail\Config;
use Pimcore\Model\Asset\Thumbnail\ImageThumbnailTrait;
use Pimcore\Model\Exception\NotFoundException;
use Pimcore\Model\Exception\ThumbnailFormatNotSupportedException;
use Pimcore\Model\Exception\ThumbnailMaxScalingFactorException;
use Pimcore\Tool;
use Symfony\Component\EventDispatcher\GenericEvent;

Expand Down Expand Up @@ -103,10 +105,20 @@ protected function useOriginalFile(string $filename): bool
}

/**
* @throws ThumbnailFormatNotSupportedException
* @throws ThumbnailMaxScalingFactorException
* @internal
*/
public function generate(bool $deferredAllowed = true): void
{
if (!$this->checkAllowedFormats($this->config->getFormat(), $this->asset)) {
throw new ThumbnailFormatNotSupportedException();
}

if (!$this->checkMaxScalingFactor($this->config->getHighResolution())) {
throw new ThumbnailMaxScalingFactorException();
}

$deferred = false;
$generated = false;

Expand Down
25 changes: 25 additions & 0 deletions models/Asset/Thumbnail/ImageThumbnailTrait.php
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Model\Asset\Thumbnail;

use Pimcore\Config as PimcoreConfig;
use Pimcore\Helper\TemporaryFileHelperTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image;
Expand Down Expand Up @@ -428,4 +429,28 @@ public function getAsFormat(string $format): static

return $thumb;
}

private function checkAllowedFormats(string $format, ?Asset $asset = null): bool
{
$format = strtolower($format);
if($asset) {
$original = pathinfo($asset->getRealFullPath(), PATHINFO_EXTENSION);
if ($format === $original || $format === 'source') {
return true;
}
}

$assetConfig = PimcoreConfig::getSystemConfiguration('assets');
return in_array(
$format,
$assetConfig['thumbnails']['allowed_formats'],
true
);
}

private function checkMaxScalingFactor(float $scalingFactor): bool
{
$assetConfig = PimcoreConfig::getSystemConfiguration('assets');
return $scalingFactor <= $assetConfig['thumbnails']['max_scaling_factor'];
}
}
8 changes: 7 additions & 1 deletion models/Asset/Video/ImageThumbnail.php
Expand Up @@ -16,12 +16,14 @@

namespace Pimcore\Model\Asset\Video;

use Exception;
use Pimcore\Event\AssetEvents;
use Pimcore\Event\FrontendEvents;
use Pimcore\File;
use Pimcore\Logger;
use Pimcore\Model;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Exception\ThumbnailFormatNotSupportedException;
use Pimcore\Tool\Storage;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Lock\LockFactory;
Expand Down Expand Up @@ -75,12 +77,16 @@ public function getPath(array $args = []): string
}

/**
* @throws \Exception
* @throws Exception
*
* @internal
*/
public function generate(bool $deferredAllowed = true): void
{
if (!$this->checkAllowedFormats($this->config->getFormat())) {
throw new ThumbnailFormatNotSupportedException();
}

$deferred = $deferredAllowed && $this->deferred;
$generated = false;

Expand Down
23 changes: 23 additions & 0 deletions models/Exception/ThumbnailFormatNotSupportedException.php
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Model\Exception;

use Exception;

final class ThumbnailFormatNotSupportedException extends Exception
{
}
23 changes: 23 additions & 0 deletions models/Exception/ThumbnailMaxScalingFactorException.php
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Model\Exception;

use Exception;

final class ThumbnailMaxScalingFactorException extends Exception
{
}

0 comments on commit a6821a1

Please sign in to comment.