1
|
|
|
<?php |
2
|
|
|
/** |
|
|
|
|
3
|
|
|
* @link https://craftcms.com/ |
|
|
|
|
4
|
|
|
* @copyright Copyright (c) Pixel & Tonic, Inc. |
|
|
|
|
5
|
|
|
* @license https://craftcms.github.io/license/ |
|
|
|
|
6
|
|
|
*/ |
|
|
|
|
7
|
|
|
|
8
|
|
|
namespace nystudio107\imageoptimize\gql\types; |
9
|
|
|
|
10
|
|
|
use craft\gql\base\ObjectType; |
11
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
12
|
|
|
use nystudio107\imageoptimize\models\OptimizedImage; |
13
|
|
|
|
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* @author nystudio107 |
|
|
|
|
16
|
|
|
* @package ImageOptimize |
|
|
|
|
17
|
|
|
* @since 1.6.2 |
|
|
|
|
18
|
|
|
*/ |
|
|
|
|
19
|
|
|
class OptimizedImagesType extends ObjectType |
20
|
|
|
{ |
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
protected function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed |
25
|
|
|
{ |
26
|
|
|
/** @var OptimizedImage $source */ |
|
|
|
|
27
|
|
|
$fieldName = $resolveInfo->fieldName; |
28
|
|
|
|
29
|
|
|
switch ($fieldName) { |
30
|
|
|
// Special-case the `src` field with arguments |
31
|
|
|
case 'src': |
|
|
|
|
32
|
|
|
$width = $arguments['width'] ?? 0; |
33
|
|
|
return $source->src($width); |
34
|
|
|
|
35
|
|
|
// Special-case the `srcWebp` field with arguments |
36
|
|
|
case 'srcWebp': |
|
|
|
|
37
|
|
|
$width = $arguments['width'] ?? 0; |
38
|
|
|
return $source->srcWebp($width); |
39
|
|
|
|
40
|
|
|
// Special-case the `srcset` field with arguments |
41
|
|
|
case 'srcset': |
|
|
|
|
42
|
|
|
$dpr = $arguments['dpr'] ?? false; |
43
|
|
|
return $source->srcset($dpr); |
44
|
|
|
|
45
|
|
|
// Special-case the `srcsetMinWidth` field with arguments |
46
|
|
|
case 'srcsetMinWidth': |
|
|
|
|
47
|
|
|
$width = $arguments['width'] ?? 0; |
48
|
|
|
$dpr = $arguments['dpr'] ?? false; |
49
|
|
|
return $source->srcsetMinWidth($width, $dpr); |
50
|
|
|
|
51
|
|
|
// Special-case the `srcsetMaxWidth` field with arguments |
52
|
|
|
case 'srcsetMaxWidth': |
|
|
|
|
53
|
|
|
$width = $arguments['width'] ?? 0; |
54
|
|
|
$dpr = $arguments['dpr'] ?? false; |
55
|
|
|
return $source->srcsetMaxWidth($width, $dpr); |
56
|
|
|
|
57
|
|
|
// Special-case the `srcsetWebp` field with arguments |
58
|
|
|
case 'srcsetWebp': |
|
|
|
|
59
|
|
|
$dpr = $arguments['dpr'] ?? false; |
60
|
|
|
return $source->srcsetWebp($dpr); |
61
|
|
|
|
62
|
|
|
// Special-case the `srcsetMinWidthWebp` field with arguments |
63
|
|
|
case 'srcsetMinWidthWebp': |
|
|
|
|
64
|
|
|
$width = $arguments['width'] ?? 0; |
65
|
|
|
$dpr = $arguments['dpr'] ?? false; |
66
|
|
|
return $source->srcsetMinWidthWebp($width, $dpr); |
67
|
|
|
|
68
|
|
|
// Special-case the `srcsetMaxWidthWebp` field with arguments |
69
|
|
|
case 'srcsetMaxWidthWebp': |
|
|
|
|
70
|
|
|
$width = $arguments['width'] ?? 0; |
71
|
|
|
$dpr = $arguments['dpr'] ?? false; |
72
|
|
|
return $source->srcsetMaxWidthWebp($width, $dpr); |
73
|
|
|
|
74
|
|
|
// Special-case the `maxSrcsetWidth` field |
75
|
|
|
case 'maxSrcsetWidth': |
|
|
|
|
76
|
|
|
return $source->maxSrcsetWidth(); |
77
|
|
|
|
78
|
|
|
// Special-case the `placeholderImage` field |
79
|
|
|
case 'placeholderImage': |
|
|
|
|
80
|
|
|
return $source->placeholderImage(); |
81
|
|
|
|
82
|
|
|
// Special-case the `placeholderBox` field |
83
|
|
|
case 'placeholderBox': |
|
|
|
|
84
|
|
|
$color = $arguments['color'] ?? null; |
85
|
|
|
return $source->placeholderBox($color); |
86
|
|
|
|
87
|
|
|
// Special-case the `placeholderSilhouette` field |
88
|
|
|
case 'placeholderSilhouette': |
|
|
|
|
89
|
|
|
return $source->placeholderSilhouette(); |
90
|
|
|
|
91
|
|
|
// Special-case the `srcUrls` field |
92
|
|
|
case 'srcUrls': |
|
|
|
|
93
|
|
|
$result = []; |
94
|
|
|
foreach ($source->optimizedImageUrls as $width => $url) { |
|
|
|
|
95
|
|
|
$result[] = ['width' => $width, 'url' => $url]; |
96
|
|
|
} |
|
|
|
|
97
|
|
|
return $result; |
98
|
|
|
|
99
|
|
|
// Special-case the `colorPaletteRgb` field |
100
|
|
|
case 'colorPaletteRgb': |
|
|
|
|
101
|
|
|
return $source->colorPaletteRgb(); |
102
|
|
|
|
103
|
|
|
// Default to just returning the field value |
104
|
|
|
default: |
|
|
|
|
105
|
|
|
return $source[$fieldName]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|