UNPKG

image-optimizer-s3-fastcodeco

Version:

IMAGE OPTIMIZER FASTCODE CO --

61 lines (36 loc) 1.5 kB
IMAGE OPTIMIZER FASTCODE CO -- A simple library for Node JS that use Sharp and AWS SDK to provide an Image Optimizer (downsize, resize and converting any image file format to png/jpg), also it brings the posibility to upload the processed image to an AWS S3 bucket. **REQUIRED** * Node JS v14.18+ **INSTALLING** ``` $ npm i image-optimizer-s3-fastcodeco ``` **USAGE** ``` import {optimizeImage, S3Uploader} from 'image-optimizer-s3-fastcodeco/dist'; import fs from 'fs'; import path from 'path'; (async()=>{ try{ //in this example with read the image from local but you can use a buffer according your needs const image_path:string = path.join(__dirname, './test.jpeg'); const image:Buffer = fs.readFileSync(image_path); //calling to optimizeImage by passing the buffer and width size (optional if not passed it will resize to 1024) const optmized_image:Buffer = await optimizeImage(image, 680); //uploading to S3. Pass values as ENV vars is highly recommended const uploader = new S3Uploader( 'BUCKET_REGION', 'AWS_KEY', 'AWS_SECRET_KEY', 'BUCKET_NAME' ); const uploaded_file:UploadS3_IMGUP = await uploader.upload(optmized_image, path.basename(image_path)); console.log(uploaded_file); //do logic for uploaded file }catch(e){ //handle the excepcion... } })(); ```