Skip to content

Commit

Permalink
Replace $allowUpScaling with supportsUpscaling()
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmeister committed Jun 3, 2024
1 parent 19e2256 commit a54b57c
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
3 changes: 1 addition & 2 deletions Classes/Domain/AbstractImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,10 @@ public function withVariantPreset(string $presetIdentifier, string $presetVarian
* Render sourceset Attribute non-scalable media.
*
* @param mixed $mediaDescriptors
* @param bool $allowUpScaling
*
* @return string
*/
public function srcset($mediaDescriptors, bool $allowUpScaling = false): string
public function srcset($mediaDescriptors): string
{
return $this->src();
}
Expand Down
22 changes: 7 additions & 15 deletions Classes/Domain/AbstractScalableImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ abstract class AbstractScalableImageSource extends AbstractImageSource implement
*/
protected $baseHeight;

/**
* @var bool
*/
protected $allowUpScaling = false;

/**
* @param int|null $targetWidth
* @param bool $preserveAspect
Expand Down Expand Up @@ -103,14 +98,12 @@ public function withDimensions(int $targetWidth, int $targetHeight): ScalableIma

/**
* @param float $factor
* @param bool $allowUpScaling
*
* @return ScalableImageSourceInterface
*/
public function scale(float $factor, bool $allowUpScaling = false): ScalableImageSourceInterface
public function scale(float $factor): ScalableImageSourceInterface
{
$scaledHelper = clone $this;
$scaledHelper->allowUpScaling = $allowUpScaling;

if ($this->targetWidth && $this->targetHeight) {
$scaledHelper = $scaledHelper->withDimensions((int) round($factor * $this->targetWidth), (int) round($factor * $this->targetHeight));
Expand Down Expand Up @@ -246,11 +239,10 @@ protected function createAdjustment(Adjustment $adjustmentConfiguration): ImageA
* use the base width.
*
* @param $mediaDescriptors
* @param bool $allowUpScaling
*
* @return string
*/
public function srcset($mediaDescriptors, bool $allowUpScaling = false): string
public function srcset($mediaDescriptors): string
{
$srcsetArray = [];

Expand Down Expand Up @@ -281,24 +273,24 @@ public function srcset($mediaDescriptors, bool $allowUpScaling = false): string
if ($srcsetType === 'width') {
$width = (int)$matches['width'];
$scaleFactor = $width / $this->width();
if (!$allowUpScaling && ($width / $this->baseWidth > 1)) {
if (!$this->supportsUpscaling() && ($width / $this->baseWidth > 1)) {
$srcsetArray[] = $this->src() . ' ' . $this->baseWidth . 'w';
} else {
$scaled = $this->scale($scaleFactor, $allowUpScaling);
$scaled = $this->scale($scaleFactor);
$srcsetArray[] = $scaled->src() . ' ' . $width . 'w';
}
} elseif ($srcsetType === 'factor') {
$factor = (float)$matches['factor'];
if (
!$allowUpScaling && (
!$this->supportsUpscaling() && (
($this->targetHeight && ($maxScaleFactor < $factor)) ||
($this->targetWidth && ($maxScaleFactor < $factor))
)
) {
$scaled = $this->scale($maxScaleFactor, $allowUpScaling);
$scaled = $this->scale($maxScaleFactor);
$srcsetArray[] = $scaled->src() . ' ' . $maxScaleFactor . 'x';
} else {
$scaled = $this->scale($factor, $allowUpScaling);
$scaled = $this->scale($factor);
$srcsetArray[] = $scaled->src() . ' ' . $factor . 'x';
}
}
Expand Down
9 changes: 7 additions & 2 deletions Classes/Domain/AssetImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function __construct(ImageInterface $asset, ?string $title = null, ?strin
$this->baseHeight = $this->asset->getHeight();
}

public function supportsUpscaling(): bool
{
return false;
}

/**
* Use the variant generated from the given variant preset in this image source.
*
Expand Down Expand Up @@ -128,7 +133,7 @@ public function src(): string

$async = $this->request ? $this->async : false;
$allowCropping = true;
$allowUpScaling = $this->allowUpScaling;
$allowUpScaling = $this->supportsUpscaling();
$thumbnailConfiguration = new ThumbnailConfiguration(
$width,
$width,
Expand Down Expand Up @@ -168,7 +173,7 @@ public function dataSrc(): string

$async = false;
$allowCropping = true;
$allowUpScaling = $this->allowUpScaling;
$allowUpScaling = $this->supportsUpscaling();
$thumbnailConfiguration = new ThumbnailConfiguration(
$width,
$width,
Expand Down
13 changes: 12 additions & 1 deletion Classes/Domain/DummyImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class DummyImageSource extends AbstractScalableImageSource
*/
protected $baseUri;

/**
* @var bool
*/
private $allowUpScaling;

/**
* @param string $baseUri
* @param string|null $title
Expand All @@ -45,7 +50,7 @@ class DummyImageSource extends AbstractScalableImageSource
* @param string|null $foregroundColor
* @param string|null $text
*/
public function __construct(string $baseUri, ?string $title = null, ?string $alt = null, ?int $baseWidth = null, ?int $baseHeight = null, ?string $backgroundColor = null, ?string $foregroundColor = null, ?string $text = null)
public function __construct(string $baseUri, ?string $title = null, ?string $alt = null, ?int $baseWidth = null, ?int $baseHeight = null, ?string $backgroundColor = null, ?string $foregroundColor = null, ?string $text = null, bool $allowUpScaling = true)
{
parent::__construct($title, $alt);
$this->baseUri = $baseUri;
Expand All @@ -54,6 +59,12 @@ public function __construct(string $baseUri, ?string $title = null, ?string $alt
$this->backgroundColor = $backgroundColor ?? '999';
$this->foregroundColor = $foregroundColor ?? 'fff';
$this->text = $text ?? '';
$this->allowUpScaling = $allowUpScaling;
}

public function supportsUpscaling(): bool
{
return $this->allowUpScaling;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Classes/Domain/ScalableImageSourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

interface ScalableImageSourceInterface extends ImageSourceInterface
{
public function scale(float $factor, bool $allowUpScaling = false): ImageSourceInterface;
public function supportsUpscaling(): bool;
public function scale(float $factor): ImageSourceInterface;
}
4 changes: 1 addition & 3 deletions Resources/Private/Fusion/Prototypes/Image.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ prototype(Sitegeist.Kaleidoscope:Image) < prototype(Neos.Fusion:Component) {
quality = null
attributes = Neos.Fusion:DataStructure
renderDimensionAttributes = true
allowSrcsetUpScaling = false
preserveAspect = true

renderer = Neos.Fusion:Component {
Expand All @@ -150,12 +149,11 @@ prototype(Sitegeist.Kaleidoscope:Image) < prototype(Neos.Fusion:Component) {
class = ${props.class}
attributes = ${props.attributes}
renderDimensionAttributes = ${props.renderDimensionAttributes}
allowSrcsetUpScaling = ${props.allowSrcsetUpScaling}

renderer = afx`
<img
src={props.imageSource.src()}
srcset={props.imageSource.srcset(props.srcset, props.allowSrcsetUpScaling)}
srcset={props.imageSource.srcset(props.srcset)}
[email protected]={props.isScalableSource && props.srcset}
sizes={props.sizes}
[email protected]={props.isScalableSource && props.sizes}
Expand Down
3 changes: 0 additions & 3 deletions Resources/Private/Fusion/Prototypes/Picture.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ prototype(Sitegeist.Kaleidoscope:Picture) < prototype(Neos.Fusion:Component) {
imgAttributes = Neos.Fusion:DataStructure
content = ''
renderDimensionAttributes = true
allowSrcsetUpScaling = false
preserveAspect = true

#
Expand Down Expand Up @@ -105,7 +104,6 @@ prototype(Sitegeist.Kaleidoscope:Picture) < prototype(Neos.Fusion:Component) {
imgAttributes = ${props.imgAttributes}
content = ${props.content}
renderDimensionAttributes = ${props.renderDimensionAttributes}
allowSrcsetUpscaling = ${props.allowSrcsetUpscaling}
preserveAspect = ${props.preserveAspect}

renderer = afx`
Expand All @@ -123,7 +121,6 @@ prototype(Sitegeist.Kaleidoscope:Picture) < prototype(Neos.Fusion:Component) {
srcset={source.srcset ? source.srcset : props.srcset}
sizes={source.sizes ? source.sizes : props.sizes}
renderDimensionAttributes={props.renderDimensionAttributes}
allowSrcsetUpScaling={props.allowSrcsetUpScaling}
preserveAspect={props.preserveAspect}
/>
</Neos.Fusion:Loop>
Expand Down
4 changes: 1 addition & 3 deletions Resources/Private/Fusion/Prototypes/Source.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ prototype(Sitegeist.Kaleidoscope:Source) < prototype(Neos.Fusion:Component) {
type = null
media = null
renderDimensionAttributes = true
allowSrcsetUpScaling = false
preserveAspect = true

renderer = Neos.Fusion:Component {
Expand Down Expand Up @@ -44,11 +43,10 @@ prototype(Sitegeist.Kaleidoscope:Source) < prototype(Neos.Fusion:Component) {
sizes = ${sizes}
media = ${props.media}
renderDimensionAttributes = ${props.renderDimensionAttributes}
allowSrcsetUpScaling = ${props.allowSrcsetUpScaling}

renderer = afx`
<source @if.has={props.imageSource}
srcset={props.imageSource.srcset(props.srcset, props.allowSrcsetUpScaling)}
srcset={props.imageSource.srcset(props.srcset)}
sizes={props.sizes}
[email protected]={props.sizes && props.isScalableSource}
[email protected]={Type.isArray(value) ? Array.join(value, ', ') : value}
Expand Down
8 changes: 4 additions & 4 deletions Tests/Unit/Domain/AbstractScalableImageSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public function srcsetIsGenerated()
*/
public function srcsetWithWidthAdheresToDefinition()
{
$dummy = $this->getDummyImageSource(400, 400);
$dummy = $this->getDummyImageSource(400, 400, true);
$this->assertEquals(
'https://example.com?w=200&h=200&bg=999&fg=fff&t=Test 200w, https://example.com?w=400&h=400&bg=999&fg=fff&t=Test 400w, https://example.com?w=600&h=600&bg=999&fg=fff&t=Test 600w',
$dummy->srcset('200w, 400w, 600w', true)
$dummy->srcset('200w, 400w, 600w')
);
}

Expand Down Expand Up @@ -140,9 +140,9 @@ public function srcsetShouldSkipMissingDescriptors()
);
}

protected function getDummyImageSource($width, $height)
protected function getDummyImageSource($width, $height, $allowUpScaling = false)
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', $width, $height, '999', 'fff', 'Test');
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', $width, $height, '999', 'fff', 'Test', $allowUpScaling);
$this->inject($dummy, 'logger', $this->logger);
return $dummy;
}
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"name": "sitegeist/kaleidoscope",
"license": "GPL-3.0-or-later",
"require": {
"php": "^7.4 || ^8.0",
"neos/neos": "^7.0 || ^8.0 || ^9.0 || dev-master",
"neos/fusion-afx": "^7.0 || ^8.0 || ^9.0 || dev-master",
"neos/media": "*",
Expand Down

0 comments on commit a54b57c

Please sign in to comment.