程序员的万圣节-开源云开发小程序-丧尸头像
发布于 4 年前 作者 caixiuying 3672 次浏览 来自 分享

3万圣节马上就到啦,有没有想好今年的万圣节干点啥?幽默的程序员可不会这样普通的过节,接下来带你们看看,程序员写的万圣节小程序-丧尸头像。

名字听着有些可怕,但是功能很搞怪,适合万圣节主题,话不多说上图

大家看出来了吧,左边是我,右边是生成的丧尸头像,好吓人。

下面给大家解析一下实现效果,首先我们要做的就是图片安全识别,不能上传违规的头像哦~

// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
// 云函数入口函数
exports.main = async (event, context) => {
  // console.log(event, "1111111111")
  if (event.type == 'imgSecCheck') {
    return imgSecCheck(event);
  } else if (event.type == 'msgSecCheck') {
    return msgSecCheck(event);
  } else {
    return '';
  }
}

// 图片内容检测
async function imgSecCheck(event) {
  try {
    const res = await cloud.downloadFile({
      fileID: event.value,
    })
    const imgResult = await cloud.openapi.security.imgSecCheck({
      media: {
        header: { 'Content-Type': 'application/octet-stream' },
        contentType: 'image/png',
        value: res.fileContent
      }
    })
    return imgResult;
  } catch (err) {
    return err;
  }
}

然后我们再上传图片时调用图片安全检测

// 内容安全检测(图片)。判断图片是否合法,不含有色情,等内容。
function imgSecCheck(imgUrl) {
  wx.showLoading({
    title: '检测图片中',
  })
  return new Promise((resolve, reject)=>{
    wx.cloud.callFunction({
      name: 'contentCheck',
      data: {
        value: imgUrl,
        type:"imgSecCheck"
      },
      success: res => {
        wx.hideLoading();
        console.log(res, '检查结果')
        if (res.result.errCode == 0) {
          // 没问题
          resolve(TIPS.SUCCESS);
        }else if (res.result.errCode == 87014) {
          wx.showToast({
            title: '图片含有敏感违法内容!',
            icon: 'none'
          });
          // 违法删除图片
          wx.cloud.deleteFile({
            fileList: [imgUrl]
          }).then(resu => {
            console.log(resu,'删除图片')
          })
        }else{
          TIPS.error(res)
        }
      },
      fail: res => {
        console.log(res, '报错结果')
        wx.hideLoading();
        TIPS.error(res)
      }
    })
  })
}

上传图片变异接口调用

        wx.uploadFile({
          url: 'https://deepgrave-image-processor-no7pxf7mmq-uc.a.run.app/transform',
          filePath,
          name: 'image',
          header: {
            'Content-Type': 'multipart/form-data'
          },
          formData: {
            method: 'POST' //请求方式
          },
          success(res) {
            const data = res.data
            //do something
            wx.hideLoading()
            if (data == 'No face found') {
              return wx.showToast({
                title: '未检测到人物图像',
                icon: 'none',
                duration: 2500
              })
            }
            _this.setData({
              zombie: data
            })
          },
          fail: () => {
            wx.hideLoading()
          }
        })

到此结果就出来了,功能很简单,玩儿法很特别。下面给大家上一个体验码:

开源链接:https://citizenfour.coding.net/public/zombie-head/zombie-head/git/files

作者:小码农

4 回复

嘿嘿 大佬 代码能拿走宣传一下我的小程序么 哈哈哈

感谢大佬推荐.

大佬卡到上图片那不动了是咋回事

太有趣了,哈哈

回到顶部