Skip to content

Commit

Permalink
Upscale and cover image
Browse files Browse the repository at this point in the history
  • Loading branch information
flameface committed Feb 11, 2024
1 parent 8eaf9d6 commit 957f0ad
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
Binary file added assets/cropped-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ async function cropImage({
try {
const image = await loadImage(imagePath);

if (!width) width = image.width;
if (!height) height = image.height;

const scaleWidth = width / image.width;
const scaleHeight = height / image.height;
const scaleFactor = Math.max(scaleWidth, scaleHeight);

const scaledWidth = image.width * scaleFactor;
const scaledHeight = image.height * scaleFactor;

x = (width - scaledWidth) / 2;
y = (height - scaledHeight) / 2;

const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');

Expand All @@ -29,12 +42,12 @@ async function cropImage({
ctx.clip();
}

ctx.drawImage(image, x, y, width, height, 0, 0, width, height);
ctx.drawImage(image, x, y, scaledWidth, scaledHeight);

return canvas.toBuffer('image/png');
return canvas.toBuffer("image/png");
} catch (error) {
console.error('Error cropping image:', error.message);
throw new Error(error.message);
}
}

module.exports = { cropImage };
module.exports = { cropImage };
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cropify",
"types": "./index.d.ts",
"description": "Crop and round image corners seamlessly.",
"version": "1.0.1",
"version": "1.0.2",
"main": "index.js",
"publishConfig": {
"access": "public",
Expand All @@ -15,7 +15,8 @@
"crop-image",
"canvas-crop",
"cropify",
"unburn"
"unburn",
"upscale"
],
"author": "FlameFace",
"license": "MIT",
Expand Down
19 changes: 7 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Crop images with precision using the Canvas API.
- Optional support for rounding corners with a customizable border radius.
- Designed for simplicity and performance.
- Upscale and cover image.

## Installation
```
Expand All @@ -16,13 +17,13 @@ npm install cropify
const { cropImage } = require("cropify");
const fs = require("fs")

const imagePath = 'https://raw.githubusercontent.com/unburn/.github/main/assets/unburngithub.png';
const imagePath = 'https://storage.googleapis.com/pai-images/8c27ed6e3c85463a8db958ecb596594e.jpeg';

const cropX = 600;
const cropX = 0;
const cropY = 0;
const cropWidth = 722;
const cropHeight = 422;
const borderRadius = 20;
const cropWidth = 1280;
const cropHeight = 720;
const borderRadius = 80;

cropImage({
imagePath: imagePath,
Expand All @@ -37,13 +38,7 @@ cropImage({
```

## Output
### Original

![original](https://raw.githubusercontent.com/unburn/.github/main/assets/unburngithub.png)

### Cropped

![crop](https://raw.githubusercontent.com/unburn/.github/main/assets/cropped-image.png)
![example](./assets/example.png)

---
If you need help or want some features to be added, join our official [discord](https://discord.com/invite/qDysF95NWh) community.

0 comments on commit 957f0ad

Please sign in to comment.