image-optim-upload
Unofficial package to handled the upload and compress image by using ImageOptim compression service. It takes an uploaded image URL, converts it, and returns to converted buffer. This package is able to write the returned buffer to file or return the buffer as it is. For further detail, visit ImageOptim API documentation.
Installation
npm install --save image-optim-upload
Usage
const ImageOptimUpload = const iou = USERNAME // all functions return promise// handled using async-awaitconst filePath = await iou const convertedBuffer = await iou // set the S3 config beforehandiouconst s3path = await iou
For more details on options, please check ImageOptiom API documentation.
API
ImageOptimUpload
Instantiation 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.
const ImageOptimUpload = const iou = 'username'
compressAndWriteFile(fileDestination, imageSource, [options], filename)
Will upload to ImageOptim and write the compressed image to file
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
compressAndSaveToBuffer(imageSource, [options], filename)
Will upload to ImageOptim and return the buffer of the compressed image.
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
compressAndSaveToS3(imageSource, [options], s3param, filename)
Will upload to ImageOptim, upload the compressed image to S3, and return the S3 path. To use this feature, make sure to pass S3 config when instantiating the ImageOptim class or set it before calling the function.
const ImageOptimUpload = const iou = USERNAME S3 CONFIG OBJECT // or you can use the setteriou
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
About options parameter
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.
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 = Joiobject
Results
image source: pexels
Before
(~5.5MB)
After
(~2.8MB)
License
ISC