mirror of https://github.com/laurent22/joplin.git
Desktop: Resize custom icon down to 256px when it is too large
parent
9036bc4fd1
commit
064891d097
|
@ -18,6 +18,7 @@ const timers = require('timers');
|
||||||
const zlib = require('zlib');
|
const zlib = require('zlib');
|
||||||
const dgram = require('dgram');
|
const dgram = require('dgram');
|
||||||
const { basename, fileExtension, safeFileExtension } = require('./path-utils');
|
const { basename, fileExtension, safeFileExtension } = require('./path-utils');
|
||||||
|
const nativeImage = require('electron').nativeImage;
|
||||||
|
|
||||||
function fileExists(filePath) {
|
function fileExists(filePath) {
|
||||||
try {
|
try {
|
||||||
|
@ -159,7 +160,6 @@ function shimInit(options = null) {
|
||||||
|
|
||||||
if (shim.isElectron()) {
|
if (shim.isElectron()) {
|
||||||
// For Electron
|
// For Electron
|
||||||
const nativeImage = require('electron').nativeImage;
|
|
||||||
let image = nativeImage.createFromPath(filePath);
|
let image = nativeImage.createFromPath(filePath);
|
||||||
if (image.isEmpty()) throw new Error(`Image is invalid or does not exist: ${filePath}`);
|
if (image.isEmpty()) throw new Error(`Image is invalid or does not exist: ${filePath}`);
|
||||||
|
|
||||||
|
@ -332,8 +332,7 @@ function shimInit(options = null) {
|
||||||
|
|
||||||
shim.imageToDataUrl = async (filePath, maxSize) => {
|
shim.imageToDataUrl = async (filePath, maxSize) => {
|
||||||
if (shim.isElectron()) {
|
if (shim.isElectron()) {
|
||||||
const nativeImage = require('electron').nativeImage;
|
let image = nativeImage.createFromPath(filePath);
|
||||||
const image = nativeImage.createFromPath(filePath);
|
|
||||||
if (!image) throw new Error(`Could not load image: ${filePath}`);
|
if (!image) throw new Error(`Could not load image: ${filePath}`);
|
||||||
|
|
||||||
const ext = fileExtension(filePath).toLowerCase();
|
const ext = fileExtension(filePath).toLowerCase();
|
||||||
|
@ -341,7 +340,19 @@ function shimInit(options = null) {
|
||||||
|
|
||||||
if (maxSize) {
|
if (maxSize) {
|
||||||
const size = image.getSize();
|
const size = image.getSize();
|
||||||
if (size.width > maxSize || size.height > maxSize) throw new Error(`Image cannot be larger than ${maxSize}x${maxSize} pixels`);
|
|
||||||
|
if (size.width > maxSize || size.height > maxSize) {
|
||||||
|
console.warn(`Image is over ${maxSize}px - resizing it: ${filePath}`);
|
||||||
|
|
||||||
|
const options = {};
|
||||||
|
if (size.width > size.height) {
|
||||||
|
options.width = maxSize;
|
||||||
|
} else {
|
||||||
|
options.height = maxSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
image = image.resize(options);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return image.toDataURL();
|
return image.toDataURL();
|
||||||
|
@ -354,7 +365,6 @@ function shimInit(options = null) {
|
||||||
if (options === null) options = {};
|
if (options === null) options = {};
|
||||||
|
|
||||||
if (shim.isElectron()) {
|
if (shim.isElectron()) {
|
||||||
const nativeImage = require('electron').nativeImage;
|
|
||||||
let image = nativeImage.createFromDataURL(imageDataUrl);
|
let image = nativeImage.createFromDataURL(imageDataUrl);
|
||||||
if (image.isEmpty()) throw new Error('Could not convert data URL to image - perhaps the format is not supported (eg. image/gif)'); // Would throw for example if the image format is no supported (eg. image/gif)
|
if (image.isEmpty()) throw new Error('Could not convert data URL to image - perhaps the format is not supported (eg. image/gif)'); // Would throw for example if the image format is no supported (eg. image/gif)
|
||||||
if (options.cropRect) {
|
if (options.cropRect) {
|
||||||
|
|
Loading…
Reference in New Issue