Skip to content

Commit

Permalink
Load Images to Batch加载文件最大宽度1024
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowcz007 committed Apr 21, 2024
1 parent 13110fa commit 4d6b167
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion web/javascript/image_mixlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@ import { api } from '../../../scripts/api.js'
import { $el } from '../../../scripts/ui.js'
import { applyTextReplacements } from '../../../scripts/utils.js'

function loadImageToCanvas (base64Image) {
var img = new Image()
var canvas = document.createElement('canvas')
var ctx = canvas.getContext('2d')
return new Promise((res, rej) => {
img.onload = function () {
// 等比例缩放图片
var width = img.width
var height = img.height
var max_width = 1024
if (width > max_width) {
height *= max_width / width
width = max_width
}

// 设置canvas尺寸
canvas.width = width
canvas.height = height

// 在canvas上绘制图片
ctx.drawImage(img, 0, 0, width, height)

// 将canvas转换为base64图片数据
var canvasData = canvas.toDataURL()
res(canvasData) // canvas转换后的base64图片数据
}

img.src = base64Image
})
}

async function uploadImage (blob, fileType = '.svg', filename) {
// const blob = await (await fetch(src)).blob();
const body = new FormData()
Expand Down Expand Up @@ -713,7 +744,9 @@ app.registerExtension({
const file = e.target.files[0]
const reader = new FileReader()
reader.onload = async event => {
const base64 = event.target.result
let base64 = event.target.result
//压缩图片,控制1024以内
base64 = await loadImageToCanvas(base64)
// console.log(base64)
if (!imagesWidget.value) imagesWidget.value = { base64: [] }
imagesWidget.value.base64.push(base64)
Expand Down

0 comments on commit 4d6b167

Please sign in to comment.