当前位置: 代码迷 >> 综合 >> picgo-plugin-imageX火山引擎ImageX插件for picGo
  详细解决方案

picgo-plugin-imageX火山引擎ImageX插件for picGo

热度:50   发布时间:2023-11-21 02:45:20.0

picGo 火山引擎imageX插件

imageX支持图片模板操作,设置模板可以给图片添加水印和压缩等操作;见下图,增加了水印并压缩了图片
在这里插入图片描述

插件依赖了'@volcengine/openapi
源码地址:gitee

const {
     imagex } = require('@volcengine/openapi')
const imagexService = new imagex.ImagexService()const config = (ctx) => {
    let userConfig = ctx.getConfig('picBed.imageX-uploader')if (!userConfig) {
    userConfig = {
    }}const config = [{
    name: 'accessKey',type: 'input',default: userConfig.accessKey || '',message: 'AccessKey不能为空',required: true},{
    name: 'secretKey',type: 'input',default: userConfig.secretKey || '',message: 'SecretKey不能为空',required: true},{
    name: 'serviceId',type: 'input',alias: 'serviceId',default: userConfig.serviceId || '',message: '服务Id',required: true},{
    name: 'region',type: 'input',alias: '地区',default: userConfig.region || 'cn-north-1',message: '例如:cn-north-1 (默认)',required: true},{
    name: 'host',type: 'input',alias: '地区host',default: userConfig.host || 'imagex.volcengineapi.com',message: '例如:imagex.volcengineapi.com (默认)',required: true},{
    name: 'imgTemp',type: 'input',alias: '图片模板',default: userConfig.imgTemp || '',message: '例如:tplv-jafkehkiym-1.png, 可以不配置',required: false},{
    name: 'customUrl',type: 'input',alias: '自定义域名',default: userConfig.customUrl || '',message: '例如:http://bucket.xxx.com',required: true}]return config
}/*** @description: 设置aksk* @param {string} AccessKeyId* @param {string} SecretKey* @return {*}*/
const setAkSk = (ctx, AccessKeyId, SecretKey, region, host) => {
    // 设置akskimagexService.setAccessKeyId(AccessKeyId)imagexService.setSecretKey(SecretKey)// 设置地域imagexService.setRegion(region)imagexService.setHost(host)
}const uploadImage = async (ctx, serviceId, img) => {
    const options = {
    serviceId: serviceId,files: [img]}ctx.log.info('上传')const res = await imagexService.UploadImages(options)ctx.log.info(res)return res
}const handle = async (ctx) => {
    const userConfig = ctx.getConfig('picBed.imageX-uploader')if (!userConfig) {
    throw new Error('未配置参数,请配置imageX上传参数')}const accessKey = userConfig.accessKeyconst secretKey = userConfig.secretKeyconst serviceId = userConfig.serviceIdconst region = userConfig.regionconst host = userConfig.hostconst imgTemp = userConfig.imgTempctx.log.warn('开始上传')// 设置配置信息setAkSk(ctx, accessKey, secretKey, region, host)const customUrl = userConfig.customUrltry {
    // 上次图片const imgList = ctx.outputfor (let i in imgList) {
    let img = imgList[i].bufferif (!img && imgList[i].base64Image) {
    img = Buffer.from(imgList[i].base64Image, 'base64')}const res = await uploadImage(ctx, serviceId, img)if (res.Result && res.Result.Results) {
    let url = res.Result.PluginResult[0].ImageUriif (imgTemp) {
    url += `~${
      imgTemp}`}ctx.log.info(url)delete imgList[i].base64Imagedelete imgList[i].bufferimgList[i]['imgUrl'] = `${
      customUrl}/${
      url}`} else {
    throw new Error('Upload failed')}}return ctx} catch (err) {
    if (err.error === 'Upload failed') {
    ctx.emit('notification', {
    title: '上传失败!',body: '请检查你的配置项是否正确'})} else {
    ctx.emit('notification', {
    title: '上传失败!',body: '请检查你的配置项是否正确'})}throw err}
}module.exports = (ctx) => {
    const register = () => {
    ctx.log.success('imageX加载成功')ctx.helper.uploader.register('imageX-uploader', {
    handle: handle,config: config,name: 'imageX'})}return {
    register,uploader: 'imageX-uploader'}
}
  相关解决方案