Skip to content

Commit

Permalink
Add rounded stickers and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Helvio88 committed Feb 9, 2023
1 parent 1960701 commit 1927147
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ Sticker types are exported as an enum.
enum StickerTypes {
DEFAULT = 'default',
CROPPED = 'crop',
FULL = 'full'
FULL = 'full',
CIRCLE = 'circle,
ROUNDED = 'rounded'
}

```
Expand Down
12 changes: 12 additions & 0 deletions examples/circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ import { Sticker } from '../src'
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Animated Potrait')
const sticker = new Sticker(images.animated.potrait, getOptions('animated', 'potrait'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Animated Landscape')
const sticker = new Sticker(images.animated.landscape, getOptions('animated', 'landscape'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
})()
1 change: 1 addition & 0 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
await import('./crop')
await import('./full')
await import('./circle')
await import('./rounded')
})().catch(console.error)
46 changes: 46 additions & 0 deletions examples/rounded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Sticker } from '../src'
;(async () => {
console.log('\n---\n')
console.log('Rounded example')
console.log('---\n')
const images = {
static: {
potrait: 'https://i.pinimg.com/originals/3a/53/d6/3a53d68345b56241a875595b21ec2a59.jpg',
landscape: 'https://chasinganime.com/wp-content/uploads/2021/02/0_YgtEypuJ2QfMPCbn.jpg'
},
animated: {
potrait: 'https://c.tenor.com/-1mtmQgH5eYAAAAC/watson-amelia-vtuber.gif',
landscape: 'https://c.tenor.com/2RdLoyV5VPsAAAAC/ayame-nakiri.gif'
}
}
const type = 'rounded'
const getOptions = (pack = '', author = '') => ({
pack,
type,
author: `${author}-${type}`
})
await (async () => {
console.log('Static Potrait')
const sticker = new Sticker(images.static.potrait, getOptions('static', 'potrait'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Static Landscape')
const sticker = new Sticker(images.static.landscape, getOptions('static', 'landscape'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Animated Potrait')
const sticker = new Sticker(images.animated.potrait, getOptions('animated', 'potrait'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
await (async () => {
console.log('Animated Landscape')
const sticker = new Sticker(images.animated.landscape, getOptions('animated', 'landscape'))
await sticker.toFile()
console.log(`Saved to ${sticker.defaultFilename}`)
})()
})()
3 changes: 2 additions & 1 deletion src/internal/Metadata/StickerTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export enum StickerTypes {
DEFAULT = 'default',
CROPPED = 'crop',
FULL = 'full',
CIRCLE = 'circle'
CIRCLE = 'circle',
ROUNDED = 'rounded'
}
21 changes: 19 additions & 2 deletions src/internal/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ const convert = async (
let image = isVideo ? await videoToGif(data) : data
const isAnimated = isVideo || mime.includes('gif')

if (isAnimated && ['crop', 'circle'].includes(type)) {
if (isAnimated && ['crop', 'circle', 'rouded'].includes(type)) {
const filename = `${tmpdir()}/${Math.random().toString(36)}.webp`
await writeFile(filename, image)
;[image, type] = [
await crop(filename),
type === StickerTypes.CIRCLE ? StickerTypes.CIRCLE : StickerTypes.DEFAULT
type === StickerTypes.CIRCLE
? StickerTypes.CIRCLE
: type === StickerTypes.ROUNDED
? StickerTypes.ROUNDED
: StickerTypes.DEFAULT
]
}

Expand Down Expand Up @@ -53,6 +57,19 @@ const convert = async (
}
])
break

case StickerTypes.ROUNDED:
img.resize(512, 512, {
fit: fit.cover
}).composite([
{
input: Buffer.from(`<svg><rect rx="50" ry="50" width="512" height="512" fill="${background}"/></svg>`),
blend: 'dest-in',
gravity: 'northeast',
tile: true
}
])
break
}

return await img
Expand Down
38 changes: 38 additions & 0 deletions tests/Sticker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ describe('Sticker', () => {
const { height, width } = sizeOf(buffer)
assert.equal(height, width)
})

it('should create a circle sticker same height and width', async () => {
const sticker = new Sticker(images.static.landscape, {
type: StickerTypes.CIRCLE
})
const buffer = await sticker.build()
const { height, width } = sizeOf(buffer)
assert.equal(height, width)
})

it('should create a rounded sticker same height and width', async () => {
const sticker = new Sticker(images.static.landscape, {
type: StickerTypes.ROUNDED
})
const buffer = await sticker.build()
const { height, width } = sizeOf(buffer)
assert.equal(height, width)
})
})

describe('Animated Stickers', () => {
Expand All @@ -60,6 +78,26 @@ describe('Sticker', () => {
assert.equal(width, 512)
})

it('should create an animated circle sticker with size 512x512', async () => {
const sticker = new Sticker(images.animated.potrait, {
type: StickerTypes.CIRCLE
})
const buffer = await sticker.build()
const { height, width } = sizeOf(buffer)
assert.equal(height, 512)
assert.equal(width, 512)
})

it('should create an animated rounded sticker with size 512x512', async () => {
const sticker = new Sticker(images.animated.potrait, {
type: StickerTypes.ROUNDED
})
const buffer = await sticker.build()
const { height, width } = sizeOf(buffer)
assert.equal(height, 512)
assert.equal(width, 512)
})

it('should create an animated sticker same height and width', async () => {
const sticker = new Sticker(images.animated.potrait, {
type: StickerTypes.FULL
Expand Down

0 comments on commit 1927147

Please sign in to comment.