Git Product home page Git Product logo

resize-image's Introduction

resize-image

Resize images in browser using canvas

NPM

Build Status Coverage Status

Install

npm

npm i resize-image --save

bower

bower install resize-image --save

Usage

var resizeImage = require('resize-image');

var img = new Image();
img.onload= function () {
  var data = resizeImage.resize(img, 200, 100, resizeImage.PNG);
  console.log(data);
};
img.src = url; // local image url

.resize2Canvas(img, width, height)

resize an <img> or Image or <canvas> to canvas

  • {Image} img: an <img> or Image()
  • {number} [width]: output image width
  • {number} [height]: output image height

.resize(img, width, height, type)

resize an <img> or Image or <canvas> to base64

  • {Image} img: an <img> or Image()
  • {number} [width]: output image width
  • {number} [height]: output image height
  • {string} [type]: output image type

types

  • .PNG (default)
  • .GIF
  • .BMP
  • .JPEG
  • .WEBP

Examples

yarn
npm run example

And visit http://localhost:9000/.

resize-image's People

Contributors

aylusltd avatar csbun avatar dependabot[bot] avatar limonte avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  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

resize-image's Issues

Ho to optimize resize quality ?

I resize a very big image (12288 × 12288) to a small image (512x512) with this library.

I have big image quality issues while resizing, how to fix this quality problem ?
Thanks a lot for your help !

My code

const zoomRoot = bigSquareImageClone
        .resize({ 'width': 512, 'height': 512 });

Original (screenshot un-zoomed)
image

Resized: Screenshot 100%
image

GIF RESIZE

Trying to resize a gift

const ImgBase64_ = resizeImage.resize(
image,
resizeWidth,
resizeHeight,
resizeImage.GIF
);

But it always return it as a PNG

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA54AAALKCAYAAABa546yAAAgAElEQVR4Xuy9a7hdRZUuPHZCyEVI9sqdhGAISQiEEISDwNPcoTFG8ALd3kA5Cm1rd3ts2+4Gn+9H43ee8zXY2mp7OdqKtkoUtQUVjAFBA6jcREggBJIQENghN7J2QsiFkOzvGVVrzDlqzDGqaq69dgKY+SNZe866zZpVNeud7xjv6PpQ4wd9zUYXNJp9gAf9xv.........

Strange behavior the resize function with omitted "height" param

Hi, first of all, thank you for this package 👍

What is actually happening?

Let's say, I have an image of 1600x900 px and I want to resize it down to 800xHeight px. I'm trying to do it with next code:

resize2Canvas(canvas, 800)

but in result I have an image with Widthx800 px 😸 because of this math

  function resize2Canvas(img, width, height) {
    // ...
    height = height || width;
    // 按原图缩放
    var detImg = img.width / img.height;
    if (width / height > detImg) {
      height = width / detImg;
    } else {
      width = height * detImg;
    }

https://jsfiddle.net/1p0z3qbm/

What is expected?

I expect that my new image should have width=800. Something like this:

function getNewImageDimentions (canvas, width, height) {
  if (width && height) {
    return [width, height]
  } else if (width) {
    return [width, width * canvas.height / canvas.width]
  } else if (height) {
    return [canvas.width * height / canvas.height, height]
  } else {
    return [canvas.width, canvas.height]
  }
}

// Force dimentions
console.log(getNewImageDimentions({
	width: 1600,
  height: 900
}, 800, 600)) // -> [800, 600]

// Auto height
console.log(getNewImageDimentions({
	width: 1600,
  height: 900
}, 800)) // -> [800, auto]

// Auto width
console.log(getNewImageDimentions({
	width: 1600,
  height: 900
}, null, 600)) // -> [auto, 600]

// No args? Just returns original dimentions
console.log(getNewImageDimentions({
	width: 1600,
  height: 900
})) // [1600, 900]

https://jsfiddle.net/1p0z3qbm/6/

Helpful links

Image resize give error when user "resize-image" in nodejs

Code for image resize is
var img = "https://cdn-img.com/sites/default/abc.jpg";
var data = resizeImage.resize2Canvas(img, 200, 100);
console.log("data" + data);

And getting this error
ReferenceError: document is not defined
at Object.resize2Canvas (PROJECT FOLDER*******\node_modules\resize-image\index.js:51:27)
at Object. (D:\work\gitlab\route\image-scrape.js:52:26)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (D:\work\gitlab\server.js:32:9)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3

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.