mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-27 13:33:09 +02:00
Use pureimage instead of canvas
This commit is contained in:
parent
8c872c6b22
commit
792ec23d7a
3 changed files with 12 additions and 9 deletions
|
@ -104,7 +104,6 @@
|
||||||
"bootstrap-vue": "2.0.0-rc.13",
|
"bootstrap-vue": "2.0.0-rc.13",
|
||||||
"bull": "3.7.0",
|
"bull": "3.7.0",
|
||||||
"cafy": "15.1.1",
|
"cafy": "15.1.1",
|
||||||
"canvas": "2.4.1",
|
|
||||||
"chai": "4.2.0",
|
"chai": "4.2.0",
|
||||||
"chalk": "2.4.2",
|
"chalk": "2.4.2",
|
||||||
"cli-highlight": "2.1.0",
|
"cli-highlight": "2.1.0",
|
||||||
|
@ -189,6 +188,7 @@
|
||||||
"promise-sequential": "1.1.1",
|
"promise-sequential": "1.1.1",
|
||||||
"pug": "2.0.3",
|
"pug": "2.0.3",
|
||||||
"punycode": "2.1.1",
|
"punycode": "2.1.1",
|
||||||
|
"pureimage": "0.1.6",
|
||||||
"qrcode": "1.3.3",
|
"qrcode": "1.3.3",
|
||||||
"random-seed": "0.3.0",
|
"random-seed": "0.3.0",
|
||||||
"randomcolor": "0.5.4",
|
"randomcolor": "0.5.4",
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
* Random avatar generator
|
* Random avatar generator
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createCanvas } from 'canvas';
|
const p = require('pureimage');
|
||||||
import * as gen from 'random-seed';
|
import * as gen from 'random-seed';
|
||||||
|
import { WriteStream } from 'fs';
|
||||||
|
|
||||||
const size = 512; // px
|
const size = 256; // px
|
||||||
const n = 5; // resolution
|
const n = 5; // resolution
|
||||||
const margin = (size / n) / 1.5;
|
const margin = (size / n) / 1.5;
|
||||||
const colors = [
|
const colors = [
|
||||||
|
@ -35,9 +36,9 @@ const sideN = Math.floor(n / 2);
|
||||||
/**
|
/**
|
||||||
* Generate buffer of random avatar by seed
|
* Generate buffer of random avatar by seed
|
||||||
*/
|
*/
|
||||||
export function genAvatar(seed: string) {
|
export function genAvatar(seed: string, stream: WriteStream): Promise<void> {
|
||||||
const rand = gen.create(seed);
|
const rand = gen.create(seed);
|
||||||
const canvas = createCanvas(size, size);
|
const canvas = p.make(size, size);
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
ctx.fillStyle = bg;
|
ctx.fillStyle = bg;
|
||||||
|
@ -85,5 +86,5 @@ export function genAvatar(seed: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return canvas.toBuffer();
|
return p.encodePNGToStream(canvas, stream);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import { program } from '../argv';
|
||||||
import { UserProfiles } from '../models';
|
import { UserProfiles } from '../models';
|
||||||
import { networkChart } from '../services/chart';
|
import { networkChart } from '../services/chart';
|
||||||
import { genAvatar } from '../misc/gen-avatar';
|
import { genAvatar } from '../misc/gen-avatar';
|
||||||
|
import { createTemp } from '../misc/create-temp';
|
||||||
|
|
||||||
export const serverLogger = new Logger('server', 'gray', false);
|
export const serverLogger = new Logger('server', 'gray', false);
|
||||||
|
|
||||||
|
@ -73,10 +74,11 @@ router.use(activityPub.routes());
|
||||||
router.use(nodeinfo.routes());
|
router.use(nodeinfo.routes());
|
||||||
router.use(wellKnown.routes());
|
router.use(wellKnown.routes());
|
||||||
|
|
||||||
router.get('/avatar/:x', ctx => {
|
router.get('/avatar/:x', async ctx => {
|
||||||
const avatar = genAvatar(ctx.params.x);
|
const [temp] = await createTemp();
|
||||||
|
await genAvatar(ctx.params.x, fs.createWriteStream(temp));
|
||||||
ctx.set('Content-Type', 'image/png');
|
ctx.set('Content-Type', 'image/png');
|
||||||
ctx.body = avatar;
|
ctx.body = fs.createReadStream(temp);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/verify-email/:code', async ctx => {
|
router.get('/verify-email/:code', async ctx => {
|
||||||
|
|
Loading…
Reference in a new issue