Coder Social home page Coder Social logo

deno-tinify's Introduction

simple URL validator with Typescript for Deno ๐Ÿฆ•

Usag:

Permssion Flags (Require):

  • --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 process

Authentication:

You must have a valid Tinify 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"
}

After (Compressed):

  • Size: 168904 Byte (164.9 KB)

Comressed image This picture is taken from Unsplash Unsplash

Before (Original):

  • Size: 402678 Byte (393.2 KB)

Uncomressed image

deno-tinify's People

Contributors

moncefplastin07 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.