1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-14 18:27:44 +02:00

Electron: Use built-in image resizing instead of Sharp

This commit is contained in:
Laurent Cozic 2018-04-22 21:10:43 +02:00
parent d8ccc38d5b
commit d4b19f19a1
2 changed files with 52 additions and 18 deletions

View File

@ -48,7 +48,7 @@
"babel-cli": "^6.26.0", "babel-cli": "^6.26.0",
"babel-preset-react": "^6.24.1", "babel-preset-react": "^6.24.1",
"electron": "^1.7.11", "electron": "^1.7.11",
"electron-builder": "^20.5.1" "electron-builder": "^20.10.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"7zip-bin-mac": "^1.0.1", "7zip-bin-mac": "^1.0.1",
@ -87,7 +87,6 @@
"react-dom": "^16.0.0", "react-dom": "^16.0.0",
"react-redux": "^5.0.6", "react-redux": "^5.0.6",
"redux": "^3.7.2", "redux": "^3.7.2",
"sharp": "^0.17.3",
"smalltalk": "^2.5.1", "smalltalk": "^2.5.1",
"sprintf-js": "^1.1.1", "sprintf-js": "^1.1.1",
"sqlite3": "^3.1.13", "sqlite3": "^3.1.13",

View File

@ -34,7 +34,41 @@ function shimInit() {
return locale; return locale;
} }
const resizeImage_ = async function(filePath, targetPath) { const resizeImage_ = async function(filePath, targetPath, mime) {
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) {
shim.fsDriver().copy(filePath, targetPath);
return;
}
const options = {};
if (size.width > size.height) {
options.width = maxDim;
} else {
options.height = maxDim;
}
image = image.resize(options);
let buffer = null;
if (mime === 'image/png') {
buffer = image.toPNG();
} else if (mime === 'image/jpg' || mime === 'image/jpeg') {
buffer = image.toJPEG(90);
}
if (!buffer) throw new Error('Cannot reisze image because mime type "' + mime + '" is not supported: ' + targetPath);
await shim.fsDriver().writeFile(targetPath, buffer, 'buffer');
} else { // For the CLI tool
const sharp = require('sharp'); const sharp = require('sharp');
const Resource = require('lib/models/Resource.js'); const Resource = require('lib/models/Resource.js');
@ -52,6 +86,7 @@ function shimInit() {
}); });
}); });
} }
}
shim.attachFileToNote = async function(note, filePath) { shim.attachFileToNote = async function(note, filePath) {
const Resource = require('lib/models/Resource.js'); const Resource = require('lib/models/Resource.js');
@ -73,7 +108,7 @@ function shimInit() {
let targetPath = Resource.fullPath(resource); let targetPath = Resource.fullPath(resource);
if (resource.mime == 'image/jpeg' || resource.mime == 'image/jpg' || resource.mime == 'image/png') { if (resource.mime == 'image/jpeg' || resource.mime == 'image/jpg' || resource.mime == 'image/png') {
const result = await resizeImage_(filePath, targetPath); const result = await resizeImage_(filePath, targetPath, resource.mime);
} else { } else {
await fs.copy(filePath, targetPath, { overwrite: true }); await fs.copy(filePath, targetPath, { overwrite: true });
} }