Git Product home page Git Product logo

multer-sharp's Introduction

Multer-Sharp

npm Codacy Badge Codeship Status for ikhsanalatsary/multer-sharp Build Status Code Climate codecov.io Depedencies Status devDepedencies Status npm Conventional Commits Standard Version Greenkeeper badge

DigitalOcean Referral Badge


Multer Sharp is streaming multer storage engine permit to resize and upload to Google Cloud Storage.

This project is mostly an integration piece for existing code samples from Multer's storage engine documentation. With add-ons include google-cloud and sharp

Requirement:

Node v10+

Breaking Change

multer-sharp >= 0.6.0 uses sharp version 0.22.1 and because of that some setup in the previous version cannot support (e.g. crop) and break the entire function. You can see it through the sharp changelog

Installation

npm:

npm install --save multer-sharp

yarn:

yarn add multer-sharp

Tests

npm test

Usage

const express = require('express');
const multer = require('multer');
const gcsSharp = require('multer-sharp');

const app = express();

// without resize image
const storage = gcsSharp({
    bucket: 'YOUR_BUCKET', // Required : bucket name to upload
    projectId: 'YOUR_PROJECTID', // Required : Google project ID
    keyFilename: 'YOUR_KEYFILENAME', // Optional : JSON credentials file for Google Cloud Storage
    destination: 'public/image', // Optional : destination folder to store your file on Google Cloud Storage, default: ''
    acl: 'publicRead' // Optional : acl credentials file for Google Cloud Storage, 'publicrRead' or 'private', default: 'private'
});
const upload = multer({ storage });

app.post('/upload', upload.single('myPic'), (req, res) => {
    console.log(req.file); // Print upload details
    res.send('Successfully uploaded!');
});

// or

// simple resize with custom filename
const storage2 = gcsSharp({
  filename: (req, file, cb) => {
      cb(null, `${file.fieldname}-newFilename`);
  },
  bucket: 'YOUR_BUCKET', // Required : bucket name to upload
  projectId: 'YOUR_PROJECTID', // Required : Google project ID
  keyFilename: 'YOUR_KEYFILENAME', // Optional : JSON credentials file for Google Cloud Storage
  acl: 'publicRead', // Optional : acl credentials file for Google Cloud Storage, 'publicrRead' or 'private', default: 'private'
  size: {
    width: 400,
    height: 400
  },
  max: true
});
const upload2 = multer({ storage: storage2 });

app.post('/uploadwithfilename', upload2.single('myPic'), (req, res, next) => {
    console.log(req.file); // Print upload details
    res.send('Successfully uploaded!');
});

/* If you need generate image with specific size
 * simply to adding `sizes` property
 * sizes must be an `array` and must be specify
 * with suffix, width / height property
 */
const storage = multerSharp({
  bucket: config.uploads.gcsUpload.bucket,
  projectId: config.uploads.gcsUpload.projectId,
  keyFilename: config.uploads.gcsUpload.keyFilename,
  acl: config.uploads.gcsUpload.acl,
  sizes: [
    { suffix: 'xlg', width: 1200, height: 1200 },
    { suffix: 'lg', width: 800, height: 800 },
    { suffix: 'md', width: 500, height: 500 },
    { suffix: 'sm', width: 300, height: 300 },
    { suffix: 'xs', width: 100 }
  ],
  max: true
});
const upload = multer({ storage });

app.post('/uploadmultiplesize', upload.single('myPic'), (req, res, next) => {
    console.log(req.file);
    /*
    * will print like this
    {
      originalname: 'nodejs-512.png',
      encoding: '7bit',
      mimetype: 'image/png',
      md: {
        path: 'https://storage.googleapis.com/multer-sharp.appspot.com/cd2105f5d60684a9f7c9fd2c340befed-md',
        filename: 'cd2105f5d60684a9f7c9fd2c340befed-md'
      },
      sm: {
        path: 'https://storage.googleapis.com/multer-sharp.appspot.com/cd2105f5d60684a9f7c9fd2c340befed-sm',
        filename: 'cd2105f5d60684a9f7c9fd2c340befed-sm'
      },
      xs: {
        path: 'https://storage.googleapis.com/multer-sharp.appspot.com/cd2105f5d60684a9f7c9fd2c340befed-xs',
        filename: 'cd2105f5d60684a9f7c9fd2c340befed-xs'
      }
    }
    */
    res.send('Successfully uploaded!');
});

for more example you can see here

Options

const storage = gcsSharp(options);

Multer-Sharp options

option default role
filename randomString your output filename
bucket no Required your bucket name on Google Cloud Storage to upload. Environment variable - GCS_BUCKET
projectId no Required your project id on Google Cloud Storage to upload. Environment variable - GC_PROJECT
keyFilename no JSON credentials file for Google Cloud Storage. Environment variable - GCS_KEYFILE or default google cloud credentials
acl 'private' Required acl credentials file for Google Cloud Storage, value: publicRead or private, doc: https://cloud.google.com/storage/docs/access-control/lists
gzip no @param {boolean} [options.gzip] Automatically gzip the file. This will set options.metadata.contentEncoding to gzip.
metadata no @param {object} additional metadata
destination emptyString Optional, destination folder to store your file on Google Cloud Storage
size no size specification object for output image, as follow: { width: 300, height: 200, option: {[...resizeOptions]} } property height & option is optional. doc: sharpResizeOptions
sizes no an Array of size specification object for output image and specify diff size with suffix, as follow: { suffix: 'md', width: 300, height: 200, option: {[...resizeOptions]} } property height & option is optional. doc: sharpResizeOptions

