Skip to content
This repository has been archived by the owner on Apr 4, 2021. It is now read-only.

Commit

Permalink
rewrite image process, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Apr 18, 2018
1 parent 49531a4 commit 7dca290
Show file tree
Hide file tree
Showing 37 changed files with 1,042 additions and 951 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ typings/
assets/fonts/*.ttf
assets/discard/
config.json
old_photobox/
old_photobox_*/
old_commands/
Icons/
146 changes: 146 additions & 0 deletions Classes/ImageCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const Jimp = require('jimp')
const path = require('path')
const gm = require('gm')
const im = require('gm').subClass({ imageMagick: true })
const GIFEncoder = require('gifencoder')
const webshot = require('webshot')

module.exports = class ImageCode {
constructor(im) {
this.im = im
}

process(msg) { }


rInt(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min
}

rBool(){
return this.rInt(0,1) === 1
}

// SENDING

sendJimp(msg, img) {
console.log('sending a jimp image')
img.getBuffer(Jimp.MIME_PNG, (err, buf) => {
if (err) throw err
return this.sendBuffer(msg, buf)
})
}

sendIM(msg, img) {
img.setFormat('png').toBuffer(function (err, buf) {
if (err) throw err
return this.sendBuffer(msg, buf)
})
}

sendBuffer(msg, buf) {
msg.status = 'success'
msg.buffer = buf.toString("base64")
return process.send(msg)
}

async sendGIF(msg, width, height, frames, repeat, delay){
this.sendBuffer(msg, await this.createGif(width, height, frames, repeat, delay))
}

// BUFFERS

jimpBuffer(img) {
return new Promise((f, r) => {
img.getBuffer(Jimp.MIME_PNG, (err, buffer) => {
if (err) return r(err)
f(buffer)
})
})
}

imBuffer(img) {
return new Promise((f, r) => {
img.setFormat('png').toBuffer(function (err, buffer) {
if (err) return r(err)
f(buffer);
})
})
}

// CONVERSION

async jimpToIM(img) {
return im(await this.jimpBuffer(img))
}

async imToJimp(img) {
return await Jimp.read(await this.imBuffer(img))
}

// CREATE STUFF

async createCaption(options) {
if (!options.text) throw new Error('No text provided')
if (!options.font) throw new Error('No font provided')
if (!options.size) throw new Error('No size provided')
if (!options.fill) options.fill = 'black'
if (!options.gravity) options.gravity = 'Center'

let image = im().command('convert')

image.font(path.join(__dirname, '..', 'assets', 'fonts', options.font))
image.out('-size').out(options.size)

image.out('-background').out('transparent')
image.out('-fill').out(options.fill)
image.out('-gravity').out(options.gravity)
if (options.stroke) {
image.out('-stroke').out(options.stroke)
if (options.strokewidth) image.out('-strokewidth').out(options.strokewidth)
}
image.out(`caption:${options.text}`)
if (options.stroke) {
image.out('-compose').out('Over')
image.out('-size').out(options.size)
image.out('-background').out('transparent')
image.out('-fill').out(options.fill)
image.out('-gravity').out(options.gravity)
image.out('-stroke').out('none')
image.out(`caption:${options.text}`)
image.out('-composite')
}
return await this.imBuffer(image)
}

createGif(width, height, frames, repeat, delay) {
return new Promise((resolve, reject) => {
let buffers = [];
let encoder = new GIFEncoder(width, height)
let stream = encoder.createReadStream()
stream.on('data', buffer => buffers.push(buffer))
stream.on('end', () => resolve(Buffer.concat(buffers)))
encoder.start()
encoder.setRepeat(repeat)
encoder.setDelay(delay)
frames.map(frame => encoder.addFrame(frame))
encoder.finish();
});
}

webshotHTML(html, width, height){
return new Promise((resolve, reject) => {
let stream = webshot(html, {
siteType: 'html',
shotSize: {
width: width,
height: height
},
quality: 100
})
let bufferArray = []
stream.on('data', buffer => bufferArray.push(buffer))
stream.on('end', () => resolve(Buffer.concat(bufferArray)))
});
}
}
24 changes: 24 additions & 0 deletions Classes/IsNowIllegal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require('path');

let b64ToBuf = function(str) {
var binstr = new Buffer(str || '', 'base64').toString();
var bin = new Uint8Array(binstr.length)
for (var i = 0; i < binstr.length; i++) {
bin[i] = binstr.charCodeAt(i)
}
return bin.buffer
}

module.exports = (text) => {
return new Promise((resolve,reject)=>{
require('python-shell').run("generate-b64.py", {
pythonPath: 'python3',
scriptPath: path.join(__dirname, '/../mods/IsNowIllegal/rotoscope'),
args: [text, path.join(__dirname, '/../mods/IsNowIllegal/Images'), "output.gif"]
}, (err,results)=>{
if(err) reject(err);
console.log(results.length, results[2])
resolve(Buffer.from(results[1], 'base64'));
})
})
};
32 changes: 32 additions & 0 deletions Classes/PhotoCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Command = require('./Command')
const { Util } = require('photobox-core')

module.exports = class PhotoCommand extends Command {
get extension(){
return 'png'
}

get code(){
return this.name
}

async exec(message, args) {
let url = Util.parseURL(message, args[0]);
if(url.toString().startsWith("Error: ")) return message.reply(url.toString())
if(url){
message.channel.startTyping()
try {
let buffer = await this.sendToProcess(message, { code: this.code, avatar: url })
message.channel.send({ files: [{ attachment: buffer, name: `${this.code}.${this.extension}` }] })
} catch (e) {
Util.sendError(message, e)
} finally {
message.channel.stopTyping()
}
}
}

get permissions() {
return ['attach']
}
}
31 changes: 31 additions & 0 deletions Classes/TextCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Command = require('./Command')
const { Util } = require('photobox-core')

