Members
WImageProc
- Description:
圖片處理
- Source:
圖片處理
Example
詳見convert、resize、crop、base64範例
Methods
(async) base64(fpInp, ext, optopt) → {Promise}
- Description:
圖片轉Base64字串
- Source:
Example
import fs from 'fs'
import _ from 'lodash-es'
import w from 'wsemi'
import wi from './src/WImageProc.mjs'
let test = async () => {
let pre = ''
let key = 'base64'
let ts = []
_.each(['png', 'svg'], (extIn) => {
_.each([
{
ext: 'jpg',
},
{
ext: 'webp',
},
{
ext: 'png',
opt: {
compressionLevely: 5,
},
},
], (o) => {
ts.push({
in: {
ext: extIn,
},
out: {
...o,
},
})
})
})
await w.pmSeries(ts, async (t) => {
// console.log('t', t)
let fpIn = `./test/cocktail.${t.in.ext}`
let fpOut = `./test/cocktail_${pre}_${key}[${t.in.ext}_to_${t.out.ext}].base64` //html
let ext = t.out.ext
let opt = {
..._.get(t, 'out.opt', {}),
}
let b64 = await wi.base64(fpIn, ext, opt)
// b64 = `
// <img src="${b64}">
// `
fs.writeFileSync(fpOut, b64, 'utf8')
console.log(t.in.ext, t.out.ext, 'ok')
})
}
await test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fpInp |
String | 輸入來源圖片位置字串 |
||
ext |
String | 輸入目標格式字串 |
||
opt |
Object |
<optional> |
{}
|
輸入設定物件,含各種類編碼設定,預設{} |
Returns:
回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息
- Type
- Promise
(async) convert(fpInp, fpOut, optopt) → {Promise}
- Description:
圖片轉換
- Source:
Example
import _ from 'lodash-es'
import w from 'wsemi'
import wi from './src/WImageProc.mjs'
let test = async () => {
let pre = ''
let key = 'convert'
let ts = []
_.each(['png', 'svg'], (extIn) => {
_.each([
{
ext: 'jpg',
},
{
ext: 'webp',
},
{
ext: 'png',
opt: {
compressionLevely: 5,
},
},
], (o) => {
ts.push({
in: {
ext: extIn,
},
out: {
...o,
},
})
})
})
await w.pmSeries(ts, async (t) => {
// console.log('t', t)
let fpIn = `./test/cocktail.${t.in.ext}`
let fpOut = `./test/cocktail_${pre}_${key}[${t.in.ext}_to_${t.out.ext}].${t.out.ext}`
let opt = {
..._.get(t, 'out.opt', {}),
}
let r = await wi.convert(fpIn, fpOut, opt)
console.log(t, r)
})
}
await test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fpInp |
String | 輸入來源圖片位置字串 |
||
fpOut |
String | 輸入目標圖片位置字串 |
||
opt |
Object |
<optional> |
{}
|
輸入設定物件,含各種類編碼設定,預設{} |
Returns:
回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息
- Type
- Promise
(async) crop(fpInp, left, top, width, height, fpOut, optopt) → {Promise}
Example
import _ from 'lodash-es'
import w from 'wsemi'
import wi from './src/WImageProc.mjs'
let test = async () => {
let pre = ''
let key = 'crop'
let ts = []
_.each(['png', 'svg'], (extIn) => {
_.each([
{
ext: 'jpg',
},
{
ext: 'webp',
},
{
ext: 'png',
opt: {
compressionLevely: 5,
},
},
], (o) => {
ts.push({
in: {
ext: extIn,
},
out: {
...o,
},
})
})
})
await w.pmSeries(ts, async (t) => {
// console.log('t', t)
let fpIn = `./test/cocktail.${t.in.ext}`
let fpOut = `./test/cocktail_${pre}_${key}[${t.in.ext}_to_${t.out.ext}].${t.out.ext}`
let opt = {
..._.get(t, 'out.opt', {}),
}
let left = 100
let top = 100
let width = 400
let height = 400
let r = await wi.crop(fpIn, left, top, width, height, fpOut, opt)
console.log(t, r)
})
}
await test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fpInp |
String | 輸入來源圖片位置字串 |
||
left |
Integer | 輸入裁切左邊座標整數,單位px |
||
top |
Integer | 輸入裁切上邊座標整數,單位px |
||
width |
Integer | 輸入裁切圖寬整數,單位px |
||
height |
Integer | 輸入裁切圖高整數,單位px |
||
fpOut |
String | 輸入目標圖片位置字串 |
||
opt |
Object |
<optional> |
{}
|
輸入設定物件,含各種類編碼設定,預設{} |
Returns:
回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息
- Type
- Promise
(async) proc(fpInp, fpOut, optopt) → {Promise}
Example
詳見convert、resize、crop、base64範例
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fpInp |
String | 輸入來源圖片位置字串 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
fpOut |
String | 輸入目標圖片位置字串 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,含各種類編碼設定,預設{} Properties
|
Returns:
回傳Promise,resolve當returnBase64=false時回傳成功訊息,當returnBase64=true時回傳Base64字串,reject回傳錯誤訊息
- Type
- Promise
(async) resize(fpInp, width, height, fpOut, optopt) → {Promise}
- Description:
圖片縮小
- Source:
Example
import _ from 'lodash-es'
import w from 'wsemi'
import wi from './src/WImageProc.mjs'
let test = async () => {
let pre = ''
let key = 'resize'
let ts = []
_.each(['png', 'svg'], (extIn) => {
_.each([
{
ext: 'jpg',
},
{
ext: 'webp',
},
{
ext: 'png',
opt: {
compressionLevely: 5,
},
},
], (o) => {
ts.push({
in: {
ext: extIn,
},
out: {
...o,
},
})
})
})
await w.pmSeries(ts, async (t) => {
// console.log('t', t)
let fpIn = `./test/cocktail.${t.in.ext}`
let fpOut = `./test/cocktail_${pre}_${key}[${t.in.ext}_to_${t.out.ext}].${t.out.ext}`
let opt = {
..._.get(t, 'out.opt', {}),
funGetWidth: (mt) => {
// console.log('mt', mt)
return mt.width / 2
}
}
let r = await wi.resize(fpIn, fpOut, opt)
console.log(t, r)
})
}
await test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fpInp |
String | 輸入來源圖片位置字串 |
||
width |
Integer | 輸入圖寬整數,單位px |
||
height |
Integer | 輸入圖高整數,單位px |
||
fpOut |
String | 輸入目標圖片位置字串 |
||
opt |
Object |
<optional> |
{}
|
輸入設定物件,含各種類編碼設定,預設{} |
Returns:
回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息
- Type
- Promise