1 | var merge = require('merge')
|
2 | var request = require('./request')
|
3 |
|
4 | module.exports = Imaginary
|
5 |
|
6 | function Imaginary(image, url) {
|
7 | this.url = url
|
8 | this.image = image
|
9 | this.params = {}
|
10 | }
|
11 |
|
12 | Imaginary.prototype.server =
|
13 | Imaginary.prototype.balance = function (url) {
|
14 | this.url = url
|
15 | return this
|
16 | }
|
17 |
|
18 | Imaginary.prototype.image = function (image) {
|
19 | this.image = image
|
20 | return this
|
21 | }
|
22 |
|
23 | Imaginary.prototype.key = function (key) {
|
24 | this.params.key = key
|
25 | return this
|
26 | }
|
27 |
|
28 | Imaginary.prototype.imageUrl = function (url) {
|
29 | this.params.url = url
|
30 | return this
|
31 | }
|
32 |
|
33 | Imaginary.prototype.params = function (params) {
|
34 | merge(this.params, params)
|
35 | return this
|
36 | }
|
37 |
|
38 | Imaginary.prototype.crop = function (params, cb) {
|
39 | return this.process('crop', params, cb)
|
40 | }
|
41 |
|
42 | Imaginary.prototype.convert = function (params, cb) {
|
43 | return this.process('convert', params, cb)
|
44 | }
|
45 |
|
46 | Imaginary.prototype.resize = function (params, cb) {
|
47 | return this.process('resize', params, cb)
|
48 | }
|
49 |
|
50 | Imaginary.prototype.enlarge = function (params, cb) {
|
51 | return this.process('enlarge', params, cb)
|
52 | }
|
53 |
|
54 | Imaginary.prototype.extract = function (params, cb) {
|
55 | return this.process('extract', params, cb)
|
56 | }
|
57 |
|
58 | Imaginary.prototype.info = function (params, cb) {
|
59 | return this.process('info', params, cb)
|
60 | }
|
61 |
|
62 | Imaginary.prototype.embed = function (params, cb) {
|
63 | return this.process('embed', params, cb)
|
64 | }
|
65 |
|
66 | Imaginary.prototype.rotate = function (params, cb) {
|
67 | return this.process('rotate', params, cb)
|
68 | }
|
69 |
|
70 | Imaginary.prototype.flip = function (params, cb) {
|
71 | return this.process('flip', params, cb)
|
72 | }
|
73 |
|
74 | Imaginary.prototype.flop = function (params, cb) {
|
75 | return this.process('flop', params, cb)
|
76 | }
|
77 |
|
78 | Imaginary.prototype.thumbnail = function (params, cb) {
|
79 | return this.process('thumbnail', params, cb)
|
80 | }
|
81 |
|
82 | Imaginary.prototype.zoom = function (params, cb) {
|
83 | return this.process('zoom', params, cb)
|
84 | }
|
85 |
|
86 | Imaginary.prototype.watermark = function (params, cb) {
|
87 | return this.process('watermark', params, cb)
|
88 | }
|
89 |
|
90 | Imaginary.prototype.health = function (cb) {
|
91 | this.params.get = true
|
92 | return this.process('health', null, cb)
|
93 | }
|
94 |
|
95 | Imaginary.prototype.versions = function (cb) {
|
96 | this.params.get = true
|
97 | return this.process('', null, cb)
|
98 | }
|
99 |
|
100 | Imaginary.prototype.process = function (action, params, cb) {
|
101 | var baseUrl = this.url
|
102 | if (Array.isArray(baseUrl)) {
|
103 | baseUrl = permute(baseUrl)
|
104 | }
|
105 |
|
106 | if (!baseUrl) {
|
107 | throw new Error('Missing imaginary server URL')
|
108 | }
|
109 |
|
110 | var url = baseUrl + '/' + action
|
111 | var opts = merge({}, this.params, params)
|
112 | return request(url, opts, this.image, cb)
|
113 | }
|
114 |
|
115 | function permute(arr) {
|
116 | var item = arr.shift()
|
117 | arr.push(item)
|
118 | return item
|
119 | }
|