Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5d1316

Browse files
authored
Add downloadGeneratedSprite and downloadMulti helpers. Add support for urls in multi and sprite Upload APIs.
1 parent 28fdd69 commit c5d1316

File tree

6 files changed

+483
-78
lines changed

6 files changed

+483
-78
lines changed

src/Api/ApiClient.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ public function postAndSignFormAsync($endPoint, $formParams)
142142
return $this->postFormAsync($endPoint, $formParams);
143143
}
144144

145+
/**
146+
* Signs posted parameters using configured account credentials and posts as a JSON to the endpoint.
147+
*
148+
* @param string|array $endPoint The API endpoint path.
149+
* @param array $params The parameters
150+
*
151+
* @return PromiseInterface
152+
*
153+
* @internal
154+
*/
155+
public function postAndSignJsonAsync($endPoint, $params)
156+
{
157+
ApiUtils::signRequest($params, $this->cloud);
158+
159+
return $this->postJsonAsync($endPoint, $params);
160+
}
161+
145162
/**
146163
* Helper method for posting multipart data asynchronously.
147164
*

src/Api/Upload/ArchiveTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function createZip($options = [])
181181
*/
182182
public function downloadArchiveUrl($options = [])
183183
{
184-
$options['mode'] = 'download';
184+
$options['mode'] = self::MODE_DOWNLOAD;
185185
$params = self::buildArchiveParams($options);
186186

187187
ApiUtils::signRequest($params, $this->getCloud());

src/Api/Upload/CreativeTrait.php

Lines changed: 115 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Cloudinary\Api\ApiUtils;
1616
use Cloudinary\ArrayUtils;
1717
use Cloudinary\Asset\AssetType;
18+
use Cloudinary\Transformation\Transformation;
1819
use GuzzleHttp\Promise\PromiseInterface;
20+
use InvalidArgumentException;
1921

2022
/**
2123
* Trait CreativeTrait
@@ -27,40 +29,39 @@
2729
trait CreativeTrait
2830
{
2931
/**
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.
3133
*
3234
* 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.
3436
* * A CSS file that includes the style class names and the location of the individual images in the sprite.
3537
*
3638
* This is an asynchronous function.
3739
*
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.
4043
*
4144
* @return PromiseInterface
4245
*
4346
* @see https://cloudinary.com/documentation/image_upload_api_reference#sprite_method
4447
*/
4548
public function generateSpriteAsync($tag, $options = [])
4649
{
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);
5151

52-
return $this->callUploadApiAsync(UploadEndPoint::SPRITE, $params, $options);
52+
return $this->callUploadApiJsonAsync(UploadEndPoint::SPRITE, $params, $options);
5353
}
5454

5555
/**
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.
5757
*
5858
* 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.
6060
* * A CSS file that includes the style class names and the location of the individual images in the sprite.
6161
*
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.
6465
*
6566
* @return ApiResponse
6667
*
@@ -72,34 +73,58 @@ public function generateSprite($tag, $options = [])
7273
}
7374

7475
/**
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.
77103
*
78104
* This is an asynchronous function.
79105
*
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.
82109
*
83110
* @return PromiseInterface
84111
*
85112
* @see https://cloudinary.com/documentation/image_upload_api_reference#multi_method
86113
*/
87114
public function multiAsync($tag, $options = [])
88115
{
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);
93117

94-
return $this->callUploadApiAsync(UploadEndPoint::MULTI, $params, $options);
118+
return $this->callUploadApiJsonAsync(UploadEndPoint::MULTI, $params, $options);
95119
}
96120

97121
/**
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.
100124
*
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.
103128
*
104129
* @return ApiResponse
105130
*
@@ -110,6 +135,31 @@ public function multi($tag, $options = [])
110135
return $this->multiAsync($tag, $options)->wait();
111136
}
112137

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+
113163
/**
114164
* Creates derived images for all of the individual pages in a multi-page file (PDF or animated GIF).
115165
*
@@ -205,7 +255,46 @@ public function text($text, $options = [])
205255
return $this->textAsync($text, $options)->wait();
206256
}
207257

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;
208296

297+
}
209298
/**
210299
* Create auto-generated video slideshows.
211300
*

src/Api/Upload/UploadApi.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class UploadApi
3434
use TagTrait;
3535
use UploadTrait;
3636

37+
const MODE_DOWNLOAD = 'download';
38+
3739
/**
3840
* @var ApiClient $apiClient The HTTP API client instance.
3941
*/
@@ -113,4 +115,23 @@ protected function callUploadApiAsync($endPoint = UploadEndPoint::UPLOAD, $param
113115
ApiUtils::finalizeUploadApiParams($params)
114116
);
115117
}
118+
119+
/**
120+
* Internal method for performing all Upload API calls as a json.
121+
*
122+
* @param string $endPoint The relative endpoint.
123+
* @param array $params Parameters to pass to the endpoint.
124+
* @param array $options Additional options.
125+
*
126+
* @return PromiseInterface
127+
*
128+
* @internal
129+
*/
130+
protected function callUploadApiJsonAsync($endPoint = UploadEndPoint::UPLOAD, $params = [], $options = [])
131+
{
132+
return $this->apiClient->postAndSignJsonAsync(
133+
self::getUploadApiEndPoint($endPoint, $options),
134+
ApiUtils::finalizeUploadApiParams($params)
135+
);
136+
}
116137
}

0 commit comments

Comments
 (0)