15
15
use Cloudinary \Api \ApiUtils ;
16
16
use Cloudinary \ArrayUtils ;
17
17
use Cloudinary \Asset \AssetType ;
18
+ use Cloudinary \Transformation \Transformation ;
18
19
use GuzzleHttp \Promise \PromiseInterface ;
20
+ use InvalidArgumentException ;
19
21
20
22
/**
21
23
* Trait CreativeTrait
27
29
trait CreativeTrait
28
30
{
29
31
/**
30
- * Creates a sprite from all images that have been assigned a specified tag.
32
+ * Creates a sprite from all images that have been assigned a specified tag or from a provided array of image URLs .
31
33
*
32
34
* The process produces two files:
33
- * * A single image file containing all the images with the specified tag (PNG by default) .
35
+ * * A single sprite image file containing all the images.
34
36
* * A CSS file that includes the style class names and the location of the individual images in the sprite.
35
37
*
36
38
* This is an asynchronous function.
37
39
*
38
- * @param string $tag The tag that indicates which images to include in the sprite.
39
- * @param array $options The optional parameters. See the upload API documentation.
40
+ * @param string|array $tag A string specifying a tag that indicates which images to include or an array
41
+ * which include options and image URLs.
42
+ * @param array $options The optional parameters. Should be omitted when $tag is an array.
40
43
*
41
44
* @return PromiseInterface
42
45
*
43
46
* @see https://cloudinary.com/documentation/image_upload_api_reference#sprite_method
44
47
*/
45
48
public function generateSpriteAsync ($ tag , $ options = [])
46
49
{
47
- $ options ['transformation ' ] = ApiUtils::serializeAssetTransformations ($ options );
48
-
49
- $ params = ArrayUtils::whitelist ($ options , ['async ' , 'notification_url ' , 'transformation ' ]);
50
- $ params ['tag ' ] = $ tag ;
50
+ $ params = self ::buildSpriteAndMultiParams ($ tag , $ options );
51
51
52
- return $ this ->callUploadApiAsync (UploadEndPoint::SPRITE , $ params , $ options );
52
+ return $ this ->callUploadApiJsonAsync (UploadEndPoint::SPRITE , $ params , $ options );
53
53
}
54
54
55
55
/**
56
- * Creates a sprite from all images that have been assigned a specified tag.
56
+ * Creates a sprite from all images that have been assigned a specified tag or from a provided array of image URLs .
57
57
*
58
58
* The process produces two files:
59
- * * A single image file containing all the images with the specified tag (PNG by default) .
59
+ * * A single sprite image file containing all the images.
60
60
* * A CSS file that includes the style class names and the location of the individual images in the sprite.
61
61
*
62
- * @param string $tag The tag that indicates which images to include in the sprite.
63
- * @param array $options The optional parameters. See the upload API documentation.
62
+ * @param string|array $tag A string specifying a tag that indicates which images to include or an array
63
+ * which include options and image URLs.
64
+ * @param array $options The optional parameters. Should be omitted when $tag is an array.
64
65
*
65
66
* @return ApiResponse
66
67
*
@@ -72,34 +73,58 @@ public function generateSprite($tag, $options = [])
72
73
}
73
74
74
75
/**
75
- * Creates a single animated image, video or PDF from all image assets that have been
76
- * assigned a specified tag.
76
+ * Generates an url to create a sprite from all images that have been assigned a specified tag or from a provided
77
+ * array of URLs.
78
+ *
79
+ * @param string|array $tag A string specifying a tag that indicates which images to include or an array
80
+ * which include options and image URLs.
81
+ * @param array $options The optional parameters. Should be omitted when $tag is an array.
82
+ *
83
+ * @return string
84
+ *
85
+ * @see https://cloudinary.com/documentation/image_upload_api_reference#sprite_method
86
+ */
87
+ public function downloadGeneratedSprite ($ tag , $ options = [])
88
+ {
89
+ $ params = self ::buildSpriteAndMultiParams ($ tag , $ options );
90
+
91
+ $ params ['mode ' ] = self ::MODE_DOWNLOAD ;
92
+
93
+ $ params = ApiUtils::finalizeUploadApiParams ($ params );
94
+
95
+ ApiUtils::signRequest ($ params , $ this ->getCloud ());
96
+
97
+ return $ this ->getUploadUrl (AssetType::IMAGE , UploadEndPoint::SPRITE , $ params );
98
+ }
99
+
100
+ /**
101
+ * Creates a single animated image, video or PDF from all image assets that have been assigned a specified tag or
102
+ * from a provided array of URLs.
77
103
*
78
104
* This is an asynchronous function.
79
105
*
80
- * @param string $tag The tag that indicates which images to include in the animated image, video or PDF.
81
- * @param array $options The optional parameters. See the upload API documentation.
106
+ * @param string|array $tag A string specifying a tag that indicates which images to include or an array
107
+ * which include options and image URLs.
108
+ * @param array $options The optional parameters. Should be omitted when $tag is an array.
82
109
*
83
110
* @return PromiseInterface
84
111
*
85
112
* @see https://cloudinary.com/documentation/image_upload_api_reference#multi_method
86
113
*/
87
114
public function multiAsync ($ tag , $ options = [])
88
115
{
89
- $ options ['transformation ' ] = ApiUtils::serializeAssetTransformations ($ options );
90
-
91
- $ params = ArrayUtils::whitelist ($ options , ['format ' , 'async ' , 'notification_url ' , 'transformation ' ]);
92
- $ params ['tag ' ] = $ tag ;
116
+ $ params = self ::buildSpriteAndMultiParams ($ tag , $ options );
93
117
94
- return $ this ->callUploadApiAsync (UploadEndPoint::MULTI , $ params , $ options );
118
+ return $ this ->callUploadApiJsonAsync (UploadEndPoint::MULTI , $ params , $ options );
95
119
}
96
120
97
121
/**
98
- * Creates a single animated image, video or PDF from all image assets that have been
99
- * assigned a specified tag .
122
+ * Creates a single animated image, video or PDF from all image assets that have been assigned a specified tag or
123
+ * from a provided array of URLs .
100
124
*
101
- * @param string $tag The tag that indicates which images to include in the animated image, video or PDF.
102
- * @param array $options The optional parameters. See the upload API documentation.
125
+ * @param string|array $tag A string specifying a tag that indicates which images to include or an array
126
+ * which include options and image URLs.
127
+ * @param array $options The optional parameters. Should be omitted when $tag is an array.
103
128
*
104
129
* @return ApiResponse
105
130
*
@@ -110,6 +135,31 @@ public function multi($tag, $options = [])
110
135
return $ this ->multiAsync ($ tag , $ options )->wait ();
111
136
}
112
137
138
+ /**
139
+ * Generates an url to create a single animated image, video or PDF from all image assets that have been assigned
140
+ * a specified tag or from a provided array of URLs.
141
+ *
142
+ * @param string|array $tag A string specifying a tag that indicates which images to include or an array
143
+ * which include options and image URLs.
144
+ * @param array $options The optional parameters. Should be omitted when $tag is an array.
145
+ *
146
+ * @return string
147
+ *
148
+ * @see https://cloudinary.com/documentation/image_upload_api_reference#sprite_method
149
+ */
150
+ public function downloadMulti ($ tag , $ options = [])
151
+ {
152
+ $ params = self ::buildSpriteAndMultiParams ($ tag , $ options );
153
+
154
+ $ params ['mode ' ] = self ::MODE_DOWNLOAD ;
155
+
156
+ $ params = ApiUtils::finalizeUploadApiParams ($ params );
157
+
158
+ ApiUtils::signRequest ($ params , $ this ->getCloud ());
159
+
160
+ return $ this ->getUploadUrl (AssetType::IMAGE , UploadEndPoint::MULTI , $ params );
161
+ }
162
+
113
163
/**
114
164
* Creates derived images for all of the individual pages in a multi-page file (PDF or animated GIF).
115
165
*
@@ -205,7 +255,46 @@ public function text($text, $options = [])
205
255
return $ this ->textAsync ($ text , $ options )->wait ();
206
256
}
207
257
258
+ /**
259
+ * Build params for multi, downloadMulti, generateSprite, and downloadGeneratedSprite methods.
260
+ *
261
+ * @param string|array $tagOrOptions A string specifying a tag that indicates which images to include or an array
262
+ * which include image URLs and options.
263
+ * @param array $options The optional parameters. Should be omitted when $tagOrOptions is an array.
264
+ *
265
+ * @return array
266
+ */
267
+ private static function buildSpriteAndMultiParams ($ tagOrOptions , $ options )
268
+ {
269
+ if (is_array ($ tagOrOptions )) {
270
+ if (empty ($ options )) {
271
+ $ options = $ tagOrOptions ;
272
+ } else {
273
+ throw new InvalidArgumentException ('First argument must be a tag when additional options are passed ' );
274
+ }
275
+ $ tag = null ;
276
+ } else {
277
+ $ tag = $ tagOrOptions ;
278
+ }
279
+ $ urls = ArrayUtils::get ($ options , 'urls ' );
280
+
281
+ if (empty ($ tag ) && empty ($ urls )) {
282
+ throw new InvalidArgumentException ('Either tag or urls are required ' );
283
+ }
284
+
285
+ $ transformation = new Transformation ($ options );
286
+ $ transformation ->format (ArrayUtils::get ($ options , 'format ' ));
287
+ $ options ['transformation ' ] = ApiUtils::serializeAssetTransformations ($ transformation );
288
+
289
+ $ params = ArrayUtils::whitelist ($ options , ['format ' , 'async ' , 'notification_url ' , 'transformation ' ]);
290
+ if (isset ($ urls )) {
291
+ $ params ['urls ' ] = $ urls ;
292
+ } else {
293
+ $ params ['tag ' ] = $ tag ;
294
+ }
295
+ return $ params ;
208
296
297
+ }
209
298
/**
210
299
* Create auto-generated video slideshows.
211
300
*
0 commit comments