Skip to content

Commit

Permalink
Use crop-universal to factorize code
Browse files Browse the repository at this point in the history
  • Loading branch information
GMartigny committed Jan 29, 2020
1 parent 5599366 commit c8e4adc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
55 changes: 17 additions & 38 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Canvas from "canvas";
import detect from "detect-edges";

const { loadImage, createCanvas } = Canvas;
import crop from "crop-universal";

const defaultOptions = {
outputFormat: "png",
};

const cropper = crop(Canvas);

/**
* @typedef {Object} Options
* @prop {String} [outputFormat="png"] - Format of the output image (`"png"` or `"jpeg"`)
*/
/**
* Crop transparent pixels from an image
* @param {String|HTMLCanvasElement} input - Path to the image to process or a tainted canvas
* @param {String|Canvas.Canvas|Canvas.Image} input - Path to the image to process, another Canvas or an Image
* @param {Options} options - Some options
* @returns {Promise<Buffer>}
*/
Expand All @@ -23,44 +23,23 @@ export default async (input, options) => {
...options,
};

if (!input) {
throw new Error("No input given.");
}

// Check outputFormat
const supportedFormat = ["png", "jpeg"];
if (!supportedFormat.includes(outputFormat)) {
const supported = JSON.stringify(supportedFormat);
throw new Error(`outputFormat should only be one of ${supported}, but "${outputFormat}" was given.`);
}

const isString = typeof input === "string";

let canvas;
let image;

if (isString) {
image = await loadImage(input);
const { width, height } = image;
canvas = createCanvas(width, height);
}
else {
canvas = input;
}

const { width, height } = canvas;
const context = canvas.getContext("2d");

if (isString) {
context.drawImage(image, 0, 0, width, height);
}

const data = context.getImageData(0, 0, width, height);

const { top, right, bottom, left } = detect(canvas, options);

canvas.width = width - (left + right);
canvas.height = height - (top + bottom);
context.putImageData(data, -left, -top);

return canvas.toBuffer(`image/${outputFormat}`);
// Do crop
const canvas = await cropper(input, options);

// Export as a buffer
return new Promise((resolve, reject) => canvas.toBuffer((error, buffer) => {
if (error) {
reject(error);
}
else {
resolve(buffer);
}
}, `image/${outputFormat}`));
};
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
{
"name": "crop-node",
"version": "2.0.0",
"description": "Crop transparent pixel from image",
"description": "Crop transparent pixel from image in Node.js",
"keywords": [
"trim",
"image",
"edge",
"cut",
"resize",
"transparent",
"opacity",
"pixel",
"remove"
],
"main": "index.mjs",
"scripts": {
"test": "ava"
Expand All @@ -18,17 +29,6 @@
"type": "git",
"url": "git+https://github.com/GMartigny/crop-node.git"
},
"keywords": [
"trim",
"image",
"edge",
"cut",
"resize",
"transparent",
"opacity",
"pixel",
"remove"
],
"author": "GMartigny",
"license": "MIT",
"bugs": {
Expand All @@ -44,6 +44,6 @@
},
"dependencies": {
"canvas": "^2.6.1",
"detect-edges": "^1.0.1"
"crop-universal": "^1.0.0"
}
}
13 changes: 11 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[![Package version](https://flat.badgen.net/npm/v/crop-node)](https://www.npmjs.com/package/crop-node)
[![Package size](https://badgen.net/bundlephobia/minzip/crop-node)](https://bundlephobia.com/result?p=crop-node)

Crop all transparent pixel around an image's edges. ([CLI version](https://github.com/gmartigny/crop-node-cli))
Crop all transparent pixel around an image's edges. ([CLI version](https://github.com/GMartigny/crop-node-cli))


## Installation

Expand All @@ -13,7 +14,8 @@ Crop all transparent pixel around an image's edges. ([CLI version](https://githu
## Usage

```js
const crop = require("crop-noode");
const crop = require("crop-node");
const { writeFileSync } = require("fs");

// Path to an image file
const path = "path/to/image.png";
Expand Down Expand Up @@ -47,6 +49,13 @@ const options = {
In addition, all [options of `detect-edges`](https://github.com/GMartigny/detect-edges#options) are supported.


## Related

- CLI version [`crop-node-cli`](https://github.com/GMartigny/crop-node-cli)
- Browser version [`crop-browser`](https://github.com/GMartigny/crop-browser)
- Environment agnostic [`crop-universal`](https://github.com/GMartigny/crop-universal)


## License

[MIT](license)

0 comments on commit c8e4adc

Please sign in to comment.