1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-29 22:48:10 +02:00

CLI: Fix: Do not resize images if they are already below the max dimensions

This commit is contained in:
Laurent Cozic
2019-05-12 11:38:33 +01:00
parent c27861d40f
commit ed3970be81
2 changed files with 46 additions and 8 deletions

View File

@@ -59,12 +59,13 @@ function shimInit() {
}
const resizeImage_ = async function(filePath, targetPath, mime) {
const maxDim = Resource.IMAGE_MAX_DIMENSION;
if (shim.isElectron()) { // For Electron
const nativeImage = require('electron').nativeImage;
let image = nativeImage.createFromPath(filePath);
if (image.isEmpty()) throw new Error('Image is invalid or does not exist: ' + filePath);
const maxDim = Resource.IMAGE_MAX_DIMENSION;
const size = image.getSize();
if (size.width <= maxDim && size.height <= maxDim) {
@@ -85,13 +86,19 @@ function shimInit() {
} else { // For the CLI tool
const sharp = require('sharp');
const image = sharp(filePath);
const md = await image.metadata();
if (md.width <= maxDim && md.height <= maxDim) {
shim.fsDriver().copy(filePath, targetPath);
return;
}
return new Promise((resolve, reject) => {
sharp(filePath)
.resize(Resource.IMAGE_MAX_DIMENSION, Resource.IMAGE_MAX_DIMENSION, {
image.resize(Resource.IMAGE_MAX_DIMENSION, Resource.IMAGE_MAX_DIMENSION, {
fit: 'inside',
withoutEnlargement: true,
})
.toFile(targetPath, (err, info) => {
}).toFile(targetPath, (err, info) => {
if (err) {
reject(err);
} else {