simple URL validator with Typescript for Deno 🦕
Usag:
Require
):
Permssion Flags (--allow-net
: for API Requests To Tinify Server--allow-read
: for read images and post it to server for compression process--allow-write
: for create new Image after compression processYou must have a valid Tinify Authentication:
API key
(you can get your own from here)
If you need to save the compressed image to a server :
import { Tinify } from "https://deno.land/x/tinify/mod.ts";
// Read file frome the server
const img = await Deno.readFile('test/Uncompressed-image.jpg') ;
// Image URL
const imgURL = "https://www.gravatar.com/avatar/ea52ed86c481ca2816b8403acdbc9169?s=512"
// Replace <API_KEY> with your tinify api key for more info visite https://tinypng.com/developers
const tinify = new Tinify({
api_key:"<API_KEY>"
})
const compressedImage:any = await (await tinify.compress(img)) // Image compression process from Uint8Array encode (file)
const compressedImageFromURl:any = await (await tinify.compress(imgURL)) // Image compression process from url
console.log(await compressedImage.saveTo('test/Compressed-image.jpg')) // save new compressed image in the server
Output:
{
input: { size: 402678, type: "image/jpeg" },
output: {
size: 168904,
type: "image/jpeg",
width: 1920,
height: 1280,
ratio: 0.4195,
url: "https://api.tinify.com/output/r72h1n39uugta3p2yybnqukdj3rbwhv6"
},
compressedImagePath: "test/Compressed-image.jpg"
}
If you need to convert the compressed image to base64 string :
const compressedImage:any = await (await tinify.compress(img)) // Image compression process from Uint8Array encode (file) or url
console.log(await compressedImage.toBase64())
Output:
{
input: { size: 402678, type: "image/jpeg" },
output: {
size: 168904,
type: "image/jpeg",
width: 1920,
height: 1280,
ratio: 0.4195,
url: "https://api.tinify.com/output/r72h1n39uugta3p2yybnqukdj3rbwhv6"
},
base64Image: "base64 string of the compressed image"
}
Compressed
):
After (- Size: 168904 Byte (164.9 KB)
This picture is taken from Unsplash
Unsplash
Original
):
Before (- Size: 402678 Byte (393.2 KB)