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: Add quality option #75

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions Classes/Domain/AbstractImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ abstract class AbstractImageSource implements ImageSourceInterface, ProtectedCon
*/
protected $targetHeight;

/**
* @var int|null
*/
protected $targetQuality;

/**
* @var string|null
*/
Expand Down Expand Up @@ -105,6 +110,22 @@ public function withHeight(int $targetHeight, bool $preserveAspect = false): Ima
return $newSource;
}

/**
* @deprecated
*/
public function setQuality(int $quality): ImageSourceInterface
{
return $this->withQuality($quality);
}

public function withQuality(int $quality): ImageSourceInterface
{
$newSource = clone $this;
$newSource->targetQuality = $quality;

return $newSource;
}

/**
* @deprecated
*/
Expand Down Expand Up @@ -302,6 +323,7 @@ public function allowsCallOfMethod($methodName)
'withTitle',
'withDimensions',
'withFormat',
'withQuality',
'withWidth',
'withHeight',
'withThumbnailPreset',
Expand All @@ -311,6 +333,7 @@ public function allowsCallOfMethod($methodName)
'setHeight',
'setDimensions',
'setFormat',
'setQuality',
'applyPreset',
'applyThumbnailPreset',
'useVariantPreset',
Expand Down
4 changes: 2 additions & 2 deletions Classes/Domain/AssetImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function src(): string
$allowCropping,
$allowUpScaling,
$async,
null,
$this->targetQuality,
$this->targetFormat
);

Expand Down Expand Up @@ -177,7 +177,7 @@ public function dataSrc(): string
$allowCropping,
$allowUpScaling,
$async,
null,
$this->targetQuality,
$this->targetFormat
);

Expand Down
2 changes: 2 additions & 0 deletions Classes/Domain/ImageSourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function withWidth(int $targetWidth, bool $preserveAspect = false): Image

public function withHeight(int $targetHeight, bool $preserveAspect = false): ImageSourceInterface;

public function withQuality(int $targetQuality): ImageSourceInterface;

public function withThumbnailPreset(string $format): ImageSourceInterface;

public function withVariantPreset(string $presetIdentifier, string $presetVariantName): ImageSourceInterface;
Expand Down
5 changes: 5 additions & 0 deletions Classes/EelHelpers/ImageSourceHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function setHeight(int $height, bool $preserveAspect = false): ImageSourc
*/
public function setDimensions(int $width, int $height): ImageSourceInterface;

/**
* @deprecated use withQuality
*/
public function setQuality(int $quality): ImageSourceInterface;

/**
* @deprecated use withFormat
*/
Expand Down
12 changes: 12 additions & 0 deletions Classes/FusionObjects/AbstractImageSourceImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public function getHeight(): ?int
return $this->fusionValue('height');
}

/**
* @return int|null
*/
public function getQuality(): ?int
{
return $this->fusionValue('quality');
}

/**
* @return string|null
*/
Expand Down Expand Up @@ -94,6 +102,10 @@ public function evaluate(): ?ImageSourceInterface
$helper = $helper->withHeight($height);
}

if ($quality = $this->getQuality()) {
$helper = $helper->withQuality($quality);
}

if ($format = $this->getFormat()) {
$helper = $helper->withFormat($format);
}
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Props:
- `sizes`: sizes attribute of the default image (string ot array)
- `loading`: (optional, default "lazy") loading attribute for the img tag
- `format`: (optional) the image-format like `webp` or `png`, will be applied to the `imageSource`
- `quality`: (optional) the image quality from 0 to 100, will be applied to the `imageSource`
- `width`: (optional) the base width, will be applied to the `imageSource`
- `height`: (optional) the base height, will be applied to the `imageSource`
- `alt`: alt-attribute for the img tag (default "")
Expand Down Expand Up @@ -136,11 +137,13 @@ Props:
- `media`: (optional) the media attribute for this source
- `type`: (optional) the type attribute for this source
- `format`: (optional) the image-format for the source like `webp` or `png`, is applied to `imageSource` and `type`
- `quality`: (optional) the image quality from 0 to 100, will be applied to the `imageSource`
- `width`: (optional) the base width, will be applied to the `imageSource`
- `height`: (optional) the base height, will be applied to the `imageSource`
- `srcset`: media descriptors like '1.5x' or '600w' of the default image (string ot array)
- `sizes`: sizes attribute of the default image (string ot array)
- `formats`: (optional) image formats that will be rendered as sources of separate type (string or array)
- `quality`: (optional) the image quality from 0 to 100, will be applied to the `imageSource`
- `width`: (optional) the base width, will be applied to the `imageSource`
- `height`: (optional) the base height, will be applied to the `imageSource`
- `loading`: (optional, default "lazy") loading attribute for the img tag
Expand Down Expand Up @@ -212,6 +215,7 @@ Props:
- `srcset`: media descriptors like '1.5x' or '600w' of the default image (string ot array, inherited from picture)
- `sizes`: (optional) sizes attribute (string or array, inherited from picture)
- `format`: (optional) the image-format like `webp` or `png`, will be applied to `imageSource` and `type`
- `quality`: (optional) the image quality from 0 to 100, will be applied to the `imageSource`
- `width`: (optional) the base width, will be applied to the `imageSource`
- `height`: (optional) the base height, will be applied to the `imageSource`
- `type`: (optional) the type attribute for the source like `image/png` or `image/webp`, the actual format is enforced via `imageSource.withFormat()`
Expand Down Expand Up @@ -307,6 +311,7 @@ Arguments:
- `thumbnailPreset`: `width` and `height` are supported as explained above
- `variantPreset`: as explained above
- `format`: Set the image output format, like webp (default null)
- `quality`: Set the image quality from 0 to 100 (default null)
- `alt`: The alt attribute if not specified otherwise (default null)
- `title`: The title attribute if not specified otherwise (default null)

