firrst working image compression

This commit is contained in:
DJ2LS 2023-02-26 08:48:51 +01:00
parent 7428d752ff
commit 61a47dbedf
2 changed files with 12 additions and 20 deletions

View file

@ -511,7 +511,7 @@ ipcMain.on("select-file", (event, data) => {
}); });
}); });
//select file //select image file
ipcMain.on("select-user-image", (event, data) => { ipcMain.on("select-user-image", (event, data) => {
dialog dialog
.showOpenDialog({ .showOpenDialog({
@ -523,17 +523,16 @@ ipcMain.on("select-user-image", (event, data) => {
console.log(filepath.filePaths[0]); console.log(filepath.filePaths[0]);
try { try {
//fs.readFile(filepath.filePaths[0], 'utf8', function (err, data) { // read data as base64 which makes conversion to blob easier
//Has to be binary fs.readFile(filepath.filePaths[0], "base64", function (err, data) {
fs.readFile(filepath.filePaths[0], "binary", function (err, data) {
console.log(data.length);
var filename = path.basename(filepath.filePaths[0]); var filename = path.basename(filepath.filePaths[0]);
var mimeType = mime.getType(filename); var mimeType = mime.getType(filename);
console.log(mimeType);
if (mimeType == "" || mimeType == null) { if (mimeType == "" || mimeType == null) {
mimeType = "plain/text"; mimeType = "plain/text";
} }
chat.webContents.send("return-select-user-image", { chat.webContents.send("return-select-user-image", {
data: data, data: data,
mime: mimeType, mime: mimeType,

View file

@ -2,6 +2,8 @@ const path = require("path");
const { ipcRenderer } = require("electron"); const { ipcRenderer } = require("electron");
const { v4: uuidv4 } = require("uuid"); const { v4: uuidv4 } = require("uuid");
const imageCompression = require("browser-image-compression"); const imageCompression = require("browser-image-compression");
const blobUtil = require("blob-util");
// https://stackoverflow.com/a/26227660 // https://stackoverflow.com/a/26227660
var appDataFolder = var appDataFolder =
@ -494,24 +496,15 @@ ipcRenderer.on("return-selected-files", (event, arg) => {
}); });
ipcRenderer.on("return-select-user-image", (event, arg) => { ipcRenderer.on("return-select-user-image", (event, arg) => {
filetype = arg.mime; let imageFiletype = arg.mime;
//console.log(filetype); let imageFile = arg.data;
file = arg.data; imageFile = blobUtil.base64StringToBlob(imageFile, imageFiletype);
//filename = arg.filename;
//console.log(arg.data)
//document.getElementById("userImage").src = '';
//var imageFile = arg.data;
var imageFile = arg.data;
console.log(imageFile);
var options = { var options = {
maxSizeMB: 1, maxSizeMB: 1,
maxWidthOrHeight: 1920, maxWidthOrHeight: 200,
useWebWorker: true, useWebWorker: false,
}; };
imageCompression(imageFile, options) imageCompression(imageFile, options)