Skip to content

Commit

Permalink
thumbs: pre-crop images for square thumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
towerofnix committed Aug 13, 2023
1 parent 38ee553 commit 5b1ab44
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/gen-thumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ const thumbnailSpec = {
'huge': {size: 1600, quality: 90},
'semihuge': {size: 1200, quality: 92},
'large': {size: 800, quality: 93},
'medium': {size: 400, quality: 95},
'small': {size: 250, quality: 85},
'medium': {size: 400, quality: 95, square: true},
'small': {size: 250, quality: 85, square: true},
};

import {spawn} from 'child_process';
Expand Down Expand Up @@ -178,16 +178,23 @@ function generateImageThumbnails(filePath, {spawnConvert}) {
const basename = path.basename(filePath, extname);
const output = (name) => path.join(dirname, basename + name + '.jpg');

const convert = (name, {size, quality}) =>
const convert = (name, {size, quality, square}) =>
spawnConvert([
filePath,
'-strip',
'-resize',
`${size}x${size}>`,
'-interlace',
'Plane',
'-quality',
`${quality}%`,
...
(square
// Scale so the smaller length matches the specified size.
// Then crop about the center. The result will always be
// a square image, scaled as to retain the best resolution
// in the specified dimensions.
? ['-thumbnail', `${size}x${size}^`,
'-gravity', 'center',
'-extent', `${size}x${size}`]
// Scale so the longer length matches the specified size.
// The result will retain the original aspect ratio.
: ['-thumbnail', `${size}x${size}>`]),
'-interlace', 'Plane',
'-quality', `${quality}%`,
output(name),
]);

Expand Down

0 comments on commit 5b1ab44

Please sign in to comment.