kraken 0.4.0 → 1.0.4
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 +41 -0
- package/.github/workflows/npm-publish.yml +50 -0
- package/.jshintrc +20 -18
- package/LICENSE +9 -0
- package/README.md +569 -142
- 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 -104
package/README.md
CHANGED
|
@@ -1,31 +1,50 @@
|
|
|
1
|
-
Node.js module for Kraken.io API
|
|
2
|
-
===========
|
|
1
|
+
# Node.js module for Kraken.io API
|
|
3
2
|
|
|
4
|
-
[](https://github.com/kraken-io/kraken-node/actions/workflows/node.js.yml)
|
|
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
|
+
- [PDF Compression](#pdf-compression)
|
|
29
|
+
- [Amazon S3, Rackspace Cloud Files, and Google Cloud Storage](#amazon-s3-rackspace-cloud-files-and-google-cloud-storage)
|
|
30
|
+
- [Amazon S3](#amazon-s3)
|
|
31
|
+
- [Rackspace Cloud Files](#rackspace-cloud-files)
|
|
32
|
+
- [Google Cloud Storage](#google-cloud-storage)
|
|
33
|
+
- [LICENSE - MIT](#license---mit)
|
|
24
34
|
|
|
25
35
|
## Installation
|
|
26
36
|
|
|
27
37
|
npm install kraken --save
|
|
28
38
|
|
|
39
|
+
## Requirements
|
|
40
|
+
|
|
41
|
+
- Minimum NodeJS version: `12.20.0`
|
|
42
|
+
|
|
43
|
+
## Dependencies
|
|
44
|
+
|
|
45
|
+
- [`form-data`](https://www.npmjs.com/package/form-data)
|
|
46
|
+
- [`axios`](https://www.npmjs.com/package/axios)
|
|
47
|
+
|
|
29
48
|
## Getting Started
|
|
30
49
|
|
|
31
50
|
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 +69,7 @@ With the `wait` option turned on for every request to the API, the connection wi
|
|
|
50
69
|
|
|
51
70
|
**Request:**
|
|
52
71
|
|
|
53
|
-
|
|
72
|
+
```javascript
|
|
54
73
|
{
|
|
55
74
|
"auth": {
|
|
56
75
|
"api_key": "your-api-key",
|
|
@@ -59,11 +78,11 @@ With the `wait` option turned on for every request to the API, the connection wi
|
|
|
59
78
|
"url": "http://awesome-website.com/images/header.jpg",
|
|
60
79
|
"wait": true
|
|
61
80
|
}
|
|
62
|
-
|
|
81
|
+
```
|
|
63
82
|
|
|
64
83
|
**Response**
|
|
65
84
|
|
|
66
|
-
|
|
85
|
+
```javascript
|
|
67
86
|
{
|
|
68
87
|
"success": true,
|
|
69
88
|
"file_name": "header.jpg",
|
|
@@ -72,17 +91,17 @@ With the `wait` option turned on for every request to the API, the connection wi
|
|
|
72
91
|
"saved_bytes": 159162,
|
|
73
92
|
"kraked_url": "http://dl.kraken.io/d1aacd2a2280c2ffc7b4906a09f78f46/header.jpg"
|
|
74
93
|
}
|
|
75
|
-
|
|
94
|
+
```
|
|
76
95
|
|
|
77
96
|
### Callback URL
|
|
78
97
|
|
|
79
98
|
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
99
|
|
|
81
|
-
We recommend [
|
|
100
|
+
We recommend [RequestBin.com](http://requestbin.com) as an easy way to capture optimization results for initial testing.
|
|
82
101
|
|
|
83
102
|
**Request:**
|
|
84
103
|
|
|
85
|
-
|
|
104
|
+
```javascript
|
|
86
105
|
{
|
|
87
106
|
"auth": {
|
|
88
107
|
"api_key": "your-api-key",
|
|
@@ -91,19 +110,19 @@ We recommend [requestb.in](http://requestb.in) as an easy way to capture optimiz
|
|
|
91
110
|
"url": "http://image-url.com/file.jpg",
|
|
92
111
|
"callback_url": "http://awesome-website.com/kraken_results"
|
|
93
112
|
}
|
|
94
|
-
|
|
113
|
+
```
|
|
95
114
|
|
|
96
115
|
**Response:**
|
|
97
116
|
|
|
98
|
-
|
|
117
|
+
```javascript
|
|
99
118
|
{
|
|
100
119
|
"id": "18fede37617a787649c3f60b9f1f280d"
|
|
101
120
|
}
|
|
102
|
-
|
|
121
|
+
```
|
|
103
122
|
|
|
104
123
|
**Results posted to the Callback URL:**
|
|
105
124
|
|
|
106
|
-
|
|
125
|
+
```javascript
|
|
107
126
|
{
|
|
108
127
|
"id": "18fede37617a787649c3f60b9f1f280d"
|
|
109
128
|
"success": true,
|
|
@@ -113,50 +132,101 @@ We recommend [requestb.in](http://requestb.in) as an easy way to capture optimiz
|
|
|
113
132
|
"saved_bytes": 159162,
|
|
114
133
|
"kraked_url": "http://dl.kraken.io/18fede37617a787649c3f60b9f1f280d/file.jpg"
|
|
115
134
|
}
|
|
116
|
-
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Promises
|
|
138
|
+
|
|
139
|
+
All Kraken.io API methods returns promises and `cb` argument is optional:
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
var Kraken = require('kraken')
|
|
143
|
+
|
|
144
|
+
var kraken = new Kraken({
|
|
145
|
+
api_key: 'your-api-key',
|
|
146
|
+
api_secret: 'your-api-secret'
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
kraken
|
|
150
|
+
.userStatus()
|
|
151
|
+
.then((status) => console.log('Kraken user status:', status))
|
|
152
|
+
.catch((error) =>
|
|
153
|
+
console.error('Error while requesting user status:', error)
|
|
154
|
+
)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Async/Await
|
|
158
|
+
|
|
159
|
+
All Kraken.io API methods support async/await syntax:
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
var Kraken = require('kraken')
|
|
163
|
+
|
|
164
|
+
var kraken = new Kraken({
|
|
165
|
+
api_key: 'your-api-key',
|
|
166
|
+
api_secret: 'your-api-secret'
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
async function exampleAsynFunction() {
|
|
170
|
+
let result = await kraken.userStatus()
|
|
171
|
+
console.log('Kraken user status:', result)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
exampleAsynFunction()
|
|
175
|
+
```
|
|
117
176
|
|
|
118
177
|
## Authentication
|
|
119
178
|
|
|
120
179
|
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
180
|
|
|
122
|
-
|
|
123
|
-
var Kraken = require('kraken')
|
|
181
|
+
```javascript
|
|
182
|
+
var Kraken = require('kraken')
|
|
124
183
|
|
|
125
184
|
var kraken = new Kraken({
|
|
126
185
|
api_key: 'your-api-key',
|
|
127
186
|
api_secret: 'your-api-secret'
|
|
128
|
-
})
|
|
129
|
-
|
|
187
|
+
})
|
|
188
|
+
```
|
|
130
189
|
|
|
131
190
|
## Usage - Image URL
|
|
132
191
|
|
|
133
192
|
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
193
|
|
|
135
|
-
|
|
136
|
-
var Kraken = require('kraken')
|
|
194
|
+
```javascript
|
|
195
|
+
var Kraken = require('kraken')
|
|
137
196
|
|
|
138
197
|
var kraken = new Kraken({
|
|
139
198
|
api_key: 'your-api-key',
|
|
140
199
|
api_secret: 'your-api-secret'
|
|
141
|
-
})
|
|
200
|
+
})
|
|
142
201
|
|
|
143
202
|
var opts = {
|
|
144
203
|
url: 'http://image-url.com/file.jpg',
|
|
145
204
|
wait: true
|
|
146
|
-
}
|
|
205
|
+
}
|
|
147
206
|
|
|
148
|
-
|
|
207
|
+
// with callbacks
|
|
208
|
+
kraken.url(opts, function (err, data) {
|
|
149
209
|
if (err) {
|
|
150
|
-
console.log('Failed. Error message: %s', err)
|
|
210
|
+
console.log('Failed. Error message: %s', err)
|
|
151
211
|
} else {
|
|
152
|
-
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
212
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
153
213
|
}
|
|
154
|
-
})
|
|
155
|
-
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
// with promises
|
|
217
|
+
kraken
|
|
218
|
+
.url(opts)
|
|
219
|
+
.then((data) => {
|
|
220
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
221
|
+
})
|
|
222
|
+
.catch((err) => {
|
|
223
|
+
console.log('Failed. Error message: %s', err)
|
|
224
|
+
})
|
|
225
|
+
```
|
|
156
226
|
|
|
157
227
|
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
228
|
|
|
159
|
-
|
|
229
|
+
```javascript
|
|
160
230
|
{
|
|
161
231
|
success: true,
|
|
162
232
|
file_name: 'file.jpg',
|
|
@@ -165,45 +235,247 @@ Depending on a choosen response option (Wait or Callback URL) in the `data` obje
|
|
|
165
235
|
saved_bytes: 30087,
|
|
166
236
|
kraked_url: 'http://dl.kraken.io/d1aacd2a2280c2ffc7b4906a09f78f46/file.jpg'
|
|
167
237
|
}
|
|
168
|
-
|
|
238
|
+
```
|
|
169
239
|
|
|
170
240
|
If no saving were found, the API will return an object containing `"success":true` and will report saved_bytes of 0:
|
|
171
241
|
|
|
172
|
-
|
|
242
|
+
```javascript
|
|
173
243
|
{
|
|
174
244
|
success: true,
|
|
175
245
|
saved_bytes: 0,
|
|
176
246
|
}
|
|
177
|
-
|
|
247
|
+
```
|
|
178
248
|
|
|
179
249
|
## Usage - Image Upload
|
|
180
250
|
|
|
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`
|
|
251
|
+
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`.
|
|
252
|
+
|
|
253
|
+
`file` parameter have next options:
|
|
254
|
+
|
|
255
|
+
- `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
|
|
256
|
+
- `Stream` | `Buffer` | `String` - file content, will be generated random filename
|
|
257
|
+
- `Object` with next properties:
|
|
258
|
+
- _mandatory_ `path` or `value` - if `path` is present, `value` is ignored
|
|
259
|
+
- `path` - path to file, will be loaded using `fs.createReadStream()` function
|
|
260
|
+
- `value` - file content, supported types: `Stream` | `Buffer` | `String`
|
|
261
|
+
- [`filename`] - new file name to be sended to server, this parameter allow to send different file name to server
|
|
182
262
|
|
|
183
263
|
In the `data` object you will find the same optimization properties as with `url` option above.
|
|
184
264
|
|
|
185
|
-
|
|
265
|
+
```javascript
|
|
186
266
|
var Kraken = require('kraken'),
|
|
187
|
-
fs = require('fs')
|
|
267
|
+
fs = require('fs')
|
|
188
268
|
|
|
189
269
|
var kraken = new Kraken({
|
|
190
270
|
api_key: 'your-api-key',
|
|
191
271
|
api_secret: 'your-api-secret'
|
|
192
|
-
})
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
var opts = {
|
|
275
|
+
file: 'file.jpg',
|
|
276
|
+
wait: true
|
|
277
|
+
}
|
|
193
278
|
|
|
279
|
+
// with callback
|
|
280
|
+
kraken.upload(opts, function (err, data) {
|
|
281
|
+
if (err) {
|
|
282
|
+
console.log('Failed. Error message: %s', err)
|
|
283
|
+
} else {
|
|
284
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
285
|
+
}
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
// with promise
|
|
289
|
+
kraken
|
|
290
|
+
.upload(opts)
|
|
291
|
+
.then((data) => {
|
|
292
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
293
|
+
})
|
|
294
|
+
.catch((err) => {
|
|
295
|
+
console.log('Failed. Error message: %s', err)
|
|
296
|
+
})
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Buffer as file, filename is ommited - it will be random:
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
var opts = {
|
|
303
|
+
file: fs.readFileSync('file.jpg'),
|
|
304
|
+
wait: true
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// with callback
|
|
308
|
+
kraken.upload(opts, function (err, data) {
|
|
309
|
+
if (err) {
|
|
310
|
+
console.log('Failed. Error message: %s', err)
|
|
311
|
+
} else {
|
|
312
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
313
|
+
}
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
// with promise
|
|
317
|
+
kraken
|
|
318
|
+
.upload(opts)
|
|
319
|
+
.then((data) => {
|
|
320
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
321
|
+
})
|
|
322
|
+
.catch((err) => {
|
|
323
|
+
console.log('Failed. Error message: %s', err)
|
|
324
|
+
})
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Stream as file, filename is ommited - it will be random:
|
|
328
|
+
|
|
329
|
+
```javascript
|
|
194
330
|
var opts = {
|
|
195
331
|
file: fs.createReadStream('file.jpg'),
|
|
196
332
|
wait: true
|
|
197
|
-
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// with callback
|
|
336
|
+
kraken.upload(opts, function (err, data) {
|
|
337
|
+
if (err) {
|
|
338
|
+
console.log('Failed. Error message: %s', err)
|
|
339
|
+
} else {
|
|
340
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
341
|
+
}
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
// with promise
|
|
345
|
+
kraken
|
|
346
|
+
.upload(opts)
|
|
347
|
+
.then((data) => {
|
|
348
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
349
|
+
})
|
|
350
|
+
.catch((err) => {
|
|
351
|
+
console.log('Failed. Error message: %s', err)
|
|
352
|
+
})
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
Object as file options with path parameter:
|
|
356
|
+
|
|
357
|
+
```javascript
|
|
358
|
+
var opts = {
|
|
359
|
+
file: {
|
|
360
|
+
path: 'file.jpg',
|
|
361
|
+
fileName: 'example.jpg'
|
|
362
|
+
},
|
|
363
|
+
wait: true
|
|
364
|
+
}
|
|
198
365
|
|
|
366
|
+
// with callback
|
|
199
367
|
kraken.upload(opts, function (err, data) {
|
|
200
368
|
if (err) {
|
|
201
|
-
console.log('Failed. Error message: %s', err)
|
|
369
|
+
console.log('Failed. Error message: %s', err)
|
|
202
370
|
} else {
|
|
203
|
-
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
371
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
372
|
+
}
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
// with promise
|
|
376
|
+
kraken
|
|
377
|
+
.upload(opts)
|
|
378
|
+
.then((data) => {
|
|
379
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
380
|
+
})
|
|
381
|
+
.catch((err) => {
|
|
382
|
+
console.log('Failed. Error message: %s', err)
|
|
383
|
+
})
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Object as file options with buffer as value:
|
|
387
|
+
|
|
388
|
+
```javascript
|
|
389
|
+
var opts = {
|
|
390
|
+
file: {
|
|
391
|
+
value: fs.readFileSync('file.jpg'),
|
|
392
|
+
fileName: 'new_file_name.jpg'
|
|
204
393
|
}
|
|
205
|
-
|
|
206
|
-
|
|
394
|
+
wait: true
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// with callback
|
|
398
|
+
kraken.upload(opts, function (err, data) {
|
|
399
|
+
if (err) {
|
|
400
|
+
console.log('Failed. Error message: %s', err)
|
|
401
|
+
} else {
|
|
402
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
403
|
+
}
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
// with promise
|
|
407
|
+
kraken.upload(opts)
|
|
408
|
+
.then(data => {
|
|
409
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
410
|
+
})
|
|
411
|
+
.catch(err => {
|
|
412
|
+
console.log('Failed. Error message: %s', err)
|
|
413
|
+
})
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
## Usage - User status
|
|
417
|
+
|
|
418
|
+
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:
|
|
419
|
+
|
|
420
|
+
```json
|
|
421
|
+
{
|
|
422
|
+
"success": true,
|
|
423
|
+
"active": true,
|
|
424
|
+
"plan_name": "Enterprise",
|
|
425
|
+
"quota_total": 64424509440,
|
|
426
|
+
"quota_used": 313271610,
|
|
427
|
+
"quota_remaining": 64111237830
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
```javascript
|
|
432
|
+
var Kraken = require('kraken')
|
|
433
|
+
var kraken = new Kraken({
|
|
434
|
+
api_key: 'your-api-key',
|
|
435
|
+
api_secret: 'your-api-secret'
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
// with callback
|
|
439
|
+
kraken.userStatus(function (err, data) {
|
|
440
|
+
if (err) {
|
|
441
|
+
console.log('Failed. Error message: %s', err)
|
|
442
|
+
} else {
|
|
443
|
+
console.log('Success', {
|
|
444
|
+
success: data.success,
|
|
445
|
+
active: data.active,
|
|
446
|
+
quota_total: formatBytes(data.quota_total, 2),
|
|
447
|
+
quota_used: formatBytes(data.quota_used, 2),
|
|
448
|
+
quota_remaining: formatBytes(data.quota_remaining, 2)
|
|
449
|
+
})
|
|
450
|
+
}
|
|
451
|
+
}) //formatBytes(data,2)
|
|
452
|
+
|
|
453
|
+
// with promise
|
|
454
|
+
kraken
|
|
455
|
+
.userStatus()
|
|
456
|
+
.then((data) => {
|
|
457
|
+
console.log('Success', {
|
|
458
|
+
success: data.success,
|
|
459
|
+
active: data.active,
|
|
460
|
+
quota_total: formatBytes(data.quota_total, 2),
|
|
461
|
+
quota_used: formatBytes(data.quota_used, 2),
|
|
462
|
+
quota_remaining: formatBytes(data.quota_remaining, 2)
|
|
463
|
+
})
|
|
464
|
+
})
|
|
465
|
+
.catch((err) => {
|
|
466
|
+
console.log('Failed. Error message: %s', err)
|
|
467
|
+
}) //formatBytes(data,2)
|
|
468
|
+
|
|
469
|
+
// Optional function which will format the bytes value
|
|
470
|
+
function formatBytes(value, decimal) {
|
|
471
|
+
if (0 == value) return '0 Bytes'
|
|
472
|
+
var c = 1024,
|
|
473
|
+
d = decimal || 2,
|
|
474
|
+
e = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
|
|
475
|
+
f = Math.floor(Math.log(value) / Math.log(c))
|
|
476
|
+
return parseFloat((value / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
|
|
477
|
+
}
|
|
478
|
+
```
|
|
207
479
|
|
|
208
480
|
## Lossy Optimization
|
|
209
481
|
|
|
@@ -211,31 +483,33 @@ When you decide to sacrifice just a small amount of image quality (usually unnot
|
|
|
211
483
|
|
|
212
484
|
To use lossy optimizations simply set `lossy: true` in your request:
|
|
213
485
|
|
|
214
|
-
|
|
486
|
+
```javascript
|
|
215
487
|
var opts = {
|
|
216
488
|
file: '/path/to/image/file.jpg',
|
|
217
489
|
lossy: true,
|
|
218
490
|
wait: true
|
|
219
|
-
}
|
|
220
|
-
|
|
491
|
+
}
|
|
492
|
+
```
|
|
221
493
|
|
|
222
494
|
### PNG Images
|
|
495
|
+
|
|
223
496
|
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
497
|
|
|
225
498
|
### JPEG Images
|
|
499
|
+
|
|
226
500
|
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
501
|
|
|
228
502
|
## Image Resizing
|
|
229
503
|
|
|
230
504
|
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
505
|
|
|
232
|
-
|
|
233
|
-
var Kraken = require('kraken')
|
|
506
|
+
```javascript
|
|
507
|
+
var Kraken = require('kraken')
|
|
234
508
|
|
|
235
509
|
var kraken = new Kraken({
|
|
236
510
|
api_key: 'your-api-key',
|
|
237
511
|
api_secret: 'your-api-secret'
|
|
238
|
-
})
|
|
512
|
+
})
|
|
239
513
|
|
|
240
514
|
var opts = {
|
|
241
515
|
file: '/path/to/image/file.jpg',
|
|
@@ -245,65 +519,125 @@ var opts = {
|
|
|
245
519
|
height: 75,
|
|
246
520
|
strategy: 'crop'
|
|
247
521
|
}
|
|
248
|
-
}
|
|
522
|
+
}
|
|
249
523
|
|
|
250
|
-
|
|
524
|
+
// with callback
|
|
525
|
+
kraken.upload(opts, function (err, data) {
|
|
251
526
|
if (err) {
|
|
252
|
-
console.log('Failed. Error message: %s', err)
|
|
527
|
+
console.log('Failed. Error message: %s', err)
|
|
253
528
|
} else {
|
|
254
|
-
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
529
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
255
530
|
}
|
|
256
|
-
})
|
|
257
|
-
|
|
531
|
+
})
|
|
532
|
+
|
|
533
|
+
// with promise
|
|
534
|
+
kraken
|
|
535
|
+
.upload(opts)
|
|
536
|
+
.then((data) => {
|
|
537
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
538
|
+
})
|
|
539
|
+
.catch((err) => {
|
|
540
|
+
console.log('Failed. Error message: %s', err)
|
|
541
|
+
})
|
|
542
|
+
```
|
|
258
543
|
|
|
259
544
|
The `strategy` property can have one of the following values:
|
|
260
545
|
|
|
261
|
-
-
|
|
262
|
-
-
|
|
263
|
-
-
|
|
264
|
-
-
|
|
265
|
-
-
|
|
546
|
+
- `exact` - Resize by exact width/height. No aspect ratio will be maintained.
|
|
547
|
+
- `portrait` - Exact height will be set, width will be adjusted according to aspect ratio.
|
|
548
|
+
- `landscape` - Exact width will be set, height will be adjusted according to aspect ratio.
|
|
549
|
+
- `auto` - The best strategy (portrait or landscape) will be selected for a given image according to aspect ratio.
|
|
550
|
+
- `crop` - This option will crop your image to the exact size you specify with no distortion.
|
|
266
551
|
|
|
267
552
|
## WebP Compression
|
|
268
553
|
|
|
269
554
|
WebP is a new image format introduced by Google in 2010 which supports both lossy and lossless compression. According to [Google](https://developers.google.com/speed/webp/), WebP lossless images are **26% smaller** in size compared to PNGs and WebP lossy images are **25-34% smaller** in size compared to JPEG images.
|
|
270
555
|
|
|
271
|
-
|
|
556
|
+
WebP's lossy compression:WebP compression no longer requires setting `"webp": true` . Instead, you should make a standard optimization request, and Kraken.io will handle WebP conversion based on your configured settings.
|
|
557
|
+
|
|
558
|
+
### Convert to WebP format
|
|
272
559
|
|
|
273
|
-
|
|
560
|
+
```javascript
|
|
274
561
|
var opts = {
|
|
275
562
|
file: '/path/to/image/file.jpg',
|
|
276
563
|
wait: true,
|
|
277
|
-
|
|
564
|
+
lossy: true,
|
|
565
|
+
convert: {
|
|
566
|
+
format: 'webp'
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
### Optimize WebP Image
|
|
572
|
+
|
|
573
|
+
```javascript
|
|
574
|
+
var opts = {
|
|
575
|
+
file: '/path/to/image/file.webp',
|
|
576
|
+
wait: true,
|
|
278
577
|
lossy: true
|
|
279
|
-
}
|
|
280
|
-
|
|
578
|
+
}
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
## PDF Compression
|
|
582
|
+
|
|
583
|
+
Kraken.io now supports PDF compression, allowing you to optimize PDF files with minimal loss of quality.
|
|
281
584
|
|
|
282
|
-
|
|
585
|
+
Kraken API automatically determines the optimal compression settings based on the content type of the PDF (e.g., image-heavy, text-heavy, or mixed content). Additionally, users can optionally provide custom parameters to further control the compression process.
|
|
586
|
+
|
|
587
|
+
```javascript
|
|
588
|
+
var opts = {
|
|
589
|
+
file: '/path/to/image/document.pdf',
|
|
590
|
+
wait: true
|
|
591
|
+
}
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
**Optional Parameters:**
|
|
283
595
|
|
|
284
|
-
|
|
596
|
+
[API Docs for PDF Compression](https://kraken.io/docs/pdf-compression)
|
|
597
|
+
|
|
598
|
+
- `level` - Optimization level for the PDF. Available options are `screen`, `ebook`, `printer` and `prepress`. **Overrides DPI values**.
|
|
599
|
+
- `quality` - JPEG quality for embedded images. Acceptable values: **1-100**. Higher values preserve more detail but increase file size. Default is **65**.
|
|
600
|
+
- `dpi` - Resolution for images within the PDF. Default value is calculated based on the PDF type. Adjust this value based on your desired output quality.
|
|
601
|
+
- `downsampleType` - Method used to downsample images. Available options are `bicubic`, `average` and `subsample`
|
|
602
|
+
|
|
603
|
+
```javascript
|
|
604
|
+
var opts = {
|
|
605
|
+
file: '/path/to/image/file.webp',
|
|
606
|
+
wait: true,
|
|
607
|
+
level: 'ebook',
|
|
608
|
+
quality: 60,
|
|
609
|
+
dpi: 150,
|
|
610
|
+
downsampleType: 'bicubic'
|
|
611
|
+
}
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
## Amazon S3, Rackspace Cloud Files and Google Cloud Storage
|
|
615
|
+
|
|
616
|
+
Kraken API allows you to store optimized images directly in your S3 bucket, Google Cloud Storage or Cloud Files container. With just a few additional parameters your optimized images will be pushed to your external storage in no time.
|
|
285
617
|
|
|
286
618
|
### Amazon S3
|
|
287
619
|
|
|
288
620
|
**Mandatory Parameters:**
|
|
289
|
-
|
|
290
|
-
-
|
|
291
|
-
-
|
|
292
|
-
-
|
|
621
|
+
|
|
622
|
+
- `key` - Your unique Amazon "Access Key ID".
|
|
623
|
+
- `secret` - Your unique Amazon "Secret Access Key".
|
|
624
|
+
- `bucket` - Name of a destination container on your Amazon S3 account.
|
|
625
|
+
- `region` - Name of the region your S3 bucket is located in.
|
|
293
626
|
|
|
294
627
|
**Optional Parameters:**
|
|
295
|
-
|
|
296
|
-
-
|
|
628
|
+
|
|
629
|
+
- `path` - Destination path in your S3 bucket (e.g. `"images/layout/header.jpg"`). Defaults to root `"/"`.
|
|
630
|
+
- `acl` - Permissions of a destination object. This can be `"public_read"` or `"private"`. Defaults to `"public_read"`.
|
|
297
631
|
|
|
298
632
|
The above parameters must be passed in a `s3_store` object:
|
|
299
633
|
|
|
300
|
-
|
|
301
|
-
var Kraken = require('kraken')
|
|
634
|
+
```javascript
|
|
635
|
+
var Kraken = require('kraken')
|
|
302
636
|
|
|
303
637
|
var kraken = new Kraken({
|
|
304
638
|
api_key: 'your-api-key',
|
|
305
639
|
api_secret: 'your-api-secret'
|
|
306
|
-
})
|
|
640
|
+
})
|
|
307
641
|
|
|
308
642
|
var opts = {
|
|
309
643
|
file: '/path/to/image/file.jpg',
|
|
@@ -314,44 +648,57 @@ var opts = {
|
|
|
314
648
|
bucket: 'destination-bucket',
|
|
315
649
|
region: 'us-east-1'
|
|
316
650
|
}
|
|
317
|
-
}
|
|
651
|
+
}
|
|
318
652
|
|
|
319
|
-
|
|
653
|
+
// with callback
|
|
654
|
+
kraken.upload(opts, function (err, data) {
|
|
320
655
|
if (err) {
|
|
321
|
-
console.log('Failed. Error message: %s', err)
|
|
656
|
+
console.log('Failed. Error message: %s', err)
|
|
322
657
|
} else {
|
|
323
|
-
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
658
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
324
659
|
}
|
|
325
|
-
})
|
|
326
|
-
|
|
660
|
+
})
|
|
661
|
+
|
|
662
|
+
// with promise
|
|
663
|
+
kraken
|
|
664
|
+
.upload(opts)
|
|
665
|
+
.then((data) => {
|
|
666
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
667
|
+
})
|
|
668
|
+
.catch((err) => {
|
|
669
|
+
console.log('Failed. Error message: %s', err)
|
|
670
|
+
})
|
|
671
|
+
```
|
|
327
672
|
|
|
328
673
|
The `data` object will contain `kraked_url` key pointing directly to the optimized file in your Amazon S3 account:
|
|
329
674
|
|
|
330
|
-
|
|
675
|
+
```javascript
|
|
331
676
|
{
|
|
332
|
-
kraked_url:
|
|
677
|
+
kraked_url: 'http://s3.amazonaws.com/YOUR_CONTAINER/path/to/file.jpg'
|
|
333
678
|
}
|
|
334
|
-
|
|
679
|
+
```
|
|
335
680
|
|
|
336
681
|
### Rackspace Cloud Files
|
|
337
682
|
|
|
338
683
|
**Mandatory Parameters:**
|
|
339
|
-
|
|
340
|
-
-
|
|
341
|
-
-
|
|
684
|
+
|
|
685
|
+
- `user` - Your Rackspace username.
|
|
686
|
+
- `key` - Your unique Cloud Files API Key.
|
|
687
|
+
- `container` - Name of a destination container on your Cloud Files account.
|
|
342
688
|
|
|
343
689
|
**Optional Parameters:**
|
|
344
|
-
|
|
690
|
+
|
|
691
|
+
- `path` - Destination path in your container (e.g. `"images/layout/header.jpg"`). Defaults to root `"/"`.
|
|
345
692
|
|
|
346
693
|
The above parameters must be passed in a `cf_store` object:
|
|
347
694
|
|
|
348
|
-
|
|
349
|
-
var Kraken = require('kraken')
|
|
695
|
+
```javascript
|
|
696
|
+
var Kraken = require('kraken')
|
|
350
697
|
|
|
351
698
|
var kraken = new Kraken({
|
|
352
699
|
api_key: 'your-api-key',
|
|
353
700
|
api_secret: 'your-api-secret'
|
|
354
|
-
})
|
|
701
|
+
})
|
|
355
702
|
|
|
356
703
|
var opts = {
|
|
357
704
|
file: '/path/to/image/file.jpg',
|
|
@@ -361,50 +708,130 @@ var opts = {
|
|
|
361
708
|
key: 'your-rackspace-api-key',
|
|
362
709
|
container: 'destination-container'
|
|
363
710
|
}
|
|
364
|
-
}
|
|
711
|
+
}
|
|
365
712
|
|
|
366
|
-
|
|
713
|
+
// with callback
|
|
714
|
+
kraken.upload(opts, function (err, data) {
|
|
367
715
|
if (err) {
|
|
368
|
-
console.log('Failed. Error message: %s', err)
|
|
716
|
+
console.log('Failed. Error message: %s', err)
|
|
369
717
|
} else {
|
|
370
|
-
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
718
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
371
719
|
}
|
|
372
|
-
})
|
|
373
|
-
|
|
720
|
+
})
|
|
721
|
+
|
|
722
|
+
// with promise
|
|
723
|
+
kraken
|
|
724
|
+
.upload(opts)
|
|
725
|
+
.then((data) => {
|
|
726
|
+
console.log('Success. Optimized image URL: %s', data.kraked_url)
|
|
727
|
+
})
|
|
728
|
+
.catch((err) => {
|
|
729
|
+
console.log('Failed. Error message: %s', err)
|
|
730
|
+
})
|
|
731
|
+
```
|
|
374
732
|
|
|
375
733
|
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
734
|
|
|
377
|
-
|
|
378
|
-
kraked_url:
|
|
379
|
-
|
|
735
|
+
```javascript
|
|
736
|
+
kraked_url: 'http://e9ffc04970a269a54eeb-cc00fdd2d4f11dffd931005c9e8de53a.r2.cf1.rackcdn.com/path/to/file.jpg'
|
|
737
|
+
```
|
|
380
738
|
|
|
381
739
|
If your container is not CDN-enabled `kraked_url` will point to the optimized image URL in the Kraken API:
|
|
382
740
|
|
|
383
|
-
|
|
384
|
-
kraked_url:
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
741
|
+
```javascript
|
|
742
|
+
kraked_url: 'http://dl.kraken.io/ecdfa5c55d5668b1b5fe9e420554c4ee/file.jpg'
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
### Google Cloud Storage
|
|
746
|
+
|
|
747
|
+
Kraken.io API allows you to store optimized images directly in your Google Cloud Storage (GCS) bucket. Follow the steps below to configure your GCS integration and securely store your optimized images.
|
|
748
|
+
|
|
749
|
+
#### Prerequisites:
|
|
750
|
+
|
|
751
|
+
Ensure you have access to Google Cloud Platform (GCP) and an active project where you will store your images.
|
|
752
|
+
|
|
753
|
+
#### Mandatory Parameters:
|
|
754
|
+
|
|
755
|
+
- `gcs_store.bucket` - Name of the destination bucket on your Google Cloud Storage account.
|
|
756
|
+
- `gcs_store.credentials` - Your service account credentials (JSON format) to authenticate with GCS.
|
|
757
|
+
|
|
758
|
+
#### Optional Parameters:
|
|
759
|
+
|
|
760
|
+
- `gcs_store.path` - Destination path in your GCS bucket (e.g., "images/layout/header.jpg"). Defaults to root `/`.
|
|
761
|
+
- `gcs_store.acl` - Permissions of the destination object. This can be "publicRead" or "private". Defaults to "private".
|
|
762
|
+
- `gcs_store.metadata` - Metadata you would like to assign to your GCS object (optimized image).
|
|
763
|
+
|
|
764
|
+
#### Important:
|
|
765
|
+
|
|
766
|
+
Make sure your Google Cloud Storage bucket has the appropriate permissions set to allow Kraken.io API to store images.
|
|
767
|
+
|
|
768
|
+
#### Step 1: Creating a Google Cloud Storage Bucket
|
|
769
|
+
|
|
770
|
+
1. Log in to your Google Cloud Console.
|
|
771
|
+
2. Navigate to the "Storage" section and select "Browser."
|
|
772
|
+
3. Click "Create bucket" and follow the prompts.
|
|
773
|
+
4. Note the bucket name for later use.
|
|
774
|
+
|
|
775
|
+
#### Step 2: Obtaining GCS Credentials
|
|
776
|
+
|
|
777
|
+
To interact with GCS through the API, you need appropriate credentials as a service account key:
|
|
778
|
+
|
|
779
|
+
1. Go to "IAM & Admin" in Google Cloud Console.
|
|
780
|
+
2. Select "Service Accounts" and click "Create Service Account."
|
|
781
|
+
3. Enter a name and description for the service account.
|
|
782
|
+
4. Assign necessary roles like "Storage Object Admin" or "Storage Object Creator."
|
|
783
|
+
5. Click "Create Key" and choose JSON type. This will download a JSON file with your credentials.
|
|
784
|
+
6. Securely store this JSON file. It contains sensitive information that allows access to your GCS resources.
|
|
785
|
+
|
|
786
|
+
#### Step 3: Configuring Your Kraken.io API Request
|
|
787
|
+
|
|
788
|
+
Include the necessary details in your Kraken.io API request to store the optimized image directly in your GCS bucket. Insert the JSON file's contents into the `credentials` property inside `gcs_store`.
|
|
789
|
+
|
|
790
|
+
```javascript
|
|
791
|
+
const fs = require('fs').promises;
|
|
792
|
+
const Kraken = require("kraken");
|
|
793
|
+
|
|
794
|
+
(async () => {
|
|
795
|
+
try {
|
|
796
|
+
const kraken = new Kraken({
|
|
797
|
+
api_key: "your_api_key",
|
|
798
|
+
api_secret: "your_api_secret"
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
const credentials = JSON.parse(await fs.readFile('path/to/your/credentials.json', 'utf8'));
|
|
802
|
+
|
|
803
|
+
const params = {
|
|
804
|
+
url: "https://example.com/image.png",
|
|
805
|
+
wait: true,
|
|
806
|
+
lossy: true,
|
|
807
|
+
gcs_store: {
|
|
808
|
+
acl: "private",
|
|
809
|
+
bucket: "your-bucket-name",
|
|
810
|
+
path: "path/to/your/image.png",
|
|
811
|
+
credentials
|
|
812
|
+
}
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
kraken.url(params, function (status) {
|
|
816
|
+
if (status.success) {
|
|
817
|
+
console.log("Success. Optimized image URL: %s", status.kraked_url);
|
|
818
|
+
} else {
|
|
819
|
+
console.log("Fail. Error message: %s", status.message);
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
} catch (error) {
|
|
823
|
+
console.error("Error:", error);
|
|
824
|
+
}
|
|
825
|
+
})();
|
|
826
|
+
}
|
|
827
|
+
```
|
|
828
|
+
|
|
829
|
+
# LICENSE - MIT
|
|
830
|
+
|
|
831
|
+
Copyright (c) 2013-2021 Nekkra UG
|
|
832
|
+
|
|
833
|
+
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:
|
|
834
|
+
|
|
835
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
836
|
+
|
|
837
|
+
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.
|