unofficial module to upload and compress image by using ImageOptim compression service
npm install image-optim-upload```
npm install --save image-optim-upload
`javascript
const ImageOptimUpload = require('image-optim-upload')
const iou = new ImageOptimUpload([USERNAME])
// all functions return promise
// handled using async-await
const filePath = await iou.compressAndWriteFile(
[FILENAME],
[IMAGE],
[OPTION OBJECT],
[FILENAME]
)
const convertedBuffer = await iou.compressAndSaveToBuffer(
[IMAGE],
[OPTION OBJECT],
[FILENAME]
)
// set the S3 config beforehand
iou.s3([S3 CONFIG OBJECT])
const s3path = await iou.compressAndSaveToS3(
[IMAGE],
[OPTION OBJECT],
[S3 PARAM],
[FILENAME]
)
`
For more details on options, please check ImageOptiom API documentation.
To instantiate the module, use new and it will result and object. The only parameter it needs is _username_ and it's required, otherwise instantiation will throw error.
`javascript`
const ImageOptimUpload = require('image-optim-upload')
const iou = new ImageOptimUpload('username')
#### Returns
Promise, if resolved will return filepath of written file, otherwise error will be returned.
#### Parameters
- fileDestination - _String_ - determine filename and directory of the written file
- imageSource - _String_/_Buffer_ - either URL, file path, or buffer of an image. Wrong file format is not handled yet
- options - _Object_ - see the details below
- filename - _String_ - required only if _imageSource_ is buffer
#### Returns
Promise, if resolved will return buffer of compressed image, otherwise error will be returned.
#### Parameters
- imageSource - _String_/_Buffer_ - either URL, file path, or buffer of an image. Wrong file format is not handled yet
- options - _Object_ - (optional) see the details below
- filename - _String_ - required only if _imageSource_ is buffer
`javascript
const ImageOptimUpload = require('image-optim-upload')
const iou = new ImageOptimUpload([USERNAME], [S3 CONFIG OBJECT])
// or you can use the setter
iou.s3([S3 CONFIG OBJECT])
`
#### Returns
Promise, if resolved will return path file on designated bucket, otherwise error will be returned.
#### Parameters
- imageSource - _String_/_Buffer_ - either URL, file path, or buffer of an image. Wrong file format is not handled yet
- options - _Object_ - (optional) see the details below
- s3param - _Object_ - params of S3's upload function on aws-sdk. Check its documentation.
- filename - _String_ - required only if _imageSource_ is buffer
By default, _quality_ is set to medium and _timeout_ is set to 30. Please take a look at ImageOptim API documentation in prior. The _options_ parameter is the equivalent object schema of the options in the API. Parameter will be validated, here is the Joi validation schema that describes allowed properties and value of inside the object.
`javascript
const IMAGE_OPTIM_QUALITY = ['low', 'medium', 'high', 'lossless']
const IMAGE_OPTIM_MULTIPLY = ['1x', '2x', '3x']
const IMAGE_OPTIM_CROP = [true, 'auto', 'top', 'left', 'right', 'bottom']
const IMAGE_OPTIM_FORMAT = ['png', 'jpeg', 'webm', 'h264']
const optionSchema = Joi.object().keys(
{
quality: Joi.string().valid(IMAGE_OPTIM_QUALITY),
multiply: Joi.string().valid(IMAGE_OPTIM_MULTIPLY),
maxWidth: Joi.number().positive(),
maxHeight: Joi.number().positive(),
fit: Joi.boolean().default(false),
scaleDown: Joi.boolean().default(false),
crop: Joi.any().valid(IMAGE_OPTIM_CROP),
cropX: Joi.number(),
cropY: Joi.number(),
trim: Joi.string().valid('border'),
format: Joi.string().valid(IMAGE_OPTIM_FORMAT),
timeout: Joi.number().positive()
}
)
``
> image source: pexels