module.exports = class TextCommand extends Command {
get extension(){
return 'png'
}

get code(){
return this.name
}

async exec(message, args) {
let text = Util.stripPrefixClean(message).split(' ').slice(1).join(' ')
if(!text) return message.channel.send("Provide text or a mention for this to work!")
if(text.match(/^<#(\d{17,19})>$/g) && message.mentions.channels[0]) text = "#" + message.mentions.channels[0].name
if(text.match(/^<@&(\d{17,19})>$/g) && message.mentions.roles[0]) text = "@" + message.mentions.roles[0].name
if(text.match(/^<(@|@!)(\d{17,19})>$/g) && message.mentions.members[0]) text = "@" + message.mentions.roles[0].displayName
message.channel.startTyping()
try {
let buffer = await this.sendToProcess(message, { code: this.code, text })
message.channel.send({ files: [{ attachment: buffer, name: `${this.code}.${this.extension}` }] })
} catch (e) {
Util.sendError(message, e)
} finally {
message.channel.stopTyping()
}
}

get permissions() { return ['attach'] }
}
5 changes: 4 additions & 1 deletion Classes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
Command: require('./Command'),
TextCommand: require('./TextCommand'),
PhotoCommand: require('./PhotoCommand'),
IsNowIllegal: require('./IsNowIllegal'),
Pix2Pix: require('./Pix2Pix')
Pix2Pix: require('./Pix2Pix'),
ImageCode: require('./ImageCode')
}
2 changes: 0 additions & 2 deletions Commands/templates/nickelback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { Command } = require('photobox')
const { Util } = require('photobox-core')
const sf = require('snekfetch')
const is = require("buffer-image-size")

module.exports = class Nickelback extends Command {
get name() { return 'nickelback' }
Expand Down
18 changes: 18 additions & 0 deletions ImageCodes/art.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { ImageCode } = require('photobox')
const Jimp = require('jimp')
const path = require('path')

module.exports = class art extends ImageCode {
async process(msg) {
console.log('aaaart')
let avatar = await Jimp.read(msg.avatar)
avatar.resize(370, 370)

let foreground = await Jimp.read(path.join(__dirname, '..', 'assets', `art.png`))
let img = new Jimp(1364, 1534)
img.composite(avatar, 903, 92).composite(avatar, 903, 860).composite(foreground, 0, 0)
console.log('lool')

this.sendJimp(msg, img)
}
}
20 changes: 20 additions & 0 deletions ImageCodes/bonzibuddy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { ImageCode } = require('photobox')
const Jimp = require('jimp')
const path = require('path')
const colorThief = require('color-thief-jimp')

module.exports = class bonzibuddy extends ImageCode {
async process(msg) {
let text = await Jimp.read(this.createCaption({
text: msg.text,
font: 'VcrOcdMono.ttf',
size: '187x118',
gravity: 'North'
}))

let img = await Jimp.read(path.join(__dirname, '..', 'assets', `bonzibuddy.png`))
img.composite(text, 19, 12)

this.sendJimp(msg, img)
}
}
22 changes: 22 additions & 0 deletions ImageCodes/changemymind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { ImageCode } = require('photobox')
const Jimp = require('jimp')
const path = require('path')
const im = require('gm').subClass({ imageMagick: true })

module.exports = class changemymind extends ImageCode {
async process(msg) {
let body = im(await this.createCaption({
text: msg.text.toUpperCase(),
font: 'impact.ttf',
size: '266x168',
gravity: 'North'
}))
body.command('convert');
body.out('-matte').out('-virtual-pixel').out('transparent').out('-distort').out('Perspective');
body.out("0,0,0,102 266,0,246,0 0,168,30,168 266,168,266,68");
let bodytext = await this.imToJimp(body)
let bg = await Jimp.read(path.join(__dirname, '..', 'assets', `changemymind.png`))
bg.composite(bodytext, 364, 203)
this.sendJimp(msg, bg)
}
}
24 changes: 24 additions & 0 deletions ImageCodes/clint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { ImageCode } = require('photobox')
const Jimp = require('jimp')
const path = require('path')

module.exports = class clint extends ImageCode {
async process(msg) {
let avatar = await Jimp.read(msg.avatar)
avatar.resize(700, 700)

let bgImg = await this.jimpToIM(avatar)
bgImg.command('convert')
bgImg.out('-matte').out('-virtual-pixel').out('transparent')
bgImg.out('-distort').out('Perspective')
bgImg.out("0,0,0,132 700,0,330,0 0,700,0,530 700,700,330,700")

let jBgImg = await this.imToJimp(bgImg)
let foreground = await Jimp.read(path.join(__dirname, '..', 'assets', `clint.png`))

let img = new Jimp(1200, 675)
img.composite(jBgImg, 782, 0).composite(foreground, 0, 0)

this.sendJimp(msg, img)
}
}
20 changes: 20 additions & 0 deletions ImageCodes/clippy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { ImageCode } = require('photobox')
const Jimp = require('jimp')
const path = require('path')
const colorThief = require('color-thief-jimp')

module.exports = class clippy extends ImageCode {
async process(msg) {
let text = await Jimp.read(this.createCaption({
text: msg.text,
font: 'VcrOcdMono.ttf',
size: '290x130',
gravity: 'North'
}))

let img = await Jimp.read(path.join(__dirname, '..', 'assets', `clippy.png`))
img.composite(text, 28, 36)

this.sendJimp(msg, img)
}
}
Loading

0 comments on commit 7dca290

Please sign in to comment.