Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jb 1236 prescale #40

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions Classes/Services/CloudinaryImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Cloudinary\Asset\Image;
use Cloudinary\Transformation\ImageTransformation;
use Cloudinary\Transformation\Scale;
use Exception;
use TYPO3\CMS\Core\Resource\StorageRepository;
use TYPO3\CMS\Core\Resource\File;
Expand All @@ -20,6 +21,9 @@

class CloudinaryImageService extends AbstractCloudinaryMediaService
{
// See "Max image megapixels" on https://cloudinary.com/pricing/compare-plans
const TRANSFORMATION_MAX_INPUT_PIXELS = 50_000_000;

protected ExplicitDataCacheRepository $explicitDataCacheRepository;

protected ?StorageRepository $storageRepository = null;
Expand All @@ -46,11 +50,13 @@ public function getExplicitData(File $file, array $options): array

// With Cloudinary API 2, we need to modify the way in which "responsive_breakpoints.transformation" are transmitted.
$apiOptions = $options;
if (isset($options['responsive_breakpoints']['transformation'])) {
$apiOptions['responsive_breakpoints']['transformation'] = []; // reset the value
foreach ($options['responsive_breakpoints']['transformation'] as $transformationParams) {
$apiOptions['responsive_breakpoints']['transformation'][] = ImageTransformation::fromParams($transformationParams);
}
if (isset($apiOptions['responsive_breakpoints']['transformation'])) {
// Check if we need to scale the image down, before applying image transformations
$prescaleTransformation = $this->getPrescaleTransformation($file);
$apiOptions['responsive_breakpoints']['transformation'] = array_map(
fn(array $parameters) => (new ImageTransformation($prescaleTransformation))->addActionFromQualifiers($parameters),
$apiOptions['responsive_breakpoints']['transformation'],
);
}

try {
Expand Down Expand Up @@ -221,4 +227,22 @@ public function injectStorageRepository(StorageRepository $storageRepository): v
$this->storageRepository = $storageRepository;
}

/**
* Check if cloudinary needs to scale down the image before applying
* transformations. This function will return the required scaling
* transformation or null if no scaling is required.
*/
protected function getPrescaleTransformation(File $file): ?Scale
{
$width = $file->getProperty('width') ?? 0;
$height = $file->getProperty('height') ?? 0;

if ($width * $height <= self::TRANSFORMATION_MAX_INPUT_PIXELS) {
return null;
}

// Calculate a width that allows the image to be processed
$maxWidth = (int)floor(sqrt(self::TRANSFORMATION_MAX_INPUT_PIXELS / ($height / $width)));
return Scale::limitFit($maxWidth);
}
}
Loading