4.1.2 • Published 1 year ago

make-a-gif-commonjs v4.1.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

MAG (make-a-gif)

A pure typescript module to make easier a gif

Contents

Installation

npm install make-a-gif

Overview

No extra dependencies, this module doesnt uses canvas, it uses imagescript

Usage

Constructor

Gif(width, height)

ParameterTypeDescriptionRequiredDefault
widthnumberThe width of images in pixelsno500
heightnumberThe height of images in pixelsno500
qualitynumberThe quality of the gif ((best) 1..30 (worst))no1
const gif = new Gif()
const gif = new Gif(200)
const gif = new Gif(600, 700)
const gif = new Gif(600, 700, 1)

Methods

MethodParameterDescriptionDefault
setLoopsnumberThe number of loops the gif will play, -1 is infinite loops-1
setFramesFrame/Frame[]Set frames to the gifn/a
addFrameFrameAdd a frame to the gifn/a
encoden/aRenderizes the gif, returns a promise Uint8Arrayn/a

Interfaces

InterfaceTypesDescription
Frame{src: SupportedImages, background?: SupportedImages, duration?: number}The src is the main image and the background the image at the back at the src and the duration of the frame in the gif, default 500 miliseconds
SupportedImagesstring | Uint8ArrayA buffer or a string

Examples

Main

//We need to import the module first
import { Gif } from 'make-a-gif'

//This is for get the dirname
import { fileURLToPath } from 'url'
import { join, dirname } from 'path'

//Import fs to write the gif maked, this step is optional
import fs from 'fs/promises'

// To fetch the buffer of an image
import fetch from 'node-fetch'

//This is for get the dirname
// @ts-ignore
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

//An async function because to render the image we recieve a promise
;(async () => {
	// This is an example
	// Fetch the buffer of an image
	const res = await fetch(
		'https://cdn.discordapp.com/attachments/724014357343895703/960220310144184330/unknown.png'
	)

	//We instance the class Gif and give the proportions of width 500 and height 500
	const myGif = new Gif(500, 500)
	//We set 3 images that will be 3 frames
	await myGif.setFrames([
		{
			src: 'https://cdn.discordapp.com/attachments/960206787775201314/960213088974561280/unknown.png'
		},
		{
			src: new Uint8Array(await res.arrayBuffer())
		},
		{
			src: 'https://cdn.discordapp.com/attachments/960206787775201314/960213089536585808/unknown.png',
			background:
				'https://cdn.discordapp.com/attachments/724014357343895703/960220070976565378/unknown.png'
		}
	])

	//Render the image, it will return a Buffer or it will give an error if anything goes wrong
	const Render = await myGif.decode()

	//Writes the gif in this folder
	await fs.writeFile(join(__dirname, 'make-a-gif.gif'), Render)
})()

Discord.js

//We need to import the module first, in your code it will be like import { Gif } from 'make-a-gif'
import { Gif } from '../build/index.js'

// To fetch the buffer of an image
import fetch from 'node-fetch'

//An async function because to render the image we recieve a promise
;(async () => {
	// This is an example
	// Fetch the buffer of an image
	const res = await fetch(
		'https://cdn.discordapp.com/attachments/724014357343895703/960220310144184330/unknown.png'
	)

	//We instance the class Gif and give the proportions of width 500 and height 500
	const myGif = new Gif(500, 500)
	//We set 3 images that will be 3 frames
	await myGif.setFrames([
		{
			src: 'https://cdn.discordapp.com/attachments/960206787775201314/960213088974561280/unknown.png'
		},
		{
			src: new Uint8Array(await res.arrayBuffer())
		},
		{
			src: 'https://cdn.discordapp.com/attachments/960206787775201314/960213089536585808/unknown.png',
			background:
				'https://cdn.discordapp.com/attachments/724014357343895703/960220070976565378/unknown.png'
		}
	])

	//Render the image, it will return a Buffer or it will give an error if anything goes wrong
	const render = await myGif.decode()
	const attachment = new Discord.MessageAttachment(Buffer.from(render.buffer))

	message.channel.send({ attachments: [attachment] })
})()