Skip to content

Commit

Permalink
Merge pull request #13 from daniellienert/feature/allow-setting-of-fo…
Browse files Browse the repository at this point in the history
…rmats

!!! FEATURE: Add `format` option for imageSources `setFormat()` for helpers and prototype `Sitegeist.Kaleidoscope:Source` 

Supported formats are amongst other: `webp`, `png`, `jpg`, `jppeg`
Note: The format has to be suported by the current imagine driver.

- the `format` option in the *ImageSource prototypes allows to set the format on ImageSource Definition
- the `setFormat()` method of the *imageSourceHelpers allows to do this later to already define ImageSourceHelpers on the presentation side
- the prototype `Sitegeist.Kaleidoscope:Source`  is used inside `Sitegeist.Kaleidoscope:Picture` for the rendering of sources but it can may also be used standalone
  • Loading branch information
mficzel authored Sep 12, 2019
2 parents 3528c74 + e832208 commit f2279a6
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 151 deletions.
7 changes: 4 additions & 3 deletions Classes/Controller/DummyImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class DummyImageController extends ActionController
* @param string $bg
* @param string $fg
* @param string $t
* @param string $f
*/
public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', string $fg = '#fff', string $t = null)
public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', string $fg = '#fff', string $t = null, string $f = 'png')
{
// limit input arguments
if ($w > 9999) {
Expand Down Expand Up @@ -110,8 +111,8 @@ public function imageAction (int $w = 600, int $h = 400, string $bg = '#000', st

// build result
$this->response->setHeader( 'Cache-Control', 'max-age=883000000');
$this->response->setHeader( 'Content-type', 'image/png');
return $image->get('png');
$this->response->setHeader( 'Content-type', 'image/' . $f);
return $image->get($f);
}

/**
Expand Down
18 changes: 17 additions & 1 deletion Classes/EelHelpers/AbstractImageSourceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ abstract class AbstractImageSourceHelper implements ImageSourceHelperInterface
*/
protected $targetHeight;

/**
* @var string|null
*/
protected $targetFormat = null;

/**
* @var mixed
* @Flow\InjectConfiguration(path="thumbnailPresets", package="Neos.Media")
Expand Down Expand Up @@ -51,6 +56,17 @@ public function setHeight(int $targetHeight = null, bool $preserveAspect = false
return $newSource;
}

/**
* @param string|null $format
* @return ImageSourceHelperInterface
*/
public function setFormat(?string $format = null) : ImageSourceHelperInterface
{
$newSource = clone($this);
$newSource->targetFormat = $format;
return $newSource;
}

/**
* @param int|null $targetWidth
* @param int|null $targetHeight
Expand Down Expand Up @@ -134,7 +150,7 @@ public function srcset($mediaDescriptors): string
*/
public function allowsCallOfMethod($methodName)
{
if (in_array($methodName, ['setWidth', 'setHeight', 'setDimensions', 'applyPreset', 'src', 'srcset'])) {
if (in_array($methodName, ['setWidth', 'setHeight', 'setDimensions', 'setFormat', 'applyPreset', 'src', 'srcset'])) {
return true;
} else {
return false;
Expand Down
5 changes: 3 additions & 2 deletions Classes/EelHelpers/AssetImageSourceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ public function src(): string
$async = $this->request ? $this->async : false;
$allowCropping = ($this->targetWidth && $this->targetHeight);
$allowUpScaling = false;

$thumbnailConfiguration = new ThumbnailConfiguration(
$this->targetWidth,
$this->targetWidth,
$this->targetHeight,
$this->targetHeight,
$allowCropping,
$allowUpScaling,
$async
$async,
null,
$this->targetFormat
);

$thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset(
Expand Down
31 changes: 22 additions & 9 deletions Classes/EelHelpers/DummyImageSourceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,28 @@ public function setText($text): void
*/
public function src(): string
{
$uri = $this->baseUri . '?' . http_build_query (
[
'w' => $this->getCurrentWidth(),
'h' => $this->getCurrentHeight(),
'bg' => ($this->backgroundColor ?: '000'),
'fg' => ($this->foregroundColor ?: 'fff'),
't' => (trim($this->text ?: $this->getCurrentWidth() . ' x ' . $this->getCurrentHeight()))
]
);
$arguments = [
'w' => $this->getCurrentWidth(),
'h' => $this->getCurrentHeight()
];

if ($this->backgroundColor) {
$arguments['bg'] = $this->backgroundColor;
}

if ($this->foregroundColor) {
$arguments['fg'] = $this->foregroundColor;
}

if ($this->text) {
$arguments['t'] = $this->text;
}

if ($this->targetFormat) {
$arguments['f'] = $this->targetFormat;
}

$uri = $this->baseUri . '?' . http_build_query ($arguments);
return $uri;
}
}
12 changes: 12 additions & 0 deletions Classes/FusionObjects/AbstractImageSourceImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function getHeight()
return $this->fusionValue('height');
}

/**
* @return string|null
*/
public function getFormat(): ?string
{
return $this->fusionValue('format');
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -55,6 +63,10 @@ public function evaluate() : ?ImageSourceHelperInterface
$helper = $helper->setHeight($height);
}

if ($format = $this->getFormat()) {
$helper = $helper->setFormat($format);
}

return $helper;
}

Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ Render a `picture`-tag with various sources.
Props:
- `imageSource`: the imageSource to render
- `sources`: an array of source definitions that supports the following keys
- `media`: the media query of this source
- `imageSource`: alternate image-source for art direction purpose
- `srcset`: media descriptors like '1.5x' or '600w' (string ot array)
- `sizes`: sizes attribute (string ot array)
- `media`: the media attribute for this source
- `type`: the type attribute for this source
- `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)
- `alt`: alt-attribute for the picture tag
Expand All @@ -139,6 +140,13 @@ sources = Neos.Fusion:RawArray {
media = 'screen and (max-width: 1599px)'
}
webp = Neos.Fusion:RawArray {
imageSource = {imageSource.setFormat('webp')}
srcset = '320w, 480w, 800w'
sizes = '(max-width: 320px) 280px, (max-width: 480px) 440px, 100vw'
type = 'image/webp'
}
print = Neos.Fusion:RawArray {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource {
text = "im am here for printing"
Expand Down Expand Up @@ -172,6 +180,17 @@ will render as:
<img src="_base_url_">
</picture>
```
### `Sitegeist.Kaleidoscope:Source`

Render an `src`-tag with `srcset`, `sizes`, `type` and `media` attributes.

Props:

- `imageSource`: the imageSource to render
- `srcset`: media descriptors like '1.5x' or '600w' of the default image (string ot array)
- `sizes`: (optional) sizes attribute (string or array)
- `type`: (optional) the type attribute for the source like `image/png` or `image/webp`, the actual format is enforced via `imageSource.setFormat()`
- `media`: (optional) the media query for the given source

## Responsive Images with AtomicFusion-Components and Sitegeist.Monocle

Expand Down Expand Up @@ -232,6 +251,7 @@ All ImageSources support the following fusion properties:
- `preset`: Set width and/or height via named-preset from Settings `Neos.Media.thumbnailPresets` (default null, settings below override the preset)
- `width`: Set the intended width (default null)
- `height`: Set the intended height (default null)
- `format`: Set the image output format, like webp (default null)

### `Sitegeist.Kaleidoscope:AssetImageSource`

Expand Down Expand Up @@ -277,6 +297,7 @@ Methods of ImageSource-Helpers that are accessible via EEL:
- `setWidth( integer $width, bool $preserveAspect = false )`: Set the intend width modify height aswell if
- `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`
- `src ()` : Render a src attribute for the given ImageSource-object
- `srcset ( array of descriptors )` : render a srcset attribute for the ImageSource with given media descriptors like `2.x` or `800w`

Expand Down
13 changes: 7 additions & 6 deletions Resources/Private/Fusion/Prototypes/AssetImageSource.fusion
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
prototype(Sitegeist.Kaleidoscope:AssetImageSource) {
@class = 'Sitegeist\\Kaleidoscope\\FusionObjects\\AssetImageSourceImplementation'
asset = ${asset}
async = true
@class = 'Sitegeist\\Kaleidoscope\\FusionObjects\\AssetImageSourceImplementation'
asset = ${asset}
async = true

preset = null
width = null
height = null
preset = null
width = null
height = null
format = null
}
19 changes: 10 additions & 9 deletions Resources/Private/Fusion/Prototypes/DummyImageSource.fusion
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
prototype(Sitegeist.Kaleidoscope:DummyImageSource) {
@class = 'Sitegeist\\Kaleidoscope\\FusionObjects\\DummyImageSourceImplementation'
@class = 'Sitegeist\\Kaleidoscope\\FusionObjects\\DummyImageSourceImplementation'

baseWidth = 600
baseHeight = 400
backgroundColor = '999'
foregroundColor = 'fff'
text = null
baseWidth = 600
baseHeight = 400
backgroundColor = '999'
foregroundColor = 'fff'
text = null

preset = null
width = null
height = null
preset = null
width = null
height = null
format = null
}

// this is only for testing of a large variant og dummy images
Expand Down
118 changes: 59 additions & 59 deletions Resources/Private/Fusion/Prototypes/Image.fusion
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
prototype(Sitegeist.Kaleidoscope:Image) < prototype(Neos.Fusion:Component) {

@styleguide {
props {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource
alt = 'Elva dressed as a fairy'
}

propSets {
multires_array {
srcset = ${['1x', '1.5x', '2x']}
}

multires_string {
srcset = '1x, 1.5x, 2x'
}

multisize_array {
srcset = ${['320w', '400w', '600w', '800w', '1000w', '1200w', '1600']}
}

multisize_string {
srcset = '320w, 400ww, 600w, 800w, 1000w, 1200w, 1600'
}

sizes_array {
srcset = ${['320w', '400w', '600w', '800w', '1000w', '1200w', '1600']}
sizes = ${['(max-width: 320px) 280px', '(max-width: 480px) 440px', '800px']}
}

sizes_string {
srcset = '320w, 400ww, 600w, 800w, 1000w, 1200w, 1600'
sizes = '(max-width: 320px) 280px, (max-width: 480px) 440px, 800px'
}
}
}

@propTypes {
imageSource = ${PropTypes.instanceOf('\\Sitegeist\\Kaleidoscope\\EelHelpers\\ImageSourceHelperInterface')}
}

imageSource = null
srcset = null
sizes = null
alt = null
title = null
class = null

renderer = afx`
<img @if.has={props.imageSource}
src={props.imageSource}
srcset={props.imageSource.srcset(props.srcset)}
[email protected]={props.srcset}
sizes={props.sizes}
[email protected]={Type.isArray(value) ? Array.join(value, ', ') : value}
class={props.class}
alt={props.alt}
title={props.title}
/>
`
}
@styleguide {
props {
imageSource = Sitegeist.Kaleidoscope:DummyImageSource
alt = 'Elva dressed as a fairy'
}

propSets {
multires_array {
srcset = ${['1x', '1.5x', '2x']}
}

multires_string {
srcset = '1x, 1.5x, 2x'
}

multisize_array {
srcset = ${['320w', '400w', '600w', '800w', '1000w', '1200w', '1600']}
}

multisize_string {
srcset = '320w, 400ww, 600w, 800w, 1000w, 1200w, 1600'
}

sizes_array {
srcset = ${['320w', '400w', '600w', '800w', '1000w', '1200w', '1600']}
sizes = ${['(max-width: 320px) 280px', '(max-width: 480px) 440px', '800px']}
}

sizes_string {
srcset = '320w, 400ww, 600w, 800w, 1000w, 1200w, 1600'
sizes = '(max-width: 320px) 280px, (max-width: 480px) 440px, 800px'
}
}
}

@propTypes {
imageSource = ${PropTypes.instanceOf('\\Sitegeist\\Kaleidoscope\\EelHelpers\\ImageSourceHelperInterface')}
}

imageSource = null
srcset = null
sizes = null
alt = null
title = null
class = null

renderer = afx`
<img @if.has={props.imageSource}
src={props.imageSource}
srcset={props.imageSource.srcset(props.srcset)}
[email protected]={props.srcset}
sizes={props.sizes}
[email protected]={Type.isArray(value) ? Array.join(value, ', ') : value}
class={props.class}
alt={props.alt}
title={props.title}
/>
`
}
Loading

0 comments on commit f2279a6

Please sign in to comment.