Background requirements:

I personally made a small program for alumni exchange. There are alumni album, alumni reunion, alumni information, alumni mutual assistance and other functions, alumni from the local album to select one or more pictures, together with the article content saved to the server. Here relates to the content security, submit the audit did not pass is also because this did not do content security. Prevent some pornographic and vulgar things.

Advantages and disadvantages of currently available image review schemes:

Option 1: The synchronization API of the applet: security.imgSecCheck

Advantages: small program with its own, no need to additional application, format support PNG, JPEG, JPG, GIF, a single APPID call up to 2000 times/minute, 200,000 times/day, basically meet the needs

Disadvantages: image size limit 1M, image size no larger than 750px x 1334px

Although the size of the picture is limited, it is found in practical application that the size up to 4000 x 4000 is OK, but problems will occur if the size is above 4000

Option 2: The asynchronous API that comes with the applet: security.mediaCheckAsync

Advantages: The size of a single file is no more than 10M, which can solve the limitation of large pictures in mobile phone photos or albums

Disadvantages: it is an asynchronous call, the picture must be uploaded first, the processing is not timely, it may return the result for 30 minutes at most, the need to provide callback URL to accept the processing results, unhealthy pictures can not be processed in real time, easy to cause risks

Option 3: Small program serves the market for coral picture security

Pros: Very good, but it’s off the shelves (April 9, 2021) and downright depressing

Scheme 4: small program service market tianyu

Just online not long ago, according to the official document can not be called successfully, and only 30 days of free use

Scheme 5: Tencent Cloud: T-Sec Tianyu Picture Content Security

Advantages: can accurately identify pictures may be objectionable, unsafe or inappropriate content, support configuration picture blacklist, identify custom picture types.

Disadvantages: document invocation is complex and expensive

Plan 6: Baidu Cloud: content review platform

Advantages: Deep learning based intelligent content audit program, accurate filtering of pornographic, violent and terrorist, politically sensitive, advertising, disgusting, bad scenes and other illegal content in images and videos, but also from the beautiful,

Clear and other dimensions of the image screening, close to the business needs, release the audit manpower

Disadvantages: 1) or the price!! 2) Access cost

My solution

Considering the advantages and disadvantages of the above schemes, I still choose scheme 1 (security.imgseccheck) for the following reasons

1 small program with its own API, easy to call, especially the cloud function call is convenient

2 There is no additional application process

3 There are no expenses

4. The upper limit of measurement times basically meets the needs of my alumni recording applet

Imgseccheck Imgseccheck Imgseccheck Imgseccheck Imgseccheck Imgseccheck

  1. The image size limit is 1M
  2. (750px x 1334px)

My solution

Although the official document had some upper limits on the size of images, which was why I hesitated at the time,

However, it is found in practical application that detection can be achieved at the size up to 4000 x 4000, but problems will occur above 4000px.

The above large size is rarely seen in the actual picture, so it can be ignored for the time being.

Only images larger than 1M need to be compressed

If the picture is larger than 1M, select the picture -> judge whether the picture is larger than 1M -> compress the picture -> upload the picture to the security. Imgseccheck -> pass –> save the picture

If the picture is less than 1M, select the picture –> judge whether the picture is greater than 1M –> does not compress the picture –> uploads the picture to security.imgseccheck –> passes –> saves the picture

Code implementation:

The small program end

/ * *

/ bindChooseImgTap: function (e) {wx.ChooseImage ({count: This. Data. ImgMax - this. Data. ImgList. Length, / / the default sizeType: [' compressed], / / you can specify it is original or a compressed, the default both sourceType: ['album', 'camera'], // Select Success: async (res) => {wx.showLoading({title: 'Album ', mask: true}); for (let k = 0; k < res.tempFiles.length; k++) { let size = res.tempFiles[k].size; let path = res.tempFiles[k].path; if (! contentCheckHelper.imgTypeCheck(path)) { wx.hideLoading(); Return PageHelper. showNoneToast(' PNG, JPG, JPEG only ', 3000); } let imageMaxSize = 1024 * 1000 * this.data.imgUploadSize; console.log('IMG SIZE=' + size + ',' + size / 1024 + 'K'); if (! contentCheckHelper.imgSizeCheck(size, imageMaxSize)) { wx.hideLoading(); Return PageHelper. showNonetoast (' single image size cannot exceed '+ this.data. imguploadSize + 'M', 3000); } if (this.data.isCheck) {let that = this; let callback = async function (path) { let check = await contentCheckHelper.imgCheckCloud(path); if (! check) { wx.hideLoading(); return false; //return PageHelper. showNonetoast (' Inappropriate image exists, blocked ', 3000); } that.setData({ imgList: that.data.imgList.concat(path) }); that.triggerEvent('myImgUploadEvent', that.data.imgList); return true; } // if (size > 1000 * 1000) {wx.compressImage({SRC: path, // quality: 50, // fail: Function () {PageHelper.ShowModal (' Upload failed, please try again '); }, success: async function (res) { await callback(res.tempFilePath); }}); } else {// < 1M, do not compress if (! await callback(path)) return; }} else {this.setData({imgList: this.data.imglist. Concat (path)}); this.triggerEvent('myImgUploadEvent', this.data.imgList); } } wx.hideLoading(); }}); },

The cloud

async function checkImg(imgData, mine) {

// Download the CDN image to check it const axios = require('axios'); let buffer = null; await axios({ method: 'get', url: imgData, responseType: 'arraybuffer' }).then(res => { buffer = res.data; }); console.log('buffer SIZE=' + buffer.length / 1024 + 'K'); let cloud = cloudBase.getCloud(); try { const result = await cloud.openapi.security.imgSecCheck({ media: { contentType: 'image/png', value: Buffer //value: buffer. From (imgData, 'base64') // Buffer. From (imgData, 'base64') // console.log('imgcheck', result); if (! result || result.errCode ! == 0) Throw new ApperError (' image content is not appropriate, please change '); } catch (err) { console.log('imgcheck ex', err); If (Err. ErrMsg && Err. ErrMsg. Includes ('invalid media size')) throw new ApperError (' Image too large, please change '); Else if (Err. ErrMsg && Err. ErrMsg. Includes (' Content size out of limit')) throw New ApperError (' Image size too large, please edit '); Else if (Err. ErrMsg && Err. ErrMsg. Includes ('604102')) throw New ApperError (' image too large, please upload smaller image '); Else throw new ApperError (' Image content is not appropriate, please change '); }

}

Test Status:

Alumni Association small program uploading normal pictures does not contain violations of laws and regulations, testing 50 times, all passed. No detection failure has been found after the small program of Alumni Association goes online.