Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit e9701ea

Browse files
author
Jayden Smith
committed
Improved transform parameter handling.
1 parent 56dc3bc commit e9701ea

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## 1.0.4 - 2020-10-14
8+
### Changed
9+
- Improved transform parameter handling
10+
711
## 1.0.3 - 2020-10-14
812
### Fixed
913
- Fixed mode parameter not being respected

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tasdev-au/craft-graphql-srcset",
33
"description": "Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms.",
44
"type": "craft-plugin",
5-
"version": "1.0.3",
5+
"version": "1.0.4",
66
"keywords": [
77
"craft",
88
"cms",

src/gql/arguments/SrcSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static function getArguments(): array
4141
'mode' => [
4242
'name' => 'mode',
4343
'type' => Type::string(),
44-
'description' => 'The mode to use for the generated transform'
44+
'description' => 'The mode to use for the generated transform.'
4545
],
4646
'position' => [
4747
'name' => 'position',
@@ -66,7 +66,7 @@ public static function getArguments(): array
6666
'immediately' => [
6767
'name' => 'immediately',
6868
'type' => Type::boolean(),
69-
'description' => 'Whether the transforms should be generated immediately or only when the image is requested used the generated URL'
69+
'description' => 'Whether the transform should be generated immediately or only when the image is requested used the generated URL'
7070
],
7171
];
7272
}

src/gql/directives/SrcSet.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,14 @@ public static function apply($source, $value, array $arguments, ResolveInfo $res
9999
$transforms = [];
100100
foreach ($widths as $width) {
101101
if ($width <= $imageWidth) {
102-
$transforms[] = [
103-
'width' => $width,
104-
'height' => isset($arguments['ratio']) ? round($width * floatval($arguments['ratio'])) : null,
105-
'mode' => isset($arguments['mode']) ? $arguments['mode'] : null,
106-
'format' => isset($arguments['format']) ? $arguments['format'] : null,
107-
];
102+
$transforms[] = static::_generateTransform($width, $arguments);
108103
}
109104
}
110105

111106
$lastWidth = end($widths);
112107
$lastTransform = end($transforms);
113108
if (!$lastTransform || ($lastTransform['width'] < $imageWidth && $imageWidth < $lastWidth)) {
114-
$transforms[] = [
115-
'width' => $imageWidth,
116-
'height' => isset($arguments['ratio']) ? round($imageWidth * floatval($arguments['ratio'])) : null,
117-
'mode' => isset($arguments['mode']) ? $arguments['mode'] : null,
118-
'format' => isset($arguments['format']) ? $arguments['format'] : null,
119-
];
109+
$transforms[] = static::_generateTransform($imageWidth, $arguments);
120110
}
121111

122112
$urls = [];
@@ -127,4 +117,31 @@ public static function apply($source, $value, array $arguments, ResolveInfo $res
127117

128118
return implode(', ', $urls);
129119
}
120+
121+
private static function _generateTransform($width, $arguments)
122+
{
123+
$transform = [
124+
'width' => $width,
125+
'height' => isset($arguments['ratio']) ? round($width * floatval($arguments['ratio'])) : null,
126+
'format' => isset($arguments['format']) ? $arguments['format'] : null,
127+
];
128+
129+
if (isset($arguments['mode'])) {
130+
$transform['mode'] = $arguments['mode'];
131+
}
132+
133+
if (isset($arguments['position'])) {
134+
$transform['position'] = $arguments['position'];
135+
}
136+
137+
if (isset($arguments['interlace'])) {
138+
$transform['interlace'] = $arguments['interlace'];
139+
}
140+
141+
if (isset($arguments['quality'])) {
142+
$transform['quality'] = $arguments['quality'];
143+
}
144+
145+
return $transform;
146+
}
130147
}

0 commit comments

Comments
 (0)