Skip to content

Commit 3da08c8

Browse files
committed
fix: handle wide image resizing properly
1 parent b3cd3c6 commit 3da08c8

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

cli.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ const getXY = index => {
5454

5555
const cropImage = async path => {
5656
try {
57-
const [ext, ...rest] = path.split(".").reverse();
58-
const image = sharp(path).resize(CONTAINER_WIDTH);
57+
const ext = path.split(".").reverse()[0];
58+
const image = sharp(path);
59+
const { width, height } = await image.metadata();
60+
const resizeOpts =
61+
width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
62+
? { width: CONTAINER_WIDTH, height: MINIMUM_HEIGHT, fit: "outside" }
63+
: { width: CONTAINER_WIDTH };
64+
const resized = image.clone().resize(resizeOpts);
5965
for (let i = 0; i < 6; i++) {
6066
const filename = `${i}.${ext}`;
6167
const { x, y } = getXY(i);
62-
await image
68+
await resized
6369
.clone()
6470
.extract({ left: x, top: y, width: CUT_WIDTH, height: CUT_HEIGHT })
6571
.toFile(filename);
@@ -71,15 +77,20 @@ const cropImage = async path => {
7177
};
7278

7379
const cropGif = async path => {
80+
const { width, height } = await sharp(path).metadata();
81+
const resizeOpts =
82+
width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
83+
? ["--resize", "_x513"]
84+
: ["--resize", "727x_"];
7485
const resized = "resized.gif";
75-
await execa(gifsicle, ["--resize", "727x_", "-o", resized, path]);
86+
await execa(gifsicle, [...resizeOpts, "-o", resized, path]);
7687
for (let i = 0; i < 6; i++) {
7788
const filename = `${i}.gif`;
7889
const { x, y } = getXY(i);
7990
await execa(gifsicle, [
8091
"--crop",
8192
`${x},${y}+${CUT_WIDTH}x${CUT_HEIGHT}`,
82-
"--output",
93+
"-o",
8394
filename,
8495
resized
8596
]);

resized.gif

-3.04 MB
Binary file not shown.

0 commit comments

Comments
 (0)