imagekitio 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e09b7b187b340674b67ffcada8c8648040462725fb5c35d11757115a50ac4cbb
4
+ data.tar.gz: d9643020bdb3cf25fcb0e7d25ab7253f961e4a4f7fd404bd137ef5ae0287b99a
5
+ SHA512:
6
+ metadata.gz: d7f186c8b43355e058da9d5b1d68230250a1ed1a278b99c34cbdafb39d12363d443f78d0ac90127715d8ab26aee9c17434ac568a3a5fc6ace8f7852c49ba337d
7
+ data.tar.gz: c68fd18c7e30530ecdf32dbd63617448d930cbf11a98a185d9aed785c44966449e98b8d54491a35894113f2218c2e635b9424ce05cc4bd73abb22e0c40095ad8
@@ -0,0 +1,517 @@
1
+ # Imagekit
2
+
3
+ [![Ruby Test](https://github.com/imagekit-developer/imagekit-ruby/workflows/Ruby%20Test/badge.svg)](https://github.com/imagekit-developer/imagekit-ruby)
4
+ [![Gem Version](https://badge.fury.io/rb/imagekitio.svg)](https://badge.fury.io/rb/imagekitio)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![Twitter Follow](https://img.shields.io/twitter/follow/imagekitio?label=Follow&style=social)](https://twitter.com/ImagekitIo)
7
+
8
+ ## Installation
9
+
10
+ If you want to create new rails application, then use this command
11
+
12
+ ```bash
13
+ # New application with default Sqlite3 database
14
+ rails new <your_application_name>
15
+
16
+ # New application with specific database
17
+ rails new <your_application_name> -d <database_name>
18
+ ```
19
+
20
+ Add this dependency to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'imagekitio'
24
+ ```
25
+
26
+ And then execute:
27
+ ```
28
+ $ bundle install
29
+ ```
30
+ Or install it yourself as:
31
+ ```
32
+ $ gem install imagekitio
33
+ ```
34
+ ## Usage
35
+
36
+ Rails SDK for [ImageKit](https://imagekit.io/) that implements the new APIs and interface for performing different file operations.
37
+
38
+ ImageKit is a complete image optimization and transformation solution that comes with and
39
+ [image CDN](https://imagekit.io/features/imagekit-infrastructure) and media storage. It can be integrated with your
40
+ existing infrastructure - storage like AWS s3, web servers, your CDN, and custom domain names, allowing you to deliver
41
+ optimize images in minutes with minimal code changes.
42
+
43
+ Table of contents -
44
+ * [Installation](#Installation)
45
+ * [Initialization](#Initialization)
46
+ * [URL Generation](#URL-generation)
47
+ * [File Upload](#File-Upload)
48
+ * [File Management](#File-Management)
49
+ * [Utility Functions](#Utility-functions)
50
+ * [Support](#Support)
51
+ * [Links](#Links)
52
+
53
+
54
+ ## Initialization
55
+
56
+ Add this configuration to `config/environments/development.rb` and `config/environments/production.rb`
57
+
58
+ ```ruby
59
+ config.imagekit={
60
+ private_key: "<your-private-key>",
61
+ public_key: "<your-public-key>",
62
+ url_endpoint: "<endpoint-url>"
63
+ }
64
+ ```
65
+
66
+ You need to create an uploader to manage file. To create an uploader, use this command inside the project directory.
67
+ ```bash
68
+ rails g uploader <Uploading_attribute_name>
69
+ # For example if you want to create uploader for Avatar attribute then use
70
+ rails g uploader Avatar
71
+ # Generated uploader's path will be app/uploaders/avatar_uploader.rb
72
+ ```
73
+
74
+ After that you need to edit your generated uploader and do the following changes:
75
+ ```ruby
76
+ # Set store as imagekit_store
77
+ storage :imagekit_store
78
+
79
+ # If you want to add uploading options then create this method inside uploader file as an example
80
+
81
+ def options
82
+ options={
83
+ response_fields: 'isPrivateFile, tags',
84
+ tags: %w[abc def],
85
+ use_unique_file_name: false
86
+ }
87
+ end
88
+ ```
89
+
90
+ Then you need to modify your model. for example- if your model name is employee then do these changes
91
+
92
+ ```ruby
93
+ class Employee < ApplicationRecord
94
+ attr_accessor :avatar
95
+ mount_uploader :avatar, AvatarUploader
96
+ end
97
+
98
+ ```
99
+
100
+ Get image url:
101
+ ```ruby
102
+ # If @employee is an object of your model that has data.
103
+
104
+ # To get original image url use
105
+ @employee.avatar.url
106
+
107
+ # And to get transformed url use
108
+ # options is a transformation options
109
+ @employee.avatar.url_with(options)
110
+ ```
111
+
112
+ ## Usage
113
+
114
+ You can use this Ruby SDK for 3 different kinds of methods - URL generation, file upload, and file management.
115
+ The usage of the SDK has been explained below
116
+
117
+ ## URL generation
118
+
119
+ **1. Using Image path and image hostname or endpoint**
120
+
121
+ This method allows you to create a URL using the path where the image exists and the URL
122
+ endpoint(url_endpoint) you want to use to access the image. You can refer to the documentation
123
+ [here](https://docs.imagekit.io/integration/url-endpoints) to read more about URL endpoints
124
+ in ImageKit and the section about [image origins](https://docs.imagekit.io/integration/configure-origin) to understand
125
+ about paths with different kinds of origins.
126
+
127
+
128
+ ```ruby
129
+ image_url = imagekitio.url({
130
+ path: "/default-image.jpg",
131
+ url_endpoint: "https://ik.imagekit.io/your_imagekit_id/endpoint/",
132
+ transformation: [{height: "300", width: "400"}]
133
+ })
134
+ ```
135
+
136
+ The result in a URL like
137
+ ```
138
+ https://ik.imagekit.io/your_imagekit_id/endpoint/tr:h-300,w-400/default-image.jpg
139
+ ```
140
+
141
+ **2.Using full image URL**
142
+ This method allows you to add transformation parameters to and existing, complete URL that is already mapped to ImageKit
143
+ using ```src``` parameter. This method should be used if you have the complete image URL mapped to ImageKit stored in your
144
+ database.
145
+
146
+
147
+ ```ruby
148
+ image_url = imagekitio.url({
149
+ src: "https://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg",
150
+ transformation: [{height: "300", width: "400"}],
151
+ })
152
+ ```
153
+
154
+ The results in a URL like
155
+
156
+ ```
157
+ https://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg?tr=h-300%2Cw-400
158
+ ```
159
+
160
+
161
+ The `.url()` method accepts the following parameters
162
+
163
+ | Option | Description |
164
+ | :---------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
165
+ | url_endpoint | Optional. The base URL to be appended before the path of the image. If not specified, the URL Endpoint specified at the time of SDK initialization is used. For example, https://ik.imagekit.io/your_imagekit_id/endpoint/ |
166
+ | path | Conditional. This is the path at which the image exists. For example, `/path/to/image.jpg`. Either the `path` or `src` parameter needs to be specified for URL generation. |
167
+ | src | Conditional. This is the complete URL of an image already mapped to ImageKit. For example, `https://ik.imagekit.io/your_imagekit_id/endpoint/path/to/image.jpg`. Either the `path` or `src` parameter needs to be specified for URL generation. |
168
+ | transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a [chained transformation](https://docs.imagekit.io/features/image-transformations/chained-transformations) can be specified as different objects of the array. The complete list of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
169
+ | transformation_position | Optional. The default value is `path` that places the transformation string as a path parameter in the URL. It can also be specified as `query` which adds the transformation string as the query parameter `tr` in the URL. If you use `src` parameter to create the URL, then the transformation string is always added as a query parameter. |
170
+ | query_parameters | Optional. These are the other query parameters that you want to add to the final URL. These can be any query parameters and not necessarily related to ImageKit. Especially useful if you want to add some versioning parameter to your URLs. |
171
+ | signed | Optional. Boolean. Default is `false`. If set to `true`, the SDK generates a signed image URL adding the image signature to the image URL. This can only be used if you are creating the URL with the `url_endpoint` and `path` parameters, and not with the `src` parameter. |
172
+ | expire_seconds | Optional. Integer. Meant to be used along with the `signed` parameter to specify the time in seconds from now when the URL should expire. If specified, the URL contains the expiry timestamp in the URL, and the image signature is modified accordingly. |
173
+
174
+
175
+ ## Examples of generating URLs
176
+ **1. Chained Transformations as a query parameter**
177
+
178
+ ```ruby
179
+ image_url = imagekitio.url({
180
+ path: "/default-image.jpg",
181
+ url_endpoint: "https://ik.imagekit.io/your_imagekit_id/endpoint/",
182
+ transformation: [{
183
+ height: "300",
184
+ width: "400"
185
+ },{
186
+ rotation: 90
187
+ }],
188
+ transformation_position: "query"
189
+ })
190
+ ```
191
+ Sample Result URL -
192
+ ```
193
+ https://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg?tr=h-300%2Cw-400%3Art-90
194
+ ```
195
+
196
+ **2. Sharpening and contrast transforms and a progressive JPG image**
197
+
198
+ There are some transforms like [Sharpening](https://docs.imagekit.io/features/image-transformations/image-enhancement-and-color-manipulation)
199
+ that can be added to the URL with or without any other value. To use such transforms without specifying a value, specify
200
+ the value as "-" in the transformation object. Otherwise, specify the value that you want to be
201
+ added to this transformation.
202
+
203
+
204
+ ```ruby
205
+ image_url = imagekitio.url({
206
+ src: "https://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg",
207
+ transformation: [{
208
+ format: "jpg",
209
+ progressive: "true",
210
+ effect_sharpen: "-",
211
+ effect_contrast: "1"
212
+ }]
213
+ })
214
+ ```
215
+
216
+ ```
217
+ //Note that because `src` parameter was used, the transformation string gets added as a query parameter `tr`
218
+ https://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg?tr=f-jpg%2Cpr-true%2Ce-sharpen%2Ce-contrast-1
219
+ ```
220
+
221
+ **3. Signed URL that expires in 300 seconds with the default URL endpoint and other query parameters**
222
+
223
+ ```ruby
224
+ image_url = imagekit.url({
225
+ path: "/default-image",
226
+ query_parameters: {
227
+ "v": "123"
228
+ },
229
+ transformation: [{
230
+ height: "300",
231
+ width: "400"
232
+ }],
233
+ signed: True,
234
+ expire_seconds: 300
235
+ })
236
+ ```
237
+ **Sample Result URL**
238
+ ```
239
+ https://ik.imagekit.io/your_imagekit_id/tr:h-300,w-400/default-image.jpg?v=123&ik-t=1567358667&ik-s=f2c7cdacbe7707b71a83d49cf1c6110e3d701054
240
+ ```
241
+
242
+ **List of transformations**
243
+
244
+ The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/features/image-transformations/resize-crop-and-other-transformations).
245
+ The SDK gives a name to each transformation parameter, making the code simpler, making the code simpler and readable.
246
+ If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, then use the
247
+ transformation code from ImageKit docs as the name when using in the `url` function.
248
+
249
+ | Supported Transformation Name | Translates to parameter |
250
+ | ----------------------------- | ----------------------- |
251
+ | height | h |
252
+ | width | w |
253
+ | aspect_ratio | ar |
254
+ | quality | q |
255
+ | crop | c |
256
+ | crop_mode | cm |
257
+ | x | x |
258
+ | y | y |
259
+ | focus | fo |
260
+ | format | f |
261
+ | radius | r |
262
+ | background | bg |
263
+ | border | bo |
264
+ | rotation | rt |
265
+ | blur | bl |
266
+ | named | n |
267
+ | overlay_image | oi |
268
+ | overlay_x | ox |
269
+ | overlay_y | oy |
270
+ | overlay_focus | ofo |
271
+ | overlay_height | oh |
272
+ | overlay_width | ow |
273
+ | overlay_text | ot |
274
+ | overlay_text_font_size | ots |
275
+ | overlay_text_font_family | otf |
276
+ | overlay_text_color | otc |
277
+ | overlay_alpha | oa |
278
+ | overlay_text_typography | ott |
279
+ | overlay_background | obg |
280
+ | overlay_image_trim | oit |
281
+ | progressive | pr |
282
+ | lossless | lo |
283
+ | trim | t |
284
+ | metadata | md |
285
+ | color_profile | cp |
286
+ | default_image | di |
287
+ | dpr | dpr |
288
+ | effect_sharpen | e-sharpen |
289
+ | effect_usm | e-usm |
290
+ | effect_contrast | e-contrast |
291
+ | effect_gray | e-grayscale |
292
+ | original | orig |
293
+
294
+ ## File Upload
295
+
296
+ The SDK provides a simple interface using the `.upload()` method to upload files to the ImageKit Media library. It
297
+ accepts all the parameters supported by the [ImageKit Upload API](https://docs.imagekit.io/api-reference/upload-file-api/server-side-file-upload).
298
+
299
+ The `upload()` method requires at least the `file` and the `file_name` parameter to upload a file and returns
300
+ a callback with the `error` and `result` as arguments. You can pass other parameters supported by the
301
+ ImageKit upload API using the same parameter name as specified in the upload API documentation. For example, to specify tags for a file at the time of upload, use the `tags` parameter as specified in the [documentation here](https://docs.imagekit.io/api-reference/upload-file-api/server-side-file-upload).
302
+
303
+ Simple usage
304
+
305
+ ```ruby
306
+ imagekitio.upload_file(
307
+ file = "<url|base_64|binary>", # required
308
+ file_name= "my_file_name.jpg", # required
309
+ options= {response_fields: 'isPrivateFile, tags', tags: %w[abc def], use_unique_file_name: true,}
310
+ )
311
+
312
+ ```
313
+
314
+ If the upload is succeeded, `error` will be `None`, and the result will be the same as what is received from ImageKit's
315
+ servers. If the upload fails, `error` will be the same as what is received from ImageKit's servers, and the result will
316
+ be `None`.
317
+
318
+ ## File Management
319
+
320
+ The SDK provides a simple interface for all the [media APIs mentioned here](https://docs.imagekit.io/api-reference/media-api)
321
+ to manage your files. This also returns `error` and `result`, error will be `None` if API succeeds.
322
+
323
+ **1. List & Search Files**
324
+
325
+ Accepts an object specifying the parameters to be used to list and search files. All parameters specified
326
+ in the [documentation here](https://docs.imagekit.io/api-reference/media-api/list-and-search-files#list-and-search-file-api) can be passed with the
327
+ correct values to get the results.
328
+
329
+ ```ruby
330
+ imagekitio.list_files({skip: 0, limit: 5})
331
+ ```
332
+ **2. Get File Details**
333
+ Accepts the file ID and fetches the details as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/get-file-details)
334
+
335
+ ```ruby
336
+ imagekitio.get_file_details(file_id)
337
+ ```
338
+
339
+ **3. Get File Metadata**
340
+ Accepts the file ID and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/api-reference/metadata-api/get-image-metadata-for-uploaded-media-files)
341
+ ```ruby
342
+ imagekit.get_file_metadata(file_id)
343
+ ```
344
+
345
+ **3. Get File Metadata from remote url**
346
+ Accepts the remote file url and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/api-reference/metadata-api/get-image-metadata-from-remote-url)
347
+
348
+ ```ruby
349
+ imagekit.get_remote_file_url_metadata(remote_file_url)
350
+ ```
351
+
352
+ **4. Update File Details**
353
+ Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/update-file-details).
354
+ The first argument to the `update_field_details` method is the file ID and the second argument is an object with the
355
+ parameters to be updated.
356
+
357
+ ```ruby
358
+ imagekitio.update_file_details(file_id, {
359
+ tags: ["image_tag"],
360
+ custom_coordinates: "10,10,100, 100"
361
+ })
362
+ ```
363
+
364
+ **6. Delete file**
365
+ Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-file). The method accepts the file ID of the file that has to be deleted.
366
+
367
+ ```ruby
368
+ imagekitio.delete_file(file_id)
369
+ ```
370
+
371
+ **6. Bulk File Delete by IDs**
372
+ Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-files-bulk). The method accepts a list of file IDs of files that has to be
373
+ deleted.
374
+
375
+ ```ruby
376
+ imagekitio.bulk_file_delete(["file_id1", "file_id2"])
377
+ ```
378
+
379
+ **6. Purge Cache**
380
+ Programmatically issue a cache clear request as pet the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache).
381
+ Accepts the full URL of the file for which the cache has to be cleared.
382
+
383
+ ```ruby
384
+ imagekitio.purge_file_cache(full_url)
385
+ ```
386
+ **7. Purge Cache Status**
387
+
388
+ Get the purge cache request status using the request ID returned when a purge cache request gets submitted as pet the
389
+ [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache-status)
390
+
391
+ ```ruby
392
+ imagekitio.get_purge_file_cache_status(cache_request_id)
393
+ ```
394
+
395
+ ## Utility functions
396
+
397
+ We have included the following commonly used utility functions in this package.
398
+
399
+ **Authentication parameter generation**
400
+
401
+ In case you are looking to implement client-side file upload, you are going to need a token, expiry timestamp
402
+ , and a valid signature for that upload. The SDK provides a simple method that you can use in your code to generate these
403
+ authentication parameters for you.
404
+
405
+ _Note: The Private API Key should never be exposed in any client-side code. You must always generate these authentication parameters on the server-side_
406
+
407
+ `authentication_parameters = imagekit.get_authentication_parameters(token, expire)`
408
+
409
+ Returns
410
+
411
+ ```ruby
412
+ {
413
+ "token": "unique_token",
414
+ "expire": "valid_expiry_timestamp",
415
+ "signature": "generated_signature"
416
+ }
417
+ ```
418
+
419
+ Both the `token` and `expire` parameters are optional. If not specified, the SDK uses the uuid to generate a random
420
+ token and also generates a valid expiry timestamp internally. The value of the token and expire used to generate the
421
+ signature are always returned in the response, no matter if they are provided as an input to this method or not.
422
+
423
+ **Distance calculation between two pHash values**
424
+
425
+ Perceptual hashing allows you to construct a hash value that uniquely identifies an input image based on the contents
426
+ of an image. [imagekit.io metadata API](https://docs.imagekit.io/api-reference/metadata-api) returns the pHash
427
+ value of an image in the response. You can use this value to find a duplicate, near the duplicate(similar) image by calculating
428
+ the distance between the two images.
429
+
430
+
431
+ This SDK exposes phash_distance function to calculate the distance between two pHash value. It accepts two pHash hexadecimal
432
+ strings and returns a numeric value indicative of the level of difference between the two images.
433
+
434
+ ```ruby
435
+ def calculate_distance():
436
+ # fetch metadata of two uploaded image files
437
+ ...
438
+ # extract pHash strings from both: say 'first_hash' and 'second_hash'
439
+ ...
440
+ # calculate the distance between them:
441
+
442
+ distance = imagekitio.phash_distance(first_hash, second_hash)
443
+ return distance
444
+ ```
445
+
446
+ **Distance calculation examples**
447
+
448
+ ```ruby
449
+ imagekitio.phash_distance('f06830ca9f1e3e90', 'f06830ca9f1e3e90')
450
+ # output: 0 (ame image)
451
+
452
+ imagekitio.phash_distance('2d5ad3936d2e015b', '2d6ed293db36a4fb')
453
+ # output: 17 (similar images)
454
+
455
+ imagekitio.phash_distance('a4a65595ac94518b', '7838873e791f8400')
456
+ # output: 37 (dissimilar images)
457
+ ```
458
+
459
+ ## Sample Code Instruction
460
+ There are two sample apps:
461
+ * [Rails application using Carrierwave](#Instructions-for-rails-application)
462
+ * [Plain ruby application](#Instructions-for-ruby-application)
463
+
464
+ ### Instructions for rails application
465
+ This is under [samples/rails_app](https://github.com/imagekit-developer/imagekit-ruby/blob/master/samples/rails_app) directory. Follow the instructions below to set up rails application.
466
+
467
+ **1. Clone git repository**
468
+ ```bash
469
+ git clone `https://github.com/imagekit-developer/imagekit-gem
470
+ ```
471
+ **2. Go to sample project directory**
472
+ ```bash
473
+ cd sample/rails_app
474
+ ```
475
+ **3. Write imagekit configuration in `config/environments/development.rb`**
476
+ ```ruby
477
+ config.imagekit={
478
+ private_key: "<your-private-key>",
479
+ public_key: "<your-public-key>",
480
+ url_endpoint: "<endpoint-url>"
481
+ }
482
+ ```
483
+ **4. Install dependency**
484
+ ```ruby
485
+ bundle install
486
+ ```
487
+ This sample project are using Sqlite3 database. If you are getting `sqlite3` gem installation error then install sqlite3 first then again run `bundle install`.
488
+
489
+ **5. Migrate the database**
490
+ ```ruby
491
+ bundle exec rake db:migrate
492
+ ```
493
+ This sample project is using Sqlite3 database. If you are getting `sqlite3` gem installation error, then install sqlite3 first then again run `bundle install`.
494
+
495
+ **6. Run your application**
496
+ ```ruby
497
+ rails s
498
+ ```
499
+ It will run on your default rails port [3000].
500
+ Sample Application URL: http://localhost:3000/posts/
501
+
502
+ ### Instructions for ruby application
503
+ Run following command under [samples/ruby_app](https://github.com/imagekit-developer/imagekit-ruby/blob/master/samples/ruby_app) directory
504
+ ```ruby
505
+ ruby app.rb
506
+ ```
507
+
508
+ ## Support
509
+ For any feedback or to report any issues or general implementation support, please reach out to [support@imagekit.io](mailto:support@imagekit.io)
510
+
511
+ ## Links
512
+ - [Documentation](https://docs.imagekit.io)
513
+ - [Main website](https://imagekit.io)
514
+
515
+ ## License
516
+ Released under the MIT license.
517
+