An opportunity to

In life, there is a picture size limit when uploading pictures, and every time you have to open the picture editing tool and then access to reduce, more trouble, simply write a program to use.

Scaffolding selection

First, I checked the official website, but there was no command line to automatically generate the initial code of the program, so I went to Github to find it. Finally, I found the electron-quick-start rep provided by the official website, and modified and added the configuration I needed on it.

Set out to develop

New repository github.com/leeseean/el… First, I studied the image compression library and finally chose compressorJS. The basic idea of development is to upload files, read files into buffers, and then compress files with compressorJS for output. The core code is roughly as follows

    new Compressor(file, {
        quality,
        success(blob) {
            blob.arrayBuffer().then(arrBuf => {
                const buf = new Uint8Array(arrBuf)
                fs.writeFile(path, buf, err => {
                    if (err) {
                        alert(path + 'Compression failed! ')
                    }
                })
            })
        },
        error(err) {
            alert(err.message)
        },
    })
Copy the code

In the pit of

Because the difference between arrayBuffer and buffer was not clear, the arrayBuffer was passed to compressor for compression. As a result, the compressed file cannot be used at all.

packaging

Here, use the electron packager package and run the electron packager./ image compression sprit –all –out./build command to generate the application program.

The finished product

The final product looks like this, a little ugly, functional. The entire code is less than 1 Megabyte, and packaged up is almost 100 megabytes. Further optimization is needed.