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

View file

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