Proper treatment of cropping instructions

This commit is contained in:
Björn Fromme
2018-09-22 12:00:29 +02:00
parent 501df008bf
commit a470fda80f
@@ -27,6 +27,8 @@ namespace EP\EpProducts\Traits;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Extbase\Service\ImageService;
@@ -63,20 +65,20 @@ trait ImageServiceTrait
*/
public function getImages($uid)
{
if (!isset(self::$cache[$uid])) {
if (!isset(static::$cache[$uid])) {
$original = $this->fileRepository->findByRelation(
$this->table,
$this->field,
$uid
);
$resized = $this->generateResizedImages($original);
self::$cache[$uid] = [
static::$cache[$uid] = [
'original' => $original,
'resized' => $resized,
];
}
return self::$cache[$uid];
return static::$cache[$uid];
}
/**
@@ -90,7 +92,7 @@ trait ImageServiceTrait
{
foreach ($images as $image)
{
$intructions['crop'] = $image->getProperty('crop');
$instructions['crop'] = $this->getCroppingInfo($image, $instructions);
$processedImage = $this->imageService->applyProcessingInstructions($image, $instructions);
$resized[$version][] = [
'url' => $this->imageService->getImageUri($processedImage, true),
@@ -102,4 +104,17 @@ trait ImageServiceTrait
return $resized;
}
/**
* @param FileReference $image
* @param array $processingInstructions
* @return null|\TYPO3\CMS\Core\Imaging\ImageManipulation\Area
*/
protected function getCroppingInfo(FileReference $image, array $processingInstructions)
{
$cropString = $image->getProperty('crop');
$cropVariantCollection = CropVariantCollection::create((string) $cropString);
$cropVariant = $processingInstructions['cropVariant'] ?: 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
return $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image);
}
}