sharp options

Please visit this sharp for detailed overview of specific option.

multer-sharp embraces sharp option, as table below:

option default role
resize true resize images as per their size mentioned in options.size
composite false Composite image(s) over the processed (resized, extracted etc.) image
median false Apply median filter. When used without parameters the default window is 3x3
modulate false Transforms the image using brightness, saturation and hue rotation.
boolean false Perform a bitwise boolean operation with operand image
linear false Apply the linear formula a * input + b to the image (levels adjustment)
recomb false Recomb the image with the specified matrix
tint false Tint the image using the provided chroma while preserving the image luminance
removeAlpha false Remove alpha channel, if any
ensureAlpha false Ensure alpha channel, if missing
extractChannel false Extract a single channel from a multi-channel image
joinChannel false Join one or more channels to the image
bandbool false Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image
extract false extract specific part of image
trim false Trim boring pixels from all edges
flatten false Merge alpha transparency channel, if any, with background.
extend false Extends/pads the edges of the image with background.
negate false Produces the negative of the image.
rotate false Rotate the output image by either an explicit angle
flip false Flip the image about the vertical Y axis.
flop false Flop the image about the horizontal X axis.
blur false Mild blur of the output image
sharpen false Mild sharpen of the output image
gamma false Apply a gamma correction.
grayscale or greyscale false Convert to 8-bit greyscale; 256 shades of grey.
normalize or normalise false Enhance output image contrast by stretching its luminance to cover the full dynamic range.
withMetadata false Include all metadata (EXIF, XMP, IPTC) from the input image in the output image.
convolve false Convolve the image with the specified kernel.
threshold false Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0
toColourspace or toColorspace false Set the output colourspace. By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
toFormat false type of output file to produce. valid value : 'jpeg', 'png', 'magick', 'webp', 'tiff', 'openslide', 'dz', 'ppm', 'fits', 'gif', 'svg', 'pdf', 'v', 'raw' or object. if object specify as follow: { type: 'png', options: { [...toFormatOptions] } } doc: sharpToFormat

License

MIT Copyright (c) 2017 - forever Abdul Fattah Ikhsan

multer-sharp's People

Contributors

arrlancore avatar codacy-badger avatar greenkeeper[bot] avatar gunnarholwerda avatar ikhsanalatsary avatar simnare avatar snyk-bot avatar tenosiswono avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

multer-sharp's Issues

Local Storage Option?

This is just what I need, a way of uploading images then compressing them with sharp, as most pictures taken from smartphones are ~5 MB each. Way too big. I am not a fan of Google Cloud Storage, though, I dug through the docs but didn't find anything. Is there a local storage option?

Issue with Image Orientation

Really great project! I was just testing it out, but I came across an issue when uploading withMetadata.

const storage = gcsSharp({
bucket: config.cloud_bucket,
projectId: config.projectID,
//keyFilename: 'YOUR_KEYFILENAME',
destination: 'Images',
acl: 'publicRead',
metadata: true,
sizes:[
{ suffix: 'xlg', width: 1200, height: 1200 },
{ suffix: 'lg', width: 800, height: 800 },
{ suffix: 'md', width: 500, height: 500 },
{ suffix: 'sm', width: 300, height: 300 },
{ suffix: 'xs', width: 100 }
],
withMetadata: true,
toFormat: 'jpeg',
max: true
});

/home/sharrington/dev/okistuff/node_modules/sharp/lib/output.js:120
if (is.defined(withMetadata.orientation)) {

TypeError: Cannot read property 'orientation' of null

Please let me know if there's anything I can do to assist.

Can't use credentials for authentication

This module doesn't except credentials for authentication only keyfilename this can be fixed by adding

options.credentials = options.credentials || process.env.GCS_Credentials || null; on line 23

and
if(options.keyFilename != null){
try{
this.gcStorage = new Storage({
projectId: options.projectId,
credentials: options.keyFilename
});
}catch(error){
console.log(error);
}
}else if(options.credentials != null){
try{
this.gcStorage = new Storage({
projectId: options.projectId,
credentials: options.credentials
});
}catch(error){
console.log(error);
}
} or something similar to this starting on line 37

I ran into this issue trying to host an app on Heroku. Without this added if the user inputs google credentials (client_email, and private_key) and not the keyfilename the google-auth-library module looks to the environment to get GOOGLE_APPLICATION_CREDENTIALS and returns an error saying that the it was unable to load default credentials, pretty much ignoring the client email, and private_key credentials. I have had no luck trying to add my google auth json file as a config var to Heroku and found that this is an alternative solution.

Update google-cloud storage

Our library depends on Google storage too, so We need to update it but still wait until Typescript definition rollout. And some breaking change (you can check it in this release )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.