33namespace craft \cloud \imagetransforms ;
44
55use Craft ;
6+ use craft \models \ImageTransform ;
67use Illuminate \Support \Collection ;
8+ use yii \base \Behavior ;
79
810/**
911 * @see https://developers.cloudflare.com/images/transform-images/transform-via-workers/#fetch-options
1012 * @see https://github.com/cloudflare/workerd/blob/main/types/defines/cf.d.ts
13+ *
14+ * @property ImageTransform $owner
1115 */
12- class ImageTransform extends \ craft \ models \ImageTransform
16+ class ImageTransformBehavior extends Behavior
1317{
1418 public ?bool $ anim = null ;
1519 public ?string $ background = null ;
@@ -59,11 +63,6 @@ class ImageTransform extends \craft\models\ImageTransform
5963 */
6064 public ?string $ flip = null ;
6165
62- /**
63- * @var 'auto'|'avif'|'webp'|'jpeg'|'baseline-jpeg'|'json'|string|null
64- */
65- public ?string $ format = null ;
66-
6766 /**
6867 * @var float|null
6968 */
@@ -74,8 +73,6 @@ class ImageTransform extends \craft\models\ImageTransform
7473 */
7574 public string |array |null $ gravity = null ;
7675
77- public ?int $ height = null ;
78-
7976 /**
8077 * @var 'keep'|'copyright'|'none'|null
8178 */
@@ -106,11 +103,9 @@ class ImageTransform extends \craft\models\ImageTransform
106103 */
107104 public null |string |array $ trim = null ;
108105
109- public ?int $ width = null ;
110-
111106 public ?float $ zoom = null ;
112107
113- public function toOptions (): array
108+ public function toOptions (array | string | null $ gravity = null ): array
114109 {
115110 $ reflection = new \ReflectionClass ($ this );
116111
@@ -124,69 +119,56 @@ public function toOptions(): array
124119 $ options ['format ' ] = $ this ->computeFormat ();
125120 $ options ['fit ' ] = $ this ->computeFit ();
126121 $ options ['background ' ] = $ this ->computeBackground ();
127- $ options ['gravity ' ] ??= $ this ->computeGravity ();
122+ $ options ['gravity ' ] ??= $ gravity ?? $ this ->computeGravity ();
123+ $ options ['height ' ] = $ this ->owner ->height ;
124+ $ options ['width ' ] = $ this ->owner ->width ;
128125
129126 return Collection::make ($ options )
130127 ->filter (fn ($ value ) => $ value !== null )
131128 ->all ();
132129 }
133130
134- /**
135- * Compute the Cloudflare format from the base format and interlace settings.
136- *
137- * @return string|null
138- */
139131 private function computeFormat (): ?string
140132 {
141- if ($ this ->format === 'jpg ' && $ this ->interlace === 'none ' ) {
133+ if ($ this ->owner -> format === 'jpg ' && $ this -> owner ->interlace === 'none ' ) {
142134 return 'baseline-jpeg ' ;
143135 }
144136
145- return match ($ this ->format ) {
137+ return match ($ this ->owner -> format ) {
146138 'jpg ' => 'jpeg ' ,
147- default => $ this ->format ,
139+ default => $ this ->owner -> format ,
148140 };
149141 }
150142
151143 /**
152- * Compute the Cloudflare fit mode from the base mode and upscale settings.
153- *
154144 * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#fit
155- * @return string
156145 */
157146 private function computeFit (): string
158147 {
159148 if ($ this ->fit !== null ) {
160149 return $ this ->fit ;
161150 }
162151
163- return match ($ this ->mode ) {
164- 'fit ' => $ this ->upscale ? 'contain ' : 'scale-down ' ,
152+ return match ($ this ->owner -> mode ) {
153+ 'fit ' => $ this ->owner -> upscale ? 'contain ' : 'scale-down ' ,
165154 'stretch ' => 'squeeze ' ,
166155 'letterbox ' => 'pad ' ,
167- default => $ this ->upscale ? 'cover ' : 'crop ' ,
156+ default => $ this ->owner -> upscale ? 'cover ' : 'crop ' ,
168157 };
169158 }
170159
171- /**
172- * Compute the Cloudflare background color from the base mode and fill settings.
173- *
174- * @return string|null
175- */
176160 private function computeBackground (): ?string
177161 {
178162 if ($ this ->background !== null ) {
179163 return $ this ->background ;
180164 }
181165
182- return $ this ->mode === 'letterbox '
183- ? $ this ->fill ?? '#FFFFFF '
166+ return $ this ->owner -> mode === 'letterbox '
167+ ? $ this ->owner -> fill ?? '#FFFFFF '
184168 : null ;
185169 }
186170
187171 /**
188- * Compute the Cloudflare gravity from the base position setting.
189- *
190172 * @return array{x: float, y: float}|null|'face'
191173 */
192174 private function computeGravity (): array |null |string
@@ -195,12 +177,11 @@ private function computeGravity(): array|null|string
195177 return $ this ->gravity ;
196178 }
197179
198- if ($ this ->position === 'center-center ' ) {
180+ if ($ this ->owner -> position === 'center-center ' ) {
199181 return null ;
200182 }
201183
202- // TODO: maybe just do this in Craft
203- $ parts = explode ('- ' , $ this ->position );
184+ $ parts = explode ('- ' , $ this ->owner ->position );
204185
205186 try {
206187 $ x = match ($ parts [1 ] ?? null ) {
@@ -214,7 +195,7 @@ private function computeGravity(): array|null|string
214195 'bottom ' => 1 ,
215196 };
216197 } catch (\UnhandledMatchError $ e ) {
217- Craft::warning ("Invalid position value: ` {$ this ->position }` " , __METHOD__ );
198+ Craft::warning ("Invalid position value: ` {$ this ->owner -> position }` " , __METHOD__ );
218199 return null ;
219200 }
220201
0 commit comments