Expand Down Expand Up @@ -355,6 +360,7 @@ Methods of ImageSource-Helpers that are accessible via Eel:
- `withThumbnailPreset( string )`: Set width and/or height via named thumbnail preset from Settings `Neos.Media.thumbnailPresets`
- `withVariantPreset( string, string )`: Select image variant via the named variant preset (parameters are "preset identifier" key and "preset variant name" key from Settings `Neos.Media.variantPresets`)
- `withFormat( string )`: Set the image format to generate like `webp`, `png` or `jpeg`
- `withQuality( integer )`: Set the image quality from 0 to 100
- `withAlt( ?string )`: Set the alt atttribute for the image tag
- `withTitle( ?string )`: Set the title atttribute for the image tag

Expand All @@ -373,6 +379,7 @@ deprecated methods:
- `setHeight( integer $height, bool $preserveAspect = false )`: Set the intended height
- `setDimensions( integer, interger)`: Set the intended width and height
- `setFormat( string )`: Set the image format to generate like `webp`, `png` or `jpeg`
- `setQuality( integer )`: Set the image quality from 0 to 100

Note: The Eel-helpers cannot be created directly. They have to be created
by using the `Sitegeist.Kaleidoscope:AssetImageSource` or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ prototype(Sitegeist.Kaleidoscope:AssetImageSource) {
width = null
height = null
format = null
quality = null
}
2 changes: 2 additions & 0 deletions Resources/Private/Fusion/Prototypes/Image.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ prototype(Sitegeist.Kaleidoscope:Image) < prototype(Neos.Fusion:Component) {
width = null
height = null
format = null
quality = null
attributes = Neos.Fusion:DataStructure
renderDimensionAttributes = true

Expand All @@ -136,6 +137,7 @@ prototype(Sitegeist.Kaleidoscope:Image) < prototype(Neos.Fusion:Component) {
[email protected] = ${props.width ? value.withWidth(props.width) : value}
[email protected] = ${props.height ? value.withHeight(props.height) : value}
[email protected] = ${props.format ? value.withFormat(props.format) : value}
[email protected] = ${props.quality ? value.withQuality(props.quality) : value}

srcset = ${props.srcset}
sizes = ${props.sizes}
Expand Down
4 changes: 4 additions & 0 deletions Resources/Private/Fusion/Prototypes/Picture.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ prototype(Sitegeist.Kaleidoscope:Picture) < prototype(Neos.Fusion:Component) {
loading = 'lazy'
sources = null
formats = null
quality = null
width = null
height = null
alt = ''
Expand All @@ -76,6 +77,7 @@ prototype(Sitegeist.Kaleidoscope:Picture) < prototype(Neos.Fusion:Component) {
__width = ${this.width}
__height = ${this.height}
__format = ${this.format}
__quality = ${this.quality}
}

renderer = Neos.Fusion:Component {
Expand All @@ -86,6 +88,7 @@ prototype(Sitegeist.Kaleidoscope:Picture) < prototype(Neos.Fusion:Component) {
[email protected] = ${props.width ? value.setWidth(props.width) : value}
[email protected] = ${props.height ? value.setHeight(props.height) : value}
[email protected] = ${props.format ? value.setFormat(props.format) : value}
[email protected] = ${props.quality ? value.setQuality(props.quality) : value}

srcset = ${props.srcset}
sizes = ${props.sizes}
Expand All @@ -111,6 +114,7 @@ prototype(Sitegeist.Kaleidoscope:Picture) < prototype(Neos.Fusion:Component) {
format={source.format}
width={source.width}
height={source.height}
quality={source.quality}
srcset={source.srcset ? source.srcset : props.srcset}
sizes={source.sizes ? source.sizes : props.sizes}
renderDimensionAttributes={props.renderDimensionAttributes}
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Fusion/Prototypes/Source.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ prototype(Sitegeist.Kaleidoscope:Source) < prototype(Neos.Fusion:Component) {
width = null
height = null
format = null
quality = null
type = null
media = null
renderDimensionAttributes = true
Expand All @@ -19,6 +20,7 @@ prototype(Sitegeist.Kaleidoscope:Source) < prototype(Neos.Fusion:Component) {
@context {
imageSource = ${props.imageSource || __imageSource}
format = ${props.format || __format}
quality = ${props.quality || __quality}
width = ${props.width || __width}
height = ${props.height || __height}
srcset = ${props.srcset || __srcset}
Expand All @@ -32,6 +34,7 @@ prototype(Sitegeist.Kaleidoscope:Source) < prototype(Neos.Fusion:Component) {
[email protected] = ${width ? value.withWidth(width) : value}
[email protected] = ${height ? value.withHeight(height) : value}
[email protected] = ${format ? value.withFormat(format) : value}
[email protected] = ${quality ? value.withQuality(quality) : value}

type = ${format ? 'image/' + format : props.type}
srcset = ${srcset}
Expand Down
Loading