1.0.5 • Published 2 years ago

imastify v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Library for downloading images via http/https

Supported formats

  • image/avif
  • image/svg+xml
  • image/png
  • image/jpeg
  • image/jpg
  • image/webp
  • image/bmp
  • image/gif

Examples

  • Base
import { ImagesPayload } from 'imastify';
import fs from "fs";

const payload = new ImagesPayload({
    // example headers
    headers: {
        Cookie: "name=cookie",
    },
    
    // http or https agent
    agent,
});

const {
    // Image Data
    image,
    // Image size in bytes
    size,
    // MIME ContentType
    contentType,
    // File extension - png, jpg and etc.
    type,
    // Image Url
    url,
    // Download status
    downloaded,
    //  Image hash
    hash,
} = await payload.request(
    "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
);

// save to file
fs.writeFileSync("google-logo.png", image.source);

// transform to base64
console.log(image.transform('base64'));
  • with handler
import { ImagesPayload } from "imastify";
import fs from "fs";

const payload = new ImagesPayload({
    // example headers
    headers: {
        Cookie: "name=cookie",
    },
    
    // http or https agent
    agent
});

const { downloaded } = await payload.request(
  "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
  {
    transforms: {
      avif: 'jpg', // {type: 'jpg', quality: 50}
      gif: 'png' // {type: 'png', quality: 100}
    },
    fracture: async ({ hash }) => {
      const state = await db.select(`SELECT * FROM t_images WHERE hash = ?`, [
        hash,
      ]);

      /**
       * After the hash is generated, you have the option to continue
       * downloading the image or abort it, false - abort the download
       */

      return state === 0; // false
    },
  }
);

console.log(`Image download status > `, downloaded); // Image download status > false
1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago