Home > Web Front-end > JS Tutorial > About electron-vue image compression

About electron-vue image compression

铁手
Release: 2021-07-21 09:01:59
Original
1747 people have browsed it

Recently I used electron-vue (Electron: based on Chromium and Node.js, allowing you to build applications using HTML, CSS and JavaScript) to create a small image compression software. It is still needed for daily work and supports commonly used Image format;

You can set the compression ratio

1. Support setting fixed width and height

2. Support fixed width or height (according to the original image ratio) Reduction)

3. Supports equal proportion reduction

The specific operations are as follows:

1. The front end transmits the path of the image and the path after saving and compressing it. For the backend, this can be communicated through the electron event.

The ipcRenderer.send event is called, and an event name is passed in. The backend can receive it with ipcRenderer.on.

Required here Note that we can use ipcRenderer.once to receive messages once to avoid receiving messages repeatedly.

Please refer to the following code for details:

await request('resize', this.form.imgPath , this.form.imgSavePath)
  request方法是对ipcRenderer.send做的一个封装
  async function request( event = '', ...params) {
  if (!event) {
    return
  }
  // 构造promise
  const reply = new Promise((resolve, reject) => {
    // 数据返回事件,事先约定
    const eventReply = `${event}-reply`
    ipcRenderer.once(eventReply, (event, data) => {
      resolve(data)
    })
  })
  // 触发事件
  ipcRenderer.send(event, ...params)
  // 返回promise
  return reply
}
Copy after login

2. The backend has just started to consider using the image-size library to obtain the width and height of the image; the usage is as follows: just pass in the path of the image

const sizeOf = require('image-size')
 let dimensions = sizeOf(path)
  if(!dimensions){
    return
  }
  let width = dimensions.width
  let height = dimensions.height
Copy after login

After testing, I found a small problem, that is, the acquisition fails occasionally. If there is a problem, it cannot be used. There is no problem in using this probe-image-size library after querying later. The usage method is as follows:

const probe = require('probe-image-size');
 let dimensions = probe.sync(require('fs').readFileSync(path))
  if(!dimensions){
    return
  }
  let width = dimensions.width
  let height = dimensions.height
Copy after login

3. Finally, perform compression. Pass in the output image path and saved width and height, and call resize-optimize. -images library, the code is as follows:

fs.readFile(path, function (err, originBuffer) {
    if (err) {
      return
    }
    output = output + `/${basename(path)}`
    fs.writeFile(output, originBuffer, async function (err) {
      if (err) {
        return
      }
      const options = {
        images: [output],
        width,
        height,
        quality: 95,
      }
      //执行压缩.
      await resizeOptimizeImages(options)
    })
  })
Copy after login

Summary:

The above are some minor problems encountered in the process of using image-size, although they have not been solved from the root cause Problems, but you can also learn something from them. Sometimes if you want to achieve the functions you want, you cannot stick to one method. You need to learn to be flexible. More importantly, you must learn to analyze and solve problems.

For more information, please pay attention to other related articles on the php Chinese website!

The above is the detailed content of About electron-vue image compression. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template