kraken 0.3.6 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/node.js.yml +37 -0
- package/.github/workflows/npm-publish.yml +46 -0
- package/.jshintrc +20 -18
- package/LICENSE +9 -0
- package/README.md +430 -144
- package/docs/code/README.md +176 -0
- package/package.json +73 -26
- package/src/kraken.js +172 -0
- package/src/lib/AxiosOptions.js +24 -0
- package/src/lib/KrakenApiData.js +25 -0
- package/src/lib/KrakenApiList.js +36 -0
- package/src/lib/KrakenAuth.js +18 -0
- package/src/lib/KrakenResponse.js +16 -0
- package/src/lib/KrakenUploadData.js +100 -0
- package/test/.jshintrc +27 -0
- package/test/kraken.test.js +177 -0
- package/test/support/images/image.gif +0 -0
- package/.travis.yml +0 -4
- package/lib/kraken.js +0 -92
package/README.md
CHANGED
|
@@ -1,31 +1,48 @@
|
|
|
1
|
-
Node.js module for Kraken.io API
|
|
2
|
-
===========
|
|
1
|
+
# Node.js module for Kraken.io API
|
|
3
2
|
|
|
4
|
-
[](https://app.travis-ci.com/kraken-io/kraken-node)
|
|
5
4
|
|
|
6
5
|
With this Node module you can plug into the power and speed of [Kraken.io](http://kraken.io/) Image Optimizer.
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
7
|
+
- [Node.js module for Kraken.io API](#nodejs-module-for-krakenio-api)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Requirements](#requirements)
|
|
10
|
+
- [Dependencies](#dependencies)
|
|
11
|
+
- [Getting Started](#getting-started)
|
|
12
|
+
- [Downloading Images](#downloading-images)
|
|
13
|
+
- [How to use](#how-to-use)
|
|
14
|
+
- [Wait and Callback URL](#wait-and-callback-url)
|
|
15
|
+
- [Wait option](#wait-option)
|
|
16
|
+
- [Callback URL](#callback-url)
|
|
17
|
+
- [Promises](#promises)
|
|
18
|
+
- [Async/Await](#asyncawait)
|
|
19
|
+
- [Authentication](#authentication)
|
|
20
|
+
- [Usage - Image URL](#usage---image-url)
|
|
21
|
+
- [Usage - Image Upload](#usage---image-upload)
|
|
22
|
+
- [Usage - User status](#usage---user-status)
|
|
23
|
+
- [Lossy Optimization](#lossy-optimization)
|
|
24
|
+
- [PNG Images](#png-images)
|
|
25
|
+
- [JPEG Images](#jpeg-images)
|
|
26
|
+
- [Image Resizing](#image-resizing)
|
|
27
|
+
- [WebP Compression](#webp-compression)
|
|
28
|
+
- [Amazon S3 and Rackspace Cloud Files](#amazon-s3-and-rackspace-cloud-files)
|
|
29
|
+
- [Amazon S3](#amazon-s3)
|
|
30
|
+
- [Rackspace Cloud Files](#rackspace-cloud-files)
|
|
31
|
+
- [LICENSE - MIT](#license---mit)
|
|
24
32
|
|
|
25
33
|
## Installation
|
|
26
34
|
|
|
27
35
|
npm install kraken --save
|
|
28
36
|
|
|
37
|
+
## Requirements
|
|
38
|
+
|
|
39
|
+
- Minimum NodeJS version: `12.20.0`
|
|
40
|
+
|
|
41
|
+
## Dependencies
|
|
42
|
+
|
|
43
|
+
- [`form-data`](https://www.npmjs.com/package/form-data)
|
|
44
|
+
- [`axios`](https://www.npmjs.com/package/axios)
|
|
45
|
+
|
|
29
46
|
## Getting Started
|
|
30
47
|
|
|
31
48
|
First you need to sign-up for the [Kraken API](https://kraken.io/plans/) and obtain your unique **API Key** and **API Secret**. You will find both under [API Credentials](https://kraken.io/account/api-credentials). Once you have set up your account, you can start using Kraken.io's image optimization API in your applications.
|
|
@@ -50,7 +67,7 @@ With the `wait` option turned on for every request to the API, the connection wi
|
|
|
50
67
|
|
|
51
68
|
**Request:**
|
|
52
69
|
|
|
53
|
-
|
|
70
|
+
```javascript
|
|
54
71
|
{
|
|
55
72
|
"auth": {
|
|
56
73
|
"api_key": "your-api-key",
|
|
@@ -59,11 +76,11 @@ With the `wait` option turned on for every request to the API, the connection wi
|
|
|
59
76
|
"url": "http://awesome-website.com/images/header.jpg",
|
|
60
77
|
"wait": true
|
|
61
78
|
}
|
|
62
|
-
|
|
79
|
+
```
|
|
63
80
|
|
|
64
81
|
**Response**
|
|
65
82
|
|
|
66
|
-
|
|
83
|
+
```javascript
|
|
67
84
|
{
|
|
68
85
|
"success": true,
|
|
69
86
|
"file_name": "header.jpg",
|
|
@@ -72,17 +89,17 @@ With the `wait` option turned on for every request to the API, the connection wi
|
|
|
72
89
|
"saved_bytes": 159162,
|
|
73
90
|
"kraked_url": "http://dl.kraken.io/d1aacd2a2280c2ffc7b4906a09f78f46/header.jpg"
|
|
74
91
|
}
|
|
75
|
-
|
|
92
|
+
```
|
|
76
93
|
|
|
77
94
|
### Callback URL
|
|
78
95
|
|
|
79
96
|
With the Callback URL the HTTPS connection will be terminated immediately and a unique `id` will be returned in the response body. After the optimization is over Kraken will POST a message to the `callback_url` specified in your request. The ID in the response will reflect the ID in the results posted to your Callback URL.
|
|
80
97
|
|
|
81
|
-
We recommend [
|
|
98
|
+
We recommend [RequestBin.com](http://requestbin.com) as an easy way to capture optimization results for initial testing.
|
|
82
99
|
|
|
83
100
|
**Request:**
|
|
84
101
|
|
|
85
|
-
|
|
102
|
+
```javascript
|
|
86
103
|
{
|
|
87
104
|
"auth": {
|
|
88
105
|
"api_key": "your-api-key",
|
|
@@ -91,19 +108,19 @@ We recommend [requestb.in](http://requestb.in) as an easy way to capture optimiz
|
|
|
91
108
|
"url": "http://image-url.com/file.jpg",
|
|
92
109
|
"callback_url": "http://awesome-website.com/kraken_results"
|
|
93
110
|
}
|
|
94
|
-
|
|
111
|
+
```
|
|
95
112
|
|
|
96
113
|
**Response:**
|
|
97
114
|
|
|
98
|
-
|
|
115
|
+
```javascript
|
|
99
116
|
{
|
|
100
117
|
"id": "18fede37617a787649c3f60b9f1f280d"
|
|
101
118
|
}
|
|
102
|
-
|
|
119
|
+
```
|
|
103
120
|
|
|
104
121
|
**Results posted to the Callback URL:**
|
|
105
122
|
|
|
106
|
-
|
|
123
|
+
```javascript
|
|
107
124
|
{
|
|
108
125
|
"id": "18fede37617a787649c3f60b9f1f280d"
|
|
109
126
|
"success": true,
|
|
@@ -113,50 +130,100 @@ We recommend [requestb.in](http://requestb.in) as an easy way to capture optimiz
|
|
|
113
130
|
"saved_bytes": 159162,
|
|
114
131
|
"kraked_url": "http://dl.kraken.io/18fede37617a787649c3f60b9f1f280d/file.jpg"
|
|
115
132
|
}
|
|
116
|
-
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Promises
|
|
136
|
+
|
|
137
|
+
All Kraken.io API methods returns promises and `cb` argument is optional:
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
var Kraken = require('kraken')
|
|
141
|
+
|
|
142
|
+
var kraken = new Kraken({
|
|
143
|
+
api_key: 'your-api-key',
|
|
144
|
+
api_secret: 'your-api-secret'
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
kraken
|
|
148
|
+
.userStatus()
|
|
149
|
+
.then((status) => console.log('Kraken user status:', status))
|
|
150
|
+
.catch((error) =>
|
|
151
|
+
console.error('Error while requesting user status:', error)
|
|
152
|
+
)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Async/Await
|
|
156
|
+
|
|
157
|
+
All Kraken.io API methods support async/await syntax:
|
|
158
|
+
|
|
159
|
+
```javascript
|
|
160
|
+
var Kraken = require('kraken')
|
|
161
|
+
|
|
162
|
+
var kraken = new Kraken({
|
|
163
|
+
api_key: 'your-api-key',
|
|
164
|
+
api_secret: 'your-api-secret'
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
async function exampleAsynFunction() {
|
|
168
|
+
let result = await kraken.userStatus()
|
|
169
|
+
console.log('Kraken user status:', result)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
exampleAsynFunction()
|
|
173
|
+
```
|
|
117
174
|
|
|
118
175
|
## Authentication
|
|
119
176
|
|
|
120
177
|
The first step is to authenticate to Kraken API by providing your unique API Key and API Secret while creating a new Kraken instance:
|
|
121
178
|
|
|
122
|
-
|
|
123
|
-
var Kraken = require('kraken')
|
|
179
|
+
```javascript
|
|
180
|
+
var Kraken = require('kraken')
|
|
124
181
|
|
|
125
182
|
var kraken = new Kraken({
|
|
126
183
|
api_key: 'your-api-key',
|
|
127
184
|
api_secret: 'your-api-secret'
|
|
128
|
-
})
|
|
129
|
-
|
|
185
|
+
})
|
|
186
|
+
```
|
|
130
187
|
|
|
131
188
|
## Usage - Image URL
|
|
132
189
|
|
|
133
190
|
To optimize an image by providing image URL use the `kraken.url()` method. You will need to provide two mandatory parameters - `url` to the image and `wait` or `callback_url`:
|
|
134
191
|
|
|
135
|
-
|
|
136
|
-
var Kraken = require('kraken')
|
|
192
|
+
```javascript
|
|
193
|
+
var Kraken = require('kraken')
|
|
137
194
|
|
|
138
195
|
var kraken = new Kraken({
|
|
139
196
|
api_key: 'your-api-key',
|
|
140
197
|
api_secret: 'your-api-secret'
|
|
141
|
-
})
|
|
198
|
+
})
|
|
142
199
|
|
|
143
200
|
var opts = {
|
|
144
201
|
url: 'http://image-url.com/file.jpg',
|
|
145
202
|
wait: true
|
|
146
|
-
}
|
|
203
|
+
}
|
|
147
204
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
205
|
+
// with callbacks
|
|
206
|
+
kraken.url(opts, function (err, data) {
|
|
207
|
+
if (err) {
|
|
208
|
+
console.log('Failed. Error message: %s', err)
|
|
151
209
|
} else {
|
|
152
|
-
console.log('
|
|
210
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
153
211
|
}
|
|
154
|
-
})
|
|
155
|
-
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
// with promises
|
|
215
|
+
kraken.url(opts)
|
|
216
|
+
.then(data => {
|
|
217
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
218
|
+
})
|
|
219
|
+
.catch(err => {
|
|
220
|
+
console.log('Failed. Error message: %s', err)
|
|
221
|
+
})
|
|
222
|
+
```
|
|
156
223
|
|
|
157
224
|
Depending on a choosen response option (Wait or Callback URL) in the `data` object you will find either the optimization ID or optimization results containing a `success` property, file name, original file size, kraked file size, amount of savings and optimized image URL:
|
|
158
225
|
|
|
159
|
-
|
|
226
|
+
```javascript
|
|
160
227
|
{
|
|
161
228
|
success: true,
|
|
162
229
|
file_name: 'file.jpg',
|
|
@@ -165,45 +232,242 @@ Depending on a choosen response option (Wait or Callback URL) in the `data` obje
|
|
|
165
232
|
saved_bytes: 30087,
|
|
166
233
|
kraked_url: 'http://dl.kraken.io/d1aacd2a2280c2ffc7b4906a09f78f46/file.jpg'
|
|
167
234
|
}
|
|
168
|
-
|
|
235
|
+
```
|
|
169
236
|
|
|
170
237
|
If no saving were found, the API will return an object containing `"success":true` and will report saved_bytes of 0:
|
|
171
238
|
|
|
172
|
-
|
|
239
|
+
```javascript
|
|
173
240
|
{
|
|
174
241
|
success: true,
|
|
175
242
|
saved_bytes: 0,
|
|
176
243
|
}
|
|
177
|
-
|
|
244
|
+
```
|
|
178
245
|
|
|
179
246
|
## Usage - Image Upload
|
|
180
247
|
|
|
181
|
-
If you want to upload your images directly to Kraken API use the `kraken.upload()` method. You will need to provide two mandatory parameters - `file`
|
|
248
|
+
If you want to upload your images directly to Kraken API use the `kraken.upload()` method. You will need to provide two mandatory parameters - `file` and `wait` or `callback_url`.
|
|
249
|
+
|
|
250
|
+
`file` parameter have next options:
|
|
251
|
+
|
|
252
|
+
- `String` - if file parameter is a string type, then it will be interpreted as a path to file and will be loaded using `fs.createReadStream()` function
|
|
253
|
+
- `Stream` | `Buffer` | `String` - file content, will be generated random filename
|
|
254
|
+
- `Object` with next properties:
|
|
255
|
+
- _mandatory_ `path` or `value` - if `path` is present, `value` is ignored
|
|
256
|
+
- `path` - path to file, will be loaded using `fs.createReadStream()` function
|
|
257
|
+
- `value` - file content, supported types: `Stream` | `Buffer` | `String`
|
|
258
|
+
- [`filename`] - new file name to be sended to server, this parameter allow to send different file name to server
|
|
182
259
|
|
|
183
260
|
In the `data` object you will find the same optimization properties as with `url` option above.
|
|
184
261
|
|
|
185
|
-
|
|
262
|
+
```javascript
|
|
186
263
|
var Kraken = require('kraken'),
|
|
187
|
-
fs = require('fs')
|
|
264
|
+
fs = require('fs')
|
|
188
265
|
|
|
189
266
|
var kraken = new Kraken({
|
|
190
267
|
api_key: 'your-api-key',
|
|
191
268
|
api_secret: 'your-api-secret'
|
|
192
|
-
})
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
var opts = {
|
|
272
|
+
file: 'file.jpg',
|
|
273
|
+
wait: true
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// with callback
|
|
277
|
+
kraken.upload(opts, function (err, data) {
|
|
278
|
+
if (err) {
|
|
279
|
+
console.log('Failed. Error message: %s', err)
|
|
280
|
+
} else {
|
|
281
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
// with promise
|
|
286
|
+
kraken.upload(opts)
|
|
287
|
+
.then(data => {
|
|
288
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
289
|
+
})
|
|
290
|
+
.catch(err => {
|
|
291
|
+
console.log('Failed. Error message: %s', err)
|
|
292
|
+
})
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
Buffer as file, filename is ommited - it will be random:
|
|
296
|
+
|
|
297
|
+
```javascript
|
|
298
|
+
var opts = {
|
|
299
|
+
file: fs.readFileSync('file.jpg'),
|
|
300
|
+
wait: true
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// with callback
|
|
304
|
+
kraken.upload(opts, function (err, data) {
|
|
305
|
+
if (err) {
|
|
306
|
+
console.log('Failed. Error message: %s', err)
|
|
307
|
+
} else {
|
|
308
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
309
|
+
}
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
// with promise
|
|
313
|
+
kraken.upload(opts)
|
|
314
|
+
.then(data => {
|
|
315
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
316
|
+
})
|
|
317
|
+
.catch(err => {
|
|
318
|
+
console.log('Failed. Error message: %s', err)
|
|
319
|
+
})
|
|
320
|
+
```
|
|
193
321
|
|
|
322
|
+
Stream as file, filename is ommited - it will be random:
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
194
325
|
var opts = {
|
|
195
326
|
file: fs.createReadStream('file.jpg'),
|
|
196
327
|
wait: true
|
|
197
|
-
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// with callback
|
|
331
|
+
kraken.upload(opts, function (err, data) {
|
|
332
|
+
if (err) {
|
|
333
|
+
console.log('Failed. Error message: %s', err)
|
|
334
|
+
} else {
|
|
335
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
336
|
+
}
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
// with promise
|
|
340
|
+
kraken.upload(opts)
|
|
341
|
+
.then(data => {
|
|
342
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
343
|
+
})
|
|
344
|
+
.catch(err => {
|
|
345
|
+
console.log('Failed. Error message: %s', err)
|
|
346
|
+
})
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Object as file options with path parameter:
|
|
350
|
+
|
|
351
|
+
```javascript
|
|
352
|
+
var opts = {
|
|
353
|
+
file: {
|
|
354
|
+
path: 'file.jpg',
|
|
355
|
+
fileName: 'example.jpg'
|
|
356
|
+
},
|
|
357
|
+
wait: true
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// with callback
|
|
361
|
+
kraken.upload(opts, function (err, data) {
|
|
362
|
+
if (err) {
|
|
363
|
+
console.log('Failed. Error message: %s', err)
|
|
364
|
+
} else {
|
|
365
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
366
|
+
}
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
// with promise
|
|
370
|
+
kraken.upload(opts)
|
|
371
|
+
.then(data => {
|
|
372
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
373
|
+
})
|
|
374
|
+
.catch(err => {
|
|
375
|
+
console.log('Failed. Error message: %s', err)
|
|
376
|
+
})
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
Object as file options with buffer as value:
|
|
380
|
+
|
|
381
|
+
```javascript
|
|
382
|
+
var opts = {
|
|
383
|
+
file: {
|
|
384
|
+
value: fs.readFileSync('file.jpg'),
|
|
385
|
+
fileName: 'new_file_name.jpg'
|
|
386
|
+
}
|
|
387
|
+
wait: true
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// with callback
|
|
391
|
+
kraken.upload(opts, function (err, data) {
|
|
392
|
+
if (err) {
|
|
393
|
+
console.log('Failed. Error message: %s', err)
|
|
394
|
+
} else {
|
|
395
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
396
|
+
}
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
// with promise
|
|
400
|
+
kraken.upload(opts)
|
|
401
|
+
.then(data => {
|
|
402
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
403
|
+
})
|
|
404
|
+
.catch(err => {
|
|
405
|
+
console.log('Failed. Error message: %s', err)
|
|
406
|
+
})
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
## Usage - User status
|
|
410
|
+
|
|
411
|
+
If you want to check your quotas or your account status, you can use [userStatus](https://kraken.io/docs/user-status) which will return something like:
|
|
412
|
+
|
|
413
|
+
```json
|
|
414
|
+
{
|
|
415
|
+
"success": true,
|
|
416
|
+
"active": true,
|
|
417
|
+
"plan_name": "Enterprise",
|
|
418
|
+
"quota_total": 64424509440,
|
|
419
|
+
"quota_used": 313271610,
|
|
420
|
+
"quota_remaining": 64111237830
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
```javascript
|
|
425
|
+
var Kraken = require('kraken')
|
|
426
|
+
var kraken = new Kraken({
|
|
427
|
+
api_key: 'your-api-key',
|
|
428
|
+
api_secret: 'your-api-secret'
|
|
429
|
+
})
|
|
198
430
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
431
|
+
// with callback
|
|
432
|
+
kraken.userStatus(function (err, data) {
|
|
433
|
+
if (err) {
|
|
434
|
+
console.log('Failed. Error message: %s', err)
|
|
202
435
|
} else {
|
|
203
|
-
console.log('
|
|
436
|
+
console.log('Success', {
|
|
437
|
+
success: data.success,
|
|
438
|
+
active: data.active,
|
|
439
|
+
quota_total: formatBytes(data.quota_total, 2),
|
|
440
|
+
quota_used: formatBytes(data.quota_used, 2),
|
|
441
|
+
quota_remaining: formatBytes(data.quota_remaining, 2)
|
|
442
|
+
})
|
|
204
443
|
}
|
|
205
|
-
})
|
|
206
|
-
|
|
444
|
+
}) //formatBytes(data,2)
|
|
445
|
+
|
|
446
|
+
// with promise
|
|
447
|
+
kraken.userStatus()
|
|
448
|
+
.then(data => {
|
|
449
|
+
console.log('Success', {
|
|
450
|
+
success: data.success,
|
|
451
|
+
active: data.active,
|
|
452
|
+
quota_total: formatBytes(data.quota_total, 2),
|
|
453
|
+
quota_used: formatBytes(data.quota_used, 2),
|
|
454
|
+
quota_remaining: formatBytes(data.quota_remaining, 2)
|
|
455
|
+
})
|
|
456
|
+
})
|
|
457
|
+
.catch(err => {
|
|
458
|
+
console.log('Failed. Error message: %s', err)
|
|
459
|
+
})//formatBytes(data,2)
|
|
460
|
+
|
|
461
|
+
// Optional function which will format the bytes value
|
|
462
|
+
function formatBytes(value, decimal) {
|
|
463
|
+
if (0 == value) return '0 Bytes'
|
|
464
|
+
var c = 1024,
|
|
465
|
+
d = decimal || 2,
|
|
466
|
+
e = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
|
|
467
|
+
f = Math.floor(Math.log(value) / Math.log(c))
|
|
468
|
+
return parseFloat((value / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
|
|
469
|
+
}
|
|
470
|
+
```
|
|
207
471
|
|
|
208
472
|
## Lossy Optimization
|
|
209
473
|
|
|
@@ -211,31 +475,33 @@ When you decide to sacrifice just a small amount of image quality (usually unnot
|
|
|
211
475
|
|
|
212
476
|
To use lossy optimizations simply set `lossy: true` in your request:
|
|
213
477
|
|
|
214
|
-
|
|
478
|
+
```javascript
|
|
215
479
|
var opts = {
|
|
216
480
|
file: '/path/to/image/file.jpg',
|
|
217
481
|
lossy: true,
|
|
218
482
|
wait: true
|
|
219
|
-
}
|
|
220
|
-
|
|
483
|
+
}
|
|
484
|
+
```
|
|
221
485
|
|
|
222
486
|
### PNG Images
|
|
487
|
+
|
|
223
488
|
PNG images will be converted from 24-bit to paletted 8-bit with full alpha channel. This process is called PNG quantization in RGBA format and means the amout of colours used in an image will be reduced to 256 while maintaining all information about alpha transparency.
|
|
224
489
|
|
|
225
490
|
### JPEG Images
|
|
491
|
+
|
|
226
492
|
For lossy JPEG optimizations Kraken will generate multiple copies of a input image with a different quality settings. It will then intelligently pick the one with the best quality to filesize ration. This ensures your JPEG image will be at the smallest size with the highest possible quality, without the need for a human to select the optimal image.
|
|
227
493
|
|
|
228
494
|
## Image Resizing
|
|
229
495
|
|
|
230
496
|
Image resizing option is great for creating thumbnails or preview images in your applications. Kraken will first resize the given image and then optimize it with its vast array of optimization algorithms. The `resize` option needs a few parameters to be passed like desired `width` and/or `height` and a mandatory `strategy` property. For example:
|
|
231
497
|
|
|
232
|
-
|
|
233
|
-
var Kraken = require('kraken')
|
|
498
|
+
```javascript
|
|
499
|
+
var Kraken = require('kraken')
|
|
234
500
|
|
|
235
501
|
var kraken = new Kraken({
|
|
236
502
|
api_key: 'your-api-key',
|
|
237
503
|
api_secret: 'your-api-secret'
|
|
238
|
-
})
|
|
504
|
+
})
|
|
239
505
|
|
|
240
506
|
var opts = {
|
|
241
507
|
file: '/path/to/image/file.jpg',
|
|
@@ -245,24 +511,34 @@ var opts = {
|
|
|
245
511
|
height: 75,
|
|
246
512
|
strategy: 'crop'
|
|
247
513
|
}
|
|
248
|
-
}
|
|
514
|
+
}
|
|
249
515
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
516
|
+
// with callback
|
|
517
|
+
kraken.upload(opts, function (err, data) {
|
|
518
|
+
if (err) {
|
|
519
|
+
console.log('Failed. Error message: %s', err)
|
|
253
520
|
} else {
|
|
254
|
-
console.log('
|
|
521
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
255
522
|
}
|
|
256
|
-
})
|
|
257
|
-
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
// with promise
|
|
526
|
+
kraken.upload(opts)
|
|
527
|
+
.then(data => {
|
|
528
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
529
|
+
})
|
|
530
|
+
.catch(err => {
|
|
531
|
+
console.log('Failed. Error message: %s', err)
|
|
532
|
+
})
|
|
533
|
+
```
|
|
258
534
|
|
|
259
535
|
The `strategy` property can have one of the following values:
|
|
260
536
|
|
|
261
|
-
-
|
|
262
|
-
-
|
|
263
|
-
-
|
|
264
|
-
-
|
|
265
|
-
-
|
|
537
|
+
- `exact` - Resize by exact width/height. No aspect ratio will be maintained.
|
|
538
|
+
- `portrait` - Exact width will be set, height will be adjusted according to aspect ratio.
|
|
539
|
+
- `landscape` - Exact height will be set, width will be adjusted according to aspect ratio.
|
|
540
|
+
- `auto` - The best strategy (portrait or landscape) will be selected for a given image according to aspect ratio.
|
|
541
|
+
- `crop` - This option will crop your image to the exact size you specify with no distortion.
|
|
266
542
|
|
|
267
543
|
## WebP Compression
|
|
268
544
|
|
|
@@ -270,14 +546,14 @@ WebP is a new image format introduced by Google in 2010 which supports both loss
|
|
|
270
546
|
|
|
271
547
|
To recompress your PNG or JPEG files into WebP format simply set `"webp": true` flag in your request JSON. You can also optionally set `"lossy": true` flag to leverage WebP's lossy compression:
|
|
272
548
|
|
|
273
|
-
|
|
549
|
+
```javascript
|
|
274
550
|
var opts = {
|
|
275
551
|
file: '/path/to/image/file.jpg',
|
|
276
552
|
wait: true,
|
|
277
553
|
webp: true,
|
|
278
554
|
lossy: true
|
|
279
|
-
}
|
|
280
|
-
|
|
555
|
+
}
|
|
556
|
+
```
|
|
281
557
|
|
|
282
558
|
## Amazon S3 and Rackspace Cloud Files
|
|
283
559
|
|
|
@@ -286,24 +562,26 @@ Kraken API allows you to store optimized images directly in your S3 bucket or Cl
|
|
|
286
562
|
### Amazon S3
|
|
287
563
|
|
|
288
564
|
**Mandatory Parameters:**
|
|
289
|
-
|
|
290
|
-
-
|
|
291
|
-
-
|
|
292
|
-
-
|
|
565
|
+
|
|
566
|
+
- `key` - Your unique Amazon "Access Key ID".
|
|
567
|
+
- `secret` - Your unique Amazon "Secret Access Key".
|
|
568
|
+
- `bucket` - Name of a destination container on your Amazon S3 account.
|
|
569
|
+
- `region` - Name of the region your S3 bucket is located in.
|
|
293
570
|
|
|
294
571
|
**Optional Parameters:**
|
|
295
|
-
|
|
296
|
-
-
|
|
572
|
+
|
|
573
|
+
- `path` - Destination path in your S3 bucket (e.g. `"images/layout/header.jpg"`). Defaults to root `"/"`.
|
|
574
|
+
- `acl` - Permissions of a destination object. This can be `"public_read"` or `"private"`. Defaults to `"public_read"`.
|
|
297
575
|
|
|
298
576
|
The above parameters must be passed in a `s3_store` object:
|
|
299
577
|
|
|
300
|
-
|
|
301
|
-
var Kraken = require('kraken')
|
|
578
|
+
```javascript
|
|
579
|
+
var Kraken = require('kraken')
|
|
302
580
|
|
|
303
581
|
var kraken = new Kraken({
|
|
304
582
|
api_key: 'your-api-key',
|
|
305
583
|
api_secret: 'your-api-secret'
|
|
306
|
-
})
|
|
584
|
+
})
|
|
307
585
|
|
|
308
586
|
var opts = {
|
|
309
587
|
file: '/path/to/image/file.jpg',
|
|
@@ -314,44 +592,56 @@ var opts = {
|
|
|
314
592
|
bucket: 'destination-bucket',
|
|
315
593
|
region: 'us-east-1'
|
|
316
594
|
}
|
|
317
|
-
}
|
|
595
|
+
}
|
|
318
596
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
597
|
+
// with callback
|
|
598
|
+
kraken.upload(opts, function (err, data) {
|
|
599
|
+
if (err) {
|
|
600
|
+
console.log('Failed. Error message: %s', err)
|
|
322
601
|
} else {
|
|
323
|
-
console.log('
|
|
602
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
324
603
|
}
|
|
325
|
-
})
|
|
326
|
-
|
|
604
|
+
})
|
|
605
|
+
|
|
606
|
+
// with promise
|
|
607
|
+
kraken.upload(opts)
|
|
608
|
+
.then(data => {
|
|
609
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
610
|
+
})
|
|
611
|
+
.catch(err => {
|
|
612
|
+
console.log('Failed. Error message: %s', err)
|
|
613
|
+
})
|
|
614
|
+
```
|
|
327
615
|
|
|
328
616
|
The `data` object will contain `kraked_url` key pointing directly to the optimized file in your Amazon S3 account:
|
|
329
617
|
|
|
330
|
-
|
|
618
|
+
```javascript
|
|
331
619
|
{
|
|
332
|
-
kraked_url:
|
|
620
|
+
kraked_url: 'http://s3.amazonaws.com/YOUR_CONTAINER/path/to/file.jpg'
|
|
333
621
|
}
|
|
334
|
-
|
|
622
|
+
```
|
|
335
623
|
|
|
336
624
|
### Rackspace Cloud Files
|
|
337
625
|
|
|
338
626
|
**Mandatory Parameters:**
|
|
339
|
-
|
|
340
|
-
-
|
|
341
|
-
-
|
|
627
|
+
|
|
628
|
+
- `user` - Your Rackspace username.
|
|
629
|
+
- `key` - Your unique Cloud Files API Key.
|
|
630
|
+
- `container` - Name of a destination container on your Cloud Files account.
|
|
342
631
|
|
|
343
632
|
**Optional Parameters:**
|
|
344
|
-
|
|
633
|
+
|
|
634
|
+
- `path` - Destination path in your container (e.g. `"images/layout/header.jpg"`). Defaults to root `"/"`.
|
|
345
635
|
|
|
346
636
|
The above parameters must be passed in a `cf_store` object:
|
|
347
637
|
|
|
348
|
-
|
|
349
|
-
var Kraken = require('kraken')
|
|
638
|
+
```javascript
|
|
639
|
+
var Kraken = require('kraken')
|
|
350
640
|
|
|
351
641
|
var kraken = new Kraken({
|
|
352
642
|
api_key: 'your-api-key',
|
|
353
643
|
api_secret: 'your-api-secret'
|
|
354
|
-
})
|
|
644
|
+
})
|
|
355
645
|
|
|
356
646
|
var opts = {
|
|
357
647
|
file: '/path/to/image/file.jpg',
|
|
@@ -361,50 +651,46 @@ var opts = {
|
|
|
361
651
|
key: 'your-rackspace-api-key',
|
|
362
652
|
container: 'destination-container'
|
|
363
653
|
}
|
|
364
|
-
}
|
|
654
|
+
}
|
|
365
655
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
656
|
+
|
|
657
|
+
// with callback
|
|
658
|
+
kraken.upload(opts, function (err, data) {
|
|
659
|
+
if (err) {
|
|
660
|
+
console.log('Failed. Error message: %s', err)
|
|
369
661
|
} else {
|
|
370
|
-
console.log('
|
|
662
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
371
663
|
}
|
|
372
|
-
})
|
|
373
|
-
|
|
664
|
+
})
|
|
665
|
+
|
|
666
|
+
// with promise
|
|
667
|
+
kraken.upload(opts)
|
|
668
|
+
.then(data => {
|
|
669
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
670
|
+
})
|
|
671
|
+
.catch(err => {
|
|
672
|
+
console.log('Failed. Error message: %s', err)
|
|
673
|
+
})
|
|
674
|
+
```
|
|
374
675
|
|
|
375
676
|
If your container is CDN-enabled, the optimization results will contain `kraked_url` which points directly to the optimized file location in your Cloud Files account, for example:
|
|
376
677
|
|
|
377
|
-
|
|
378
|
-
kraked_url:
|
|
379
|
-
|
|
678
|
+
```javascript
|
|
679
|
+
kraked_url: 'http://e9ffc04970a269a54eeb-cc00fdd2d4f11dffd931005c9e8de53a.r2.cf1.rackcdn.com/path/to/file.jpg'
|
|
680
|
+
```
|
|
380
681
|
|
|
381
682
|
If your container is not CDN-enabled `kraked_url` will point to the optimized image URL in the Kraken API:
|
|
382
683
|
|
|
383
|
-
|
|
384
|
-
kraked_url:
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
Copyright (c) 2013 Nekkra UG
|
|
390
|
-
|
|
391
|
-
Permission is hereby granted, free of charge, to any person
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
copies of the Software, and to permit persons to whom the
|
|
397
|
-
Software is furnished to do so, subject to the following
|
|
398
|
-
conditions:
|
|
399
|
-
|
|
400
|
-
The above copyright notice and this permission notice shall be
|
|
401
|
-
included in all copies or substantial portions of the Software.
|
|
402
|
-
|
|
403
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
404
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
405
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
406
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
407
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
408
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
409
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
410
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
|
684
|
+
```javascript
|
|
685
|
+
kraked_url: 'http://dl.kraken.io/ecdfa5c55d5668b1b5fe9e420554c4ee/file.jpg'
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
# LICENSE - MIT
|
|
689
|
+
|
|
690
|
+
Copyright (c) 2013-2021 Nekkra UG
|
|
691
|
+
|
|
692
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
693
|
+
|
|
694
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
695
|
+
|
|
696